-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathReorderableListExample01.cs
146 lines (125 loc) · 4.22 KB
/
ReorderableListExample01.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
public class ReorderableListExample01 : EditorWindow {
ReorderableList rl;
List<MyClass01> someList;
Vector2 scrollPos = Vector2.zero;
[MenuItem("MyTools/ReorderableListExample01")]
static void ShowWindow()
{
EditorWindow window = EditorWindow.GetWindow<ReorderableListExample01>();
window.Show();
}
void OnEnable()
{
someList = new List<MyClass01>();
// Add some data to the list
someList.Add(new MyClass01() { Name = "John", Role = "2D Artist" });
someList.Add(new MyClass01() { Name = "Jane", Role = "Engine Developer" });
someList.Add(new MyClass01() { Name = "Max", Role = "Music Composer" });
rl = new ReorderableList(someList, typeof(MyClass01));
rl.elementHeight += 5;
// Add callback methods
rl.onAddCallback += rlAddCallback;
rl.drawElementCallback += rlDrawElementCallback;
rl.onSelectCallback += rlSelectCallback;
rl.drawHeaderCallback += rlDrawHeaderCallback;
rl.onChangedCallback += rlOnChangedCallback;
rl.onAddCallback += rlOnAddCallback;
rl.onRemoveCallback += rlOnRemoveCallback;
}
void OnDisable()
{
// Remove callback methods
rl.onAddCallback -= rlAddCallback;
rl.drawElementCallback -= rlDrawElementCallback;
rl.onSelectCallback -= rlSelectCallback;
rl.drawHeaderCallback -= rlDrawHeaderCallback;
rl.onChangedCallback -= rlOnChangedCallback;
rl.onAddCallback -= rlOnAddCallback;
rl.onRemoveCallback -= rlOnRemoveCallback;
}
void OnGUI()
{
EditorGUILayout.Space();
EditorGUILayout.LabelField("Reorderable List Example 1",
new GUIStyle() { alignment = TextAnchor.MiddleCenter, fontSize = 15, fontStyle = FontStyle.Bold });
EditorGUILayout.Space();
scrollPos = EditorGUILayout.BeginScrollView(scrollPos);
rl.DoLayoutList();
EditorGUILayout.EndScrollView();
// If you want to print out the names from the reordered list.
// This is to show that the list can be used after reodering.
if(GUILayout.Button("Print list names"))
{
string s = "";
for(int i = 0; i < rl.list.Count; i++)
{
MyClass01 mc = (MyClass01)rl.list[i];
s += i < rl.list.Count-1 ? mc.Name + ", " : mc.Name + ".";
}
Debug.Log(s);
}
}
void rlOnAddCallback(ReorderableList _rl)
{
// Uncomment the below line to display message when Item is added.
// Debug.Log("New item added");
}
void rlOnChangedCallback(ReorderableList _rl)
{
// Uncomment the below line to display message when list has changed.
// Debug.Log("List changed");
}
void rlOnRemoveCallback(ReorderableList _rl)
{
// Uncomment the below line to display message when Item is removed.
// Debug.Log("Item #" + _rl.index.ToString() + " removed.");
// This line is required because when you add a method to the OnRemoveCallback, clicking the remove
// button doesn't remove items anymore. So we have to add the functionality manually.
_rl.list.RemoveAt(_rl.index);
}
void rlDrawHeaderCallback(Rect myRect)
{
EditorGUI.LabelField(myRect, "Team Members");
}
void rlDrawElementCallback(Rect myRect, int index, bool isActive, bool isFocused)
{
EditorGUI.LabelField(new Rect(myRect.x, myRect.y + 5, 40, 40), index.ToString());
EditorGUI.LabelField(new Rect(myRect.x + 30, myRect.y + 5, 100, 40), someList[index].Name);
if(isActive)
{
someList[index].Name = EditorGUI.TextArea(new Rect(myRect.x + 30, myRect.y + 5, 100, 15),
someList[index].Name);
someList[index].Role = GUI.TextArea(new Rect(myRect.x + 150, myRect.y + 5, 400, 15),
someList[index].Role);
}
else
{
EditorGUI.LabelField(new Rect(myRect.x + 30, myRect.y + 5, 100, 15),
someList[index].Name);
EditorGUI.LabelField(new Rect(myRect.x + 150, myRect.y + 5, 400, 15),
someList[index].Role);
}
}
void rlSelectCallback(ReorderableList _rl)
{
// Uncomment the below line to display message when Item is selected.
// Debug.Log("Item #" + _rl.index.ToString() + " selected.");
}
void rlAddCallback(ReorderableList _rl)
{
_rl.list.Add(new MyClass01() { IndexNo = _rl.count, Name = "SomeName", Role = "SomeRole" });
_rl.index = _rl.list.Count-1;
}
}
[System.Serializable]
public class MyClass01
{
public int IndexNo = 1;
public string Name = "";
public string Role = "";
}