Advertisement
Learning000001

Untitled

Feb 12th, 2025
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.EventSystems;
  3. using UnityEngine.Events;
  4.  
  5. public class DraggableHandler : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler
  6. {
  7. [SerializeField] private RectTransform objectHolder;
  8. [SerializeField] private int dragMargin;
  9. [SerializeField] private UnityEvent onDragComplete;
  10. [SerializeField] private UnityEvent onExitScreen;
  11.  
  12. private Vector2 startingPosition;
  13. private Vector2 startingClickPosition;
  14. private bool isDragging;
  15. private Canvas parentCanvas;
  16. private RectTransform canvasRectTransform;
  17. private Vector2 dragOffset;
  18. private Vector2 lastPointerPosition;
  19.  
  20. private void Awake()
  21. {
  22. InitializeComponents();
  23. }
  24.  
  25. private void InitializeComponents()
  26. {
  27. startingPosition = objectHolder.anchoredPosition;
  28. parentCanvas = GetComponentInParent<Canvas>();
  29. canvasRectTransform = parentCanvas.GetComponent<RectTransform>();
  30. }
  31.  
  32. public void OnBeginDrag(PointerEventData _eventData)
  33. {
  34. isDragging = true;
  35. startingClickPosition = _eventData.position;
  36. lastPointerPosition = GetLocalPoint(_eventData.position);
  37. dragOffset = objectHolder.anchoredPosition - lastPointerPosition;
  38. }
  39.  
  40. public void OnEndDrag(PointerEventData _eventData)
  41. {
  42. if (!isDragging)
  43. {
  44. return;
  45. }
  46.  
  47. isDragging = false;
  48. var _distanceDragged = Mathf.Abs(startingClickPosition.y - _eventData.position.y);
  49. if (_distanceDragged<=dragMargin)
  50. {
  51. objectHolder.anchoredPosition = startingPosition;
  52. }
  53. else
  54. {
  55. onDragComplete?.Invoke();
  56. }
  57. }
  58.  
  59.  
  60. public void OnDrag(PointerEventData _eventData)
  61. {
  62. if (!isDragging)
  63. {
  64. return;
  65. }
  66.  
  67. SetNewPosition(_eventData);
  68. CheckScreenVisibility();
  69. }
  70.  
  71. private void SetNewPosition(PointerEventData _eventData)
  72. {
  73. Vector2 _localPoint = GetLocalPoint(_eventData.position);
  74. lastPointerPosition = _localPoint;
  75.  
  76. float _newY = Mathf.Min((_localPoint + dragOffset).y, startingPosition.y);
  77. objectHolder.anchoredPosition = new Vector2(objectHolder.anchoredPosition.x, _newY);
  78. }
  79.  
  80. private Vector2 GetLocalPoint(Vector2 _position)
  81. {
  82. Camera _camera = parentCanvas.renderMode == RenderMode.ScreenSpaceOverlay ? null : parentCanvas.worldCamera;
  83. RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRectTransform, _position, _camera, out Vector2 _localPoint);
  84.  
  85. return _localPoint;
  86. }
  87.  
  88. private void CheckScreenVisibility()
  89. {
  90. if (IsObjectVisible())
  91. {
  92. return;
  93. }
  94.  
  95. isDragging = false;
  96. onExitScreen?.Invoke();
  97. }
  98.  
  99. private bool IsObjectVisible()
  100. {
  101. Vector3[] _corners = new Vector3[4];
  102. objectHolder.GetWorldCorners(_corners);
  103.  
  104. Camera _camera = parentCanvas.renderMode == RenderMode.ScreenSpaceOverlay ? null : parentCanvas.worldCamera;
  105. Rect _screenRect = new Rect(0, 0, Screen.width, Screen.height);
  106.  
  107. foreach (var _corner in _corners)
  108. {
  109. Vector3 _screenPoint = _camera ? _camera.WorldToScreenPoint(_corner) : _corner;
  110. if (_screenRect.Contains(new Vector2(_screenPoint.x, _screenPoint.y)))
  111. {
  112. return true;
  113. }
  114. }
  115.  
  116. return false;
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement