HeadHandRayCaster.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using UnityEngine;
  2. namespace Rokid.UXR.Interaction
  3. {
  4. public class HeadHandRayCaster : BaseRayCaster, IHeadHandDriver
  5. {
  6. [SerializeField]
  7. private HandType hand;
  8. private GestureInput gesInput;
  9. private string dragToTarget = "OnGesDragToTarget";
  10. protected override void Start()
  11. {
  12. base.Start();
  13. if (base.rayInteractor == null)
  14. base.rayInteractor = GetComponent<RayInteractor>();
  15. }
  16. protected override void Init()
  17. {
  18. base.Init();
  19. if (inputOverride == null)
  20. {
  21. inputOverride = GetComponent<GestureInput>();
  22. if (inputOverride == null)
  23. {
  24. inputOverride = gameObject.AddComponent<GestureInput>();
  25. }
  26. }
  27. gesInput = GetComponent<GestureInput>();
  28. gesInput.SetHandType(hand);
  29. pointerEnter = "OnGesPointerEnter";
  30. pointerExit = "OnGesPointerExit";
  31. pointerHover = "OnGesPointerHover";
  32. pointerClick = "OnGesPointerClick";
  33. dragBegin = "OnGesBeginDrag";
  34. drag = "OnGesDrag";
  35. dragEnd = "OnGesEndDrag";
  36. }
  37. protected override bool CanDrag(Vector3 delta)
  38. {
  39. return !dragging && (m_SelectedObj.GetComponent<IGesBeginDrag>() != null || m_SelectedObj.GetComponentInParent<IGesBeginDrag>() != null) && Vector3.SqrMagnitude(delta) >= m_DragThreshold * m_DragThreshold;
  40. }
  41. protected override bool ProcessDrag(Ray ray)
  42. {
  43. RKLog.Info("====HeadHandRayCaster====: ProcessDrag");
  44. // 计算拖拽点的目标位置
  45. var targetHitPosition = ray.origin + ray.direction * oriHitPointDis;
  46. var delta = targetHitPosition - oriHitPoint;
  47. m_SelectedObj.SendMessageUpwards(drag, delta, SendMessageOptions.DontRequireReceiver);
  48. m_SelectedObj.SendMessageUpwards(dragToTarget, targetHitPosition, SendMessageOptions.DontRequireReceiver);
  49. oriHitPoint = targetHitPosition;
  50. return true;
  51. }
  52. public void OnChangeHoldHandType(HandType hand)
  53. {
  54. this.hand = hand;
  55. gesInput.SetHandType(hand);
  56. TriggerPointerDown();
  57. RKLog.Info("====HeadHandRayCaster====: ChangeHoldHandType");
  58. }
  59. protected override bool TriggerPointerDown()
  60. {
  61. RKLog.Info("====HeadHandRayCaster====: TriggerPointerDown");
  62. pressTime = 0;
  63. Raycast(ray, Mathf.Infinity, sortedRaycastResults);
  64. result = FirstRaycastResult();
  65. UpdatePointerEventData();
  66. if (result.gameObject != null)
  67. {
  68. m_FirstSelectedObj = result.gameObject;
  69. OnFirstSelect();
  70. }
  71. else
  72. {
  73. m_FirstSelectedObj = null;
  74. ProcessNothingDownEvent(pointerEventData);
  75. }
  76. return true;
  77. }
  78. public void OnHandPress(HandType hand)
  79. {
  80. }
  81. public void OnHandRelease()
  82. {
  83. }
  84. public void OnBeforeChangeHoldHandType(HandType hand)
  85. {
  86. }
  87. }
  88. }