Draggable.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7. public class Draggable : MonoBehaviour
  8. {
  9. private Transform root;
  10. [HideInInspector] public Transform area;
  11. [HideInInspector] public Transform self;
  12. public CanvasGroup canvasGroup = null;
  13. public int oldChildIndex { get; set; } = -1;
  14. public Transform oldParent { get; set; }
  15. public void Awake()
  16. {
  17. this.self = this.transform;
  18. this.area = this.self.parent;
  19. this.root = this.area.parent;
  20. }
  21. public virtual void OnPointerEnter()
  22. {
  23. }
  24. public virtual void OnPointerExit()
  25. {
  26. }
  27. public virtual void OnBeginDrag(BaseEventData eventData)
  28. {
  29. }
  30. public virtual void OnDrag(BaseEventData eventData)
  31. {
  32. this.self.localPosition = GetLocalPosition(Input.mousePosition, this.transform);
  33. }
  34. public static Vector3 GetLocalPosition(Vector3 position, Transform transform)
  35. {
  36. Camera MainCamera = null;
  37. if (GManager.instance != null)
  38. {
  39. MainCamera = GManager.instance.camara;
  40. }
  41. else if (Opening.instance != null)
  42. {
  43. MainCamera = Opening.instance.MainCamera;
  44. }
  45. if (MainCamera != null)
  46. {
  47. // Converts coordinates on the screen (Screen Point) to local coordinates on the RectTransform
  48. RectTransformUtility.ScreenPointToLocalPointInRectangle(
  49. transform.parent.GetComponent<RectTransform>(),
  50. position,
  51. MainCamera,
  52. out var result);
  53. return new Vector3(result.x, result.y, 0);
  54. }
  55. return Vector3.zero;
  56. }
  57. public virtual void OnEndDrag(BaseEventData eventData)
  58. {
  59. if (this != null)
  60. {
  61. if (this.canvasGroup != null)
  62. {
  63. // Temporarily disable UI functions
  64. //this.canvasGroup.blocksRaycasts = false;
  65. // Restore UI functionality
  66. this.canvasGroup.blocksRaycasts = true;
  67. if (GetRaycastArea() != null)
  68. {
  69. if (GetRaycastArea().Count == 0)
  70. {
  71. ReturnDefaultPosition();
  72. }
  73. }
  74. if (oldParent != null)
  75. {
  76. if (oldParent.GetComponent<GridLayoutGroup>() != null)
  77. {
  78. oldParent.GetComponent<GridLayoutGroup>().enabled = true;
  79. }
  80. }
  81. }
  82. }
  83. }
  84. public void ReturnDefaultPosition()
  85. {
  86. if (oldParent != null)
  87. {
  88. this.transform.SetParent(oldParent);
  89. }
  90. if (oldChildIndex >= 0)
  91. {
  92. this.transform.SetSiblingIndex(oldChildIndex);
  93. }
  94. }
  95. /// <summary>
  96. /// Obtains the DropArea at the point where the event occurs.
  97. /// </summary>
  98. /// <param name="eventData">event data</param>
  99. /// <returns>DropArea</returns>
  100. public static List<DropArea> GetRaycastArea()
  101. {
  102. PointerEventData pointer = new PointerEventData(EventSystem.current);
  103. List<RaycastResult> results = new List<RaycastResult>();
  104. // Ray skip to mouse pointer position and save hits
  105. pointer.position = Input.mousePosition;
  106. EventSystem.current.RaycastAll(pointer, results);
  107. List<DropArea> DropAreas = new List<DropArea>();
  108. // Name of the hit UI
  109. foreach (RaycastResult target in results)
  110. {
  111. DropArea dropArea = target.gameObject.GetComponent<DropArea>();
  112. if (dropArea != null)
  113. {
  114. DropAreas.Add(target.gameObject.GetComponent<DropArea>());
  115. }
  116. }
  117. return DropAreas;
  118. }
  119. }