CanvasPointerInputDetector.cs 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright (c) 2022 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 _convertToNormalizedPoint(localPoint);
  49. }
  50. protected override Vector2 _convertToNormalizedPoint(Vector3 worldPosition) {
  51. var localPoint = _getRectTransform().InverseTransformPoint(worldPosition);
  52. return _convertToNormalizedPoint(localPoint);
  53. }
  54. Vector2 _convertToNormalizedPoint(Vector2 localPoint) {
  55. var normalizedPoint = Rect.PointToNormalized(_getRectTransform().rect, localPoint);
  56. normalizedPoint.y = 1 - normalizedPoint.y;
  57. return normalizedPoint;
  58. }
  59. RectTransform _getRectTransform() {
  60. if (_cachedRectTransform == null) {
  61. _cachedRectTransform = GetComponent<RectTransform>();
  62. }
  63. return _cachedRectTransform;
  64. }
  65. protected override bool _positionIsZero(PointerEventData eventData) => eventData.position == Vector2.zero;
  66. // Code specific to Microsoft's Mixed Reality Toolkit.
  67. #if VUPLEX_MRTK
  68. void Start() {
  69. // Add a NearInteractionTouchable script to allow touch interactions
  70. // to trigger the IMixedRealityPointerHandler methods.
  71. var touchable = gameObject.AddComponent<NearInteractionTouchableUnityUI>();
  72. touchable.EventsToReceive = TouchableEventType.Pointer;
  73. }
  74. #endif
  75. }
  76. }