DragMe.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.EventSystems;
  4. using UnityEngine.UI;
  5. [RequireComponent(typeof(Image))]
  6. public class DragMe : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
  7. {
  8. public bool dragOnSurfaces = true;
  9. private Dictionary<int,GameObject> m_DraggingIcons = new Dictionary<int, GameObject>();
  10. private Dictionary<int, RectTransform> m_DraggingPlanes = new Dictionary<int, RectTransform>();
  11. public void OnBeginDrag(PointerEventData eventData)
  12. {
  13. var canvas = FindInParents<Canvas>(gameObject);
  14. if (canvas == null)
  15. return;
  16. // We have clicked something that can be dragged.
  17. // What we want to do is create an icon for this.
  18. m_DraggingIcons[eventData.pointerId] = new GameObject("icon");
  19. m_DraggingIcons[eventData.pointerId].transform.SetParent (canvas.transform, false);
  20. m_DraggingIcons[eventData.pointerId].transform.SetAsLastSibling();
  21. var image = m_DraggingIcons[eventData.pointerId].AddComponent<Image>();
  22. // The icon will be under the cursor.
  23. // We want it to be ignored by the event system.
  24. var group = m_DraggingIcons[eventData.pointerId].AddComponent<CanvasGroup>();
  25. group.blocksRaycasts = false;
  26. image.sprite = GetComponent<Image>().sprite;
  27. image.SetNativeSize();
  28. if (dragOnSurfaces)
  29. m_DraggingPlanes[eventData.pointerId] = transform as RectTransform;
  30. else
  31. m_DraggingPlanes[eventData.pointerId] = canvas.transform as RectTransform;
  32. SetDraggedPosition(eventData);
  33. }
  34. public void OnDrag(PointerEventData eventData)
  35. {
  36. if (m_DraggingIcons[eventData.pointerId] != null)
  37. SetDraggedPosition(eventData);
  38. }
  39. private void SetDraggedPosition(PointerEventData eventData)
  40. {
  41. if (dragOnSurfaces && eventData.pointerEnter != null && eventData.pointerEnter.transform as RectTransform != null)
  42. m_DraggingPlanes[eventData.pointerId] = eventData.pointerEnter.transform as RectTransform;
  43. var rt = m_DraggingIcons[eventData.pointerId].GetComponent<RectTransform>();
  44. Vector3 globalMousePos;
  45. if (RectTransformUtility.ScreenPointToWorldPointInRectangle(m_DraggingPlanes[eventData.pointerId], eventData.position, eventData.pressEventCamera, out globalMousePos))
  46. {
  47. rt.position = globalMousePos;
  48. rt.rotation = m_DraggingPlanes[eventData.pointerId].rotation;
  49. }
  50. }
  51. public void OnEndDrag(PointerEventData eventData)
  52. {
  53. if (m_DraggingIcons[eventData.pointerId] != null)
  54. Destroy(m_DraggingIcons[eventData.pointerId]);
  55. m_DraggingIcons[eventData.pointerId] = null;
  56. }
  57. static public T FindInParents<T>(GameObject go) where T : Component
  58. {
  59. if (go == null) return null;
  60. var comp = go.GetComponent<T>();
  61. if (comp != null)
  62. return comp;
  63. var t = go.transform.parent;
  64. while (t != null && comp == null)
  65. {
  66. comp = t.gameObject.GetComponent<T>();
  67. t = t.parent;
  68. }
  69. return comp;
  70. }
  71. }