GesPinchRayCaster.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using Rokid.UXR.Utility;
  3. using UnityEngine;
  4. namespace Rokid.UXR.Interaction
  5. {
  6. public class GesPinchRayCaster : BaseRayCaster
  7. {
  8. [SerializeField]
  9. private HandType hand;
  10. [SerializeField, Tooltip("是否过滤Grip手势类型的拖拽")]
  11. private bool filterGripToDrag;
  12. protected override void Init()
  13. {
  14. base.Init();
  15. inputOverride = GetComponent<GesturePinchInput>();
  16. if (inputOverride == null)
  17. inputOverride = gameObject.AddComponent<GesturePinchInput>();
  18. GetComponent<GesturePinchInput>().SetHandType(hand);
  19. pointerEnter = "OnGesPinchEnter";
  20. pointerExit = "OnGesPinchExit";
  21. pointerHover = "OnGesPinchHover";
  22. pointerClick = "OnGesPinchClick";
  23. dragBegin = "OnGesPinchBeginDrag";
  24. drag = "OnGesPinchDrag";
  25. dragEnd = "OnGesPinchEndDrag";
  26. }
  27. private bool GetWorldPointInRectangle(RectTransform rect, Ray ray, out Vector3 worldPoint)
  28. {
  29. worldPoint = Vector2.zero;
  30. Plane plane = new Plane(rect.rotation * Vector3.back, rect.position);
  31. float enter = 0f;
  32. Ray inverseRay = new Ray(ray.origin, -ray.direction);
  33. if (!plane.Raycast(ray, out enter))
  34. {
  35. worldPoint = ray.GetPoint(enter);
  36. return true;
  37. }
  38. else if (!plane.Raycast(inverseRay, out enter))
  39. {
  40. worldPoint = inverseRay.GetPoint(enter);
  41. return true;
  42. }
  43. return false;
  44. }
  45. protected override bool ProcessBeginDrag(Ray ray)
  46. {
  47. RectTransform rect = m_FirstSelectedObj.GetComponent<RectTransform>();
  48. if (GetWorldPointInRectangle(rect, ray, out Vector3 worldPoint))
  49. {
  50. RKLog.Debug($"====NearDragRayCaster==== ProcessBeginDrag");
  51. result.worldPosition = worldPoint;
  52. pointerEventData.pointerCurrentRaycast = result;
  53. m_SelectedObj.SendMessageUpwards(dragBegin, pointerEventData, SendMessageOptions.DontRequireReceiver);
  54. RKPointerLisener.OnPointerDragBegin?.Invoke(pointerEventData);
  55. }
  56. return true;
  57. }
  58. protected override bool ProcessEndDrag(Ray ray)
  59. {
  60. RKLog.Debug($"====NearDragRayCaster==== ProcessEndDrag");
  61. m_SelectedObj.SendMessageUpwards(dragEnd, pointerEventData, SendMessageOptions.DontRequireReceiver);
  62. RKPointerLisener.OnPointerDragEnd?.Invoke(pointerEventData);
  63. return true;
  64. }
  65. /// <summary>
  66. /// 重写处理拖拽的逻辑
  67. /// </summary>
  68. protected override bool ProcessDrag(Ray ray)
  69. {
  70. if (filterGripToDrag && GesEventInput.Instance.GetGestureType(hand) == GestureType.Grip)
  71. {
  72. return true;
  73. }
  74. RectTransform rect = m_FirstSelectedObj.GetComponent<RectTransform>();
  75. if (GetWorldPointInRectangle(rect, ray, out Vector3 worldPoint))
  76. {
  77. RKLog.Debug($"====NearDragRayCaster==== ProcessDrag");
  78. result.worldPosition = worldPoint;
  79. pointerEventData.pointerCurrentRaycast = result;
  80. m_SelectedObj.SendMessageUpwards(drag, pointerEventData, SendMessageOptions.DontRequireReceiver);
  81. RKPointerLisener.OnPointerDrag?.Invoke(pointerEventData);
  82. }
  83. return true;
  84. }
  85. protected override Vector3 CalDragDelta()
  86. {
  87. return Vector3.one;
  88. }
  89. protected override void OnFirstSelect()
  90. {
  91. }
  92. protected override Camera GetEventCamera()
  93. {
  94. return MainCameraCache.mainCamera;
  95. }
  96. }
  97. }