Draggable.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using UnityEngine;
  2. using UnityEngine.EventSystems;
  3. namespace Rokid.UXR.Interaction
  4. {
  5. /// <summary>
  6. /// Draggable 拖拽组件
  7. /// </summary>
  8. [RequireComponent(typeof(RayInteractable))]
  9. public class Draggable : MonoBehaviour, IGesDragToTarget, IGesBeginDrag, IGesEndDrag, IBezierCurveDrag, IRayBeginDrag, IRayDrag, IRayEndDrag, IMouseBeginDrag, IMouseDrag, IMouseEndDrag, IDraggable
  10. {
  11. [SerializeField, Tooltip("拖拽平滑速度")]
  12. private int smoothSpeed = 10;
  13. [SerializeField, Tooltip("跟随该拖拽物体对象")]
  14. private Transform followObj;
  15. private Vector3 offsetPos;
  16. private Vector3 allDelta;
  17. private Vector3 oriPos;
  18. private Vector3 hitLocalPos;
  19. private Vector3 hitLocalNormal;
  20. private bool dragging = false;
  21. private bool gesDragging = false;
  22. private void Start()
  23. {
  24. if (followObj != null)
  25. {
  26. offsetPos = transform.InverseTransformPoint(followObj.position);
  27. }
  28. }
  29. public void OnGesBeginDrag(PointerEventData eventData)
  30. {
  31. allDelta = Vector3.zero;
  32. oriPos = transform.position;
  33. hitLocalPos = transform.InverseTransformPoint(eventData.pointerCurrentRaycast.worldPosition);
  34. hitLocalNormal = transform.InverseTransformVector(eventData.pointerCurrentRaycast.worldNormal);
  35. dragging = true;
  36. gesDragging = true;
  37. }
  38. public void OnGesEndDrag()
  39. {
  40. allDelta = Vector3.zero;
  41. dragging = false;
  42. gesDragging = false;
  43. }
  44. public void OnRayBeginDrag(PointerEventData eventData)
  45. {
  46. allDelta = Vector3.zero;
  47. oriPos = transform.position;
  48. hitLocalPos = transform.InverseTransformPoint(eventData.pointerCurrentRaycast.worldPosition);
  49. hitLocalNormal = transform.InverseTransformVector(eventData.pointerCurrentRaycast.worldNormal);
  50. dragging = true;
  51. }
  52. public void OnRayDrag(Vector3 delta)
  53. {
  54. allDelta += delta;
  55. }
  56. public void OnRayEndDrag()
  57. {
  58. allDelta = Vector3.zero;
  59. dragging = false;
  60. }
  61. public void OnMouseBeginDrag(PointerEventData eventData)
  62. {
  63. allDelta = Vector3.zero;
  64. oriPos = transform.position;
  65. hitLocalPos = transform.InverseTransformPoint(eventData.pointerCurrentRaycast.worldPosition);
  66. hitLocalNormal = transform.InverseTransformVector(eventData.pointerCurrentRaycast.worldNormal);
  67. dragging = true;
  68. }
  69. public void OnMouseRayDrag(Vector3 delta)
  70. {
  71. allDelta += delta;
  72. }
  73. public void OnMouseEndDrag()
  74. {
  75. allDelta = Vector3.zero;
  76. dragging = false;
  77. }
  78. public bool IsEnablePinchBezierCurve()
  79. {
  80. return true;
  81. }
  82. public bool IsEnableGripBezierCurve()
  83. {
  84. return true;
  85. }
  86. public bool IsInBezierCurveDragging()
  87. {
  88. return dragging;
  89. }
  90. public Vector3 GetBezierCurveEndPoint()
  91. {
  92. return transform.TransformPoint(hitLocalPos);
  93. }
  94. public Vector3 GetBezierCurveEndNormal()
  95. {
  96. return transform.TransformVector(hitLocalNormal).normalized;
  97. }
  98. private void Update()
  99. {
  100. if (dragging && !gesDragging)
  101. {
  102. Vector3 targetPos = oriPos + allDelta;
  103. this.transform.position = Vector3.Slerp(this.transform.position, targetPos, Time.deltaTime * smoothSpeed);
  104. // this.transform.position = Utils.SmoothTo(this.transform.position, targetPos, 0.01f, Time.deltaTime);
  105. if (followObj != null)
  106. followObj.position = transform.TransformPoint(offsetPos);
  107. }
  108. }
  109. public void OnGesDragToTarget(Vector3 targetPos)
  110. {
  111. this.transform.position = Vector3.Slerp(this.transform.position, targetPos, Time.deltaTime * smoothSpeed);
  112. // this.transform.position = Utils.SmoothTo(this.transform.position, targetPos, 0.01f, Time.deltaTime);
  113. if (followObj != null)
  114. followObj.position = transform.TransformPoint(offsetPos);
  115. }
  116. }
  117. }