MouseRayCaster.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using UnityEngine;
  2. namespace Rokid.UXR.Interaction
  3. {
  4. /// <summary>
  5. /// Mouse Ray Caster
  6. /// </summary>
  7. public class MouseRayCaster : BaseRayCaster
  8. {
  9. protected override void Init()
  10. {
  11. base.Init();
  12. pointerEnter = "OnMousePointerEnter";
  13. pointerExit = "OnMousePointerExit";
  14. pointerHover = "OnMousePointerHover";
  15. pointerClick = "OnMousePointerClick";
  16. dragBegin = "OnMouseBeginDrag";
  17. drag = "OnMouseRayDrag";
  18. dragEnd = "OnMouseEndDrag";
  19. }
  20. protected override bool ProcessDrag(Ray ray)
  21. {
  22. // 计算拖拽点的目标位置
  23. var targetHitPosition = transform.position + ray.direction * oriHitPointDis;
  24. var delta = targetHitPosition - oriHitPoint;
  25. m_SelectedObj.SendMessageUpwards(drag, delta, SendMessageOptions.DontRequireReceiver);
  26. oriHitPoint = targetHitPosition;
  27. return true;
  28. }
  29. protected override bool CanDrag(Vector3 delta)
  30. {
  31. return !dragging && (m_SelectedObj.GetComponent<IMouseBeginDrag>() != null || m_SelectedObj.GetComponentInParent<IMouseBeginDrag>() != null) && Vector3.SqrMagnitude(delta) >= m_DragThreshold * m_DragThreshold;
  32. }
  33. }
  34. }