BezierDragUI.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System.Runtime.InteropServices.WindowsRuntime;
  2. using Rokid.UXR.Interaction;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. namespace Rokid.UXR.Components
  6. {
  7. public class BezierDragUI : MonoBehaviour, IDragHandler, IEndDragHandler, IBeginDragHandler, IBezierCurveDrag
  8. {
  9. [SerializeField]
  10. public RectTransform canvas;//得到canvas的ugui坐标
  11. private RectTransform imgRect;
  12. private Vector2 offset;//临时记录点击点与UI的相对位置
  13. private bool IsDraging;
  14. void Start()
  15. {
  16. //初始化组件
  17. imgRect = GetComponent<RectTransform>();
  18. }
  19. public void OnDrag(PointerEventData eventData)
  20. {
  21. // UnityEngine.RKLog.Info("On Drag");
  22. Vector2 mouseDrag = eventData.position; //当鼠标拖动时的屏幕坐标
  23. Vector2 uguiPos; //用来接收转换后的拖动坐标
  24. bool isRect = RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas, mouseDrag, eventData.enterEventCamera, out uguiPos);
  25. if (isRect && Mathf.Abs(uguiPos.x) < Screen.width && Mathf.Abs(uguiPos.y) < Screen.height)
  26. {
  27. //设置图片的ugui坐标与鼠标的ugui坐标保持不变
  28. if (!UseBezierCurve)
  29. {
  30. imgRect.anchoredPosition = offset + uguiPos;
  31. }
  32. else
  33. {
  34. anchoredPos = offset + uguiPos;
  35. }
  36. }
  37. }
  38. public void OnBeginDrag(PointerEventData eventData)
  39. {
  40. IsDraging = true;
  41. // 保存点击点的坐标
  42. HitPoint = transform.InverseTransformPoint(eventData.pointerCurrentRaycast.worldPosition);
  43. //开始拖拽
  44. //这里必须先更换父物体否则,offset会计算出问题
  45. Vector2 mouseDown = eventData.position;
  46. Vector2 mouseUguiPos;
  47. bool isRect = RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas, mouseDown, eventData.enterEventCamera, out mouseUguiPos);
  48. if (isRect && Mathf.Abs(mouseUguiPos.x) < Screen.width && Mathf.Abs(mouseUguiPos.y) < Screen.height)
  49. {
  50. offset = imgRect.anchoredPosition - mouseUguiPos;
  51. }
  52. else
  53. {
  54. offset = imgRect.anchoredPosition;
  55. }
  56. }
  57. public void OnEndDrag(PointerEventData eventData)
  58. {
  59. RKLog.Info("On End Drag");
  60. IsDraging = false;
  61. }
  62. #region BezierCurve
  63. [SerializeField]
  64. private bool UseBezierCurve = true;
  65. [SerializeField]
  66. private float moveLerpTime = 0.05f;
  67. private Vector3 HitPoint;
  68. private Vector2 anchoredPos;
  69. public bool IsEnablePinchBezierCurve()
  70. {
  71. return UseBezierCurve;
  72. }
  73. public bool IsEnableGripBezierCurve()
  74. {
  75. return false;
  76. }
  77. public bool IsInBezierCurveDragging()
  78. {
  79. return IsDraging;
  80. }
  81. public Vector3 GetBezierCurveEndPoint()
  82. {
  83. return transform.TransformPoint(HitPoint);
  84. }
  85. private void Update()
  86. {
  87. if (UseBezierCurve && IsDraging)
  88. {
  89. imgRect.anchoredPosition = Vector2.Lerp(imgRect.anchoredPosition, anchoredPos, 1f - Mathf.Pow(moveLerpTime, Time.deltaTime));
  90. }
  91. }
  92. public Vector3 GetBezierCurveEndNormal()
  93. {
  94. return transform.forward;
  95. }
  96. #endregion
  97. }
  98. }