CanvasPointerInputDetector.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright (c) 2024 Vuplex Inc. All rights reserved.
  2. //
  3. // Licensed under the Vuplex Commercial Software Library License, you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // https://vuplex.com/commercial-library-license
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. using System;
  15. using System.Reflection;
  16. using UnityEngine;
  17. using UnityEngine.EventSystems;
  18. using UnityEngine.UI;
  19. using Vuplex.WebView.Internal;
  20. #if VUPLEX_MRTK
  21. using Microsoft.MixedReality.Toolkit.Input;
  22. #endif
  23. namespace Vuplex.WebView {
  24. [HelpURL("https://developer.vuplex.com/webview/IPointerInputDetector")]
  25. public class CanvasPointerInputDetector : DefaultPointerInputDetector {
  26. RectTransform _cachedRectTransform;
  27. CachingGetter<Canvas> _canvasGetter;
  28. protected override Vector2 _convertToNormalizedPoint(PointerEventData pointerEventData) {
  29. if (_canvasGetter == null) {
  30. _canvasGetter = new CachingGetter<Canvas>(GetComponentInParent<Canvas>, 1, this);
  31. }
  32. var canvas = _canvasGetter.GetValue();
  33. var camera = canvas == null || canvas.renderMode == RenderMode.ScreenSpaceOverlay ? null : canvas.worldCamera;
  34. Vector2 localPoint;
  35. var mousePosition = pointerEventData.position;
  36. #if (UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX) && !UNITY_EDITOR
  37. // To handle multiple displays on Windows and macOS, Display.RelativeMouseAt() must be used
  38. // to translate the mouse position. However, Unity's UI system still has a limitation where
  39. // this may not work when the monitors have different sizes / resolutions.
  40. // - https://issuetracker.unity3d.com/issues/buttons-hitbox-is-offset-when-building-standalone-project-for-two-screens
  41. var positionForDisplay = Display.RelativeMouseAt(new Vector3(mousePosition.x, mousePosition.y));
  42. // RelativeMouseAt() returns Vector3.zero when multiple displays aren't supported.
  43. if (positionForDisplay != Vector3.zero) {
  44. mousePosition = new Vector2(positionForDisplay.x, positionForDisplay.y);
  45. }
  46. #endif
  47. RectTransformUtility.ScreenPointToLocalPointInRectangle(_getRectTransform(), mousePosition, camera, out localPoint);
  48. return _convertVector2ToNormalizedPoint(localPoint);
  49. }
  50. protected override Vector2 _convertToNormalizedPoint(Vector3 worldPosition) {
  51. var localPoint = _getRectTransform().InverseTransformPoint(worldPosition);
  52. var normalizedPoint = _convertVector2ToNormalizedPoint(localPoint);
  53. return normalizedPoint;
  54. }
  55. // Note: This method was originally named _convertToNormalizedPoint(Vector2), but since a Vector3 can be implicitly
  56. // converted to a Vector2, it caused this method to incorrectly be called instead of _convertToNormalizedPoint(Vector3)
  57. // in some cases.
  58. Vector2 _convertVector2ToNormalizedPoint(Vector2 localPoint) {
  59. var normalizedPoint = Rect.PointToNormalized(_getRectTransform().rect, localPoint);
  60. normalizedPoint.y = 1 - normalizedPoint.y;
  61. return normalizedPoint;
  62. }
  63. RectTransform _getRectTransform() {
  64. if (_cachedRectTransform == null) {
  65. _cachedRectTransform = GetComponent<RectTransform>();
  66. }
  67. return _cachedRectTransform;
  68. }
  69. protected override bool _positionIsZero(PointerEventData eventData) => eventData.position == Vector2.zero;
  70. void Start() {
  71. #if VUPLEX_MRTK
  72. // Add a NearInteractionTouchable script to allow touch interactions
  73. // to trigger the IMixedRealityPointerHandler methods.
  74. var touchable = gameObject.AddComponent<NearInteractionTouchableUnityUI>();
  75. touchable.EventsToReceive = TouchableEventType.Pointer;
  76. #endif
  77. }
  78. }
  79. }