Tanoda
ReorderableList.cs
Go to the documentation of this file.
1
3
4using System;
6
8{
9
10 [RequireComponent(typeof(RectTransform)), DisallowMultipleComponent]
11 [AddComponentMenu("UI/Extensions/Re-orderable list")]
12 public class ReorderableList : MonoBehaviour
13 {
14 [Tooltip("Child container with re-orderable items in a layout group")]
15 public LayoutGroup ContentLayout;
16 [Tooltip("Parent area to draw the dragged element on top of containers. Defaults to the root Canvas")]
17 public RectTransform DraggableArea;
18
19 [Tooltip("Can items be dragged from the container?")]
20 public bool IsDraggable = true;
21
22 [Tooltip("Should the draggable components be removed or cloned?")]
23 public bool CloneDraggedObject = false;
24
25 [Tooltip("Can new draggable items be dropped in to the container?")]
26 public bool IsDropable = true;
27
28 [Tooltip("Should dropped items displace a current item if the list is full?\n " +
29 "Depending on the dropped items origin list, the displaced item may be added, dropped in space or deleted.")]
30 public bool IsDisplacable = false;
31
32 // This sets every item size (when being dragged over this list) to the current size of the first element of this list
33 [Tooltip("Should items being dragged over this list have their sizes equalized?")]
34 public bool EqualizeSizesOnDrag = false;
35
36 public int maxItems = int.MaxValue;
37
38
39 [Header("UI Re-orderable Events")]
49
50 private RectTransform _content;
51 private ReorderableListContent _listContent;
52
53 public RectTransform Content
54 {
55 get
56 {
57 if (_content == null)
58 {
59 _content = ContentLayout.GetComponent<RectTransform>();
60 }
61 return _content;
62 }
63 }
64
65 Canvas GetCanvas()
66 {
67 Transform t = transform;
68 Canvas canvas = null;
69
70
71 int lvlLimit = 100;
72 int lvl = 0;
73
74 while (canvas == null && lvl < lvlLimit)
75 {
76 canvas = t.gameObject.GetComponent<Canvas>();
77 if (canvas == null)
78 {
79 t = t.parent;
80 }
81
82 lvl++;
83 }
84 return canvas;
85 }
86
90 public void Refresh()
91 {
92 Destroy(_listContent);
93 _listContent = ContentLayout.gameObject.AddComponent<ReorderableListContent>();
94 _listContent.Init(this);
95 }
96
97 private void Start()
98 {
99
100 if (ContentLayout == null)
101 {
102 Debug.LogError("You need to have a child LayoutGroup content set for the list: " + name, gameObject);
103 return;
104 }
105 if (DraggableArea == null)
106 {
107 DraggableArea = transform.root.GetComponentInChildren<Canvas>().GetComponent<RectTransform>();
108 }
109 if (IsDropable && !GetComponent<Graphic>())
110 {
111 Debug.LogError("You need to have a Graphic control (such as an Image) for the list [" + name + "] to be droppable", gameObject);
112 return;
113 }
114
115 Refresh();
116 }
117
118
119 #region Nested type: ReorderableListEventStruct
120
121 [Serializable]
123 {
124 public GameObject DroppedObject;
125 public int FromIndex;
127 public bool IsAClone;
128 public GameObject SourceObject;
129 public int ToIndex;
131
132 public void Cancel()
133 {
134 SourceObject.GetComponent<ReorderableListElement>().isValid = false;
135 }
136 }
137
138 #endregion
139
140
141 #region Nested type: ReorderableListHandler
142
143 [Serializable]
144 public class ReorderableListHandler : UnityEvent<ReorderableListEventStruct>
145 {
146 }
147
149 {
150 Debug.Log("Event Received");
151 Debug.Log("Hello World, is my item a clone? [" + item.IsAClone + "]");
152 }
153
154 #endregion
155 }
156}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
void TestReOrderableListTarget(ReorderableListEventStruct item)
void Refresh()
Refresh related list content
ReorderableListHandler OnElementDisplacedFromReturned
ReorderableListHandler OnElementDisplacedFrom
ReorderableListHandler OnElementDisplacedToReturned
ReorderableListHandler OnElementDroppedWithMaxItems
Credit Erdener Gonenc - @PixelEnvision.