Tanoda
DragAndDropItem.cs
Go to the documentation of this file.
1using UnityEngine;
2using UnityEngine.UI;
4using System.Collections.Generic;
5using System.Collections;
6
10public class DragAndDropItem : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
11{
12 public static bool dragDisabled = false; // Drag start global disable
13
14 public static DragAndDropItem draggedItem; // Item that is dragged now
15 public static GameObject icon; // Icon of dragged item
16 public static DragAndDropCell sourceCell; // From this cell dragged item is
17
18 public delegate void DragEvent(DragAndDropItem item);
19 public static event DragEvent OnItemDragStartEvent; // Drag start event
20 public static event DragEvent OnItemDragEndEvent; // Drag end event
21
22 private static Canvas canvas; // Canvas for item drag operation
23 private static string canvasName = "DragAndDropCanvas"; // Name of canvas
24 private static int canvasSortOrder = 31009; // Sort order for canvas
25
29 void Awake()
30 {
31 if (canvas == null)
32 {
33 GameObject canvasObj = new GameObject(canvasName);
34 canvas = canvasObj.AddComponent<Canvas>();
35 canvas.renderMode = RenderMode.ScreenSpaceOverlay;
36 canvas.sortingOrder = canvasSortOrder;
37 }
38 }
39
44 public void OnBeginDrag(PointerEventData eventData)
45 {
46 if (dragDisabled == false)
47 {
48 sourceCell = GetCell(); // Remember source cell
49 draggedItem = this; // Set as dragged item
50 // Create item's icon
51 icon = new GameObject();
52 icon.transform.SetParent(canvas.transform);
53 icon.name = "Icon";
54 RawImage myImage = GetComponentInChildren<RawImage>();
55 if (myImage)
56 {
57 myImage.raycastTarget = false; // Disable icon's raycast for correct drop handling
58 RawImage iconImage = icon.AddComponent<RawImage>();
59 iconImage.raycastTarget = false;
60 iconImage.texture = myImage.texture;
61 //iconImage.color = myImage.color;
62 }
63 else
64 {
65 Image myImage2 = GetComponentInChildren<Image>();
66 myImage2.raycastTarget = false; // Disable icon's raycast for correct drop handling
67 Image iconImage = icon.AddComponent<Image>();
68 iconImage.raycastTarget = false;
69 iconImage.sprite = myImage2.sprite;
70 //iconImage.color = myImage2.color;
71 }
72
73 if (transform.childCount > 0)
74 {
75 var child = Instantiate(transform.GetChild(0).gameObject, icon.transform);
76 if (child.GetComponent<Text>())
77 {
78 var rt = child.GetComponent<RectTransform>();
79 rt.anchorMin = Vector2.zero;
80 rt.anchorMax = Vector2.one;
81 rt.pivot = Vector2.one / 2;
82 }
83 }
84
85 RectTransform iconRect = icon.GetComponent<RectTransform>();
86 // Set icon's dimensions
87 RectTransform myRect = GetComponent<RectTransform>();
88 iconRect.pivot = new Vector2(0.5f, 0.5f);
89 iconRect.anchorMin = new Vector2(0.5f, 0.5f);
90 iconRect.anchorMax = new Vector2(0.5f, 0.5f);
91 iconRect.sizeDelta = new Vector2(myRect.rect.width, myRect.rect.height);
92
93 if (OnItemDragStartEvent != null)
94 {
95 OnItemDragStartEvent(this); // Notify all items about drag start for raycast disabling
96 }
97 }
98 }
99
104 public void OnDrag(PointerEventData data)
105 {
106 if (icon != null)
107 {
108 icon.transform.position = Input.mousePosition; // Item's icon follows to cursor in screen pixels
109 }
110 }
111
116 public void OnEndDrag(PointerEventData eventData)
117 {
118 ResetConditions();
119 }
120
124 private void ResetConditions()
125 {
126 if (icon != null)
127 {
128 Destroy(icon); // Destroy icon on item drop
129 }
130 if (OnItemDragEndEvent != null)
131 {
132 OnItemDragEndEvent(this); // Notify all cells about item drag end
133 }
134 draggedItem = null;
135 icon = null;
136 sourceCell = null;
137 }
138
143 public void MakeRaycast(bool condition)
144 {
145 Image image = GetComponent<Image>();
146 if (image != null)
147 {
148 image.raycastTarget = condition;
149 }
150 }
151
157 {
158 return GetComponentInParent<DragAndDropCell>();
159 }
160
164 void OnDisable()
165 {
166 ResetConditions();
167 }
168}
System.Drawing.Image Image
Definition: TestScript.cs:37
Every item's cell must contain this script
Drag and Drop item.
static DragEvent OnItemDragEndEvent
static DragAndDropCell sourceCell
delegate void DragEvent(DragAndDropItem item)
void OnDrag(PointerEventData data)
Every frame on this item drag.
static DragAndDropItem draggedItem
void MakeRaycast(bool condition)
Enable item's raycast.
static GameObject icon
void OnBeginDrag(PointerEventData eventData)
This item started to drag.
static bool dragDisabled
DragAndDropCell GetCell()
Gets DaD cell which contains this item.
void OnEndDrag(PointerEventData eventData)
This item is dropped.
static DragEvent OnItemDragStartEvent