-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEditorGUIHelper.cs
More file actions
272 lines (232 loc) · 12.8 KB
/
Copy pathEditorGUIHelper.cs
File metadata and controls
272 lines (232 loc) · 12.8 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Linq;
using System.Reflection;
using UnityEngine.SceneManagement;
namespace ActionGraph
{
public static class EditorGUIHelper
{
// -------------------------------------------------------------------------------
public static object ShowInspectorForType(System.Type type, object value, string name)
{
if (type == typeof(bool))
{
return EditorGUILayout.Toggle(name, (bool)value);
}
else if (type == typeof(string))
{
return EditorGUILayout.TextField(name, (string)value);
}
else if (type == typeof(int))
{
return EditorGUILayout.IntField(name, (int)value);
}
else if (type == typeof(float))
{
return EditorGUILayout.FloatField(name, (float)value);
}
else if (type == typeof(Vector2))
{
return EditorGUILayout.Vector2Field(name, (Vector2)value);
}
else if (type == typeof(Vector3))
{
return EditorGUILayout.Vector3Field(name, (Vector3)value);
}
else if (typeof(Enum).IsAssignableFrom(type))
{
return EditorGUILayout.EnumPopup(name, (Enum)value);
}
else if (typeof(IList<>).IsAssignableFrom(type))
{
EditorGUILayout.LabelField("TODO: LIST SUPPORT");
}
else if (typeof(UnityEngine.Object).IsAssignableFrom(type))
{
return EditorGUILayout.ObjectField(name, (UnityEngine.Object)value, type,
typeof(Component).IsAssignableFrom(type) || type == typeof(GameObject) || type == typeof(UnityEngine.Object));
}
else if (type.IsGenericType && typeof(Variable<>).IsAssignableFrom(type.GetGenericTypeDefinition()))
{
EditorGUILayout.BeginHorizontal();
if (value == null)
{
value = System.Activator.CreateInstance(type);
}
var variableBoundMemberNameField = GetPrivateFieldInfo(value, "mBoundMemberName");
var variableBoundMemberName = (string)variableBoundMemberNameField.GetValue(value);
bool isBound = (variableBoundMemberName != null);
if (!isBound)
{
var variableValueField = GetPrivateFieldInfo(value, "mValue");
var variableValue = variableValueField.GetValue(value);
var variableValuetype = variableValueField.FieldType;
variableValueField.SetValue(value, ShowInspectorForType(variableValuetype, variableValue, name));
}
else
{
var variableBoundOwnerField = GetPrivateFieldInfo(value, "mBoundOwner");
var variableBoundOwnerValue = (MonoBehaviour)variableBoundOwnerField.GetValue(value);
if (variableBoundOwnerValue != null)
{
string path = GetComponentPath(variableBoundOwnerValue);
EditorGUILayout.LabelField(name + ": " + path + "/" + variableBoundMemberName);
}
else
{
Debug.LogWarning("Bound value couldn't be found, the object may have been deleted? Unbinding...");
UnbindVariable(value);
}
}
if (GUILayout.Button("@", GUILayout.Width(20), GUILayout.Height(16)))
{
var menu = new GenericMenu();
if (isBound)
{
menu.AddItem(new GUIContent("Unbind"), false, () => { UnbindVariable(value); });
}
else
{
// Grab a list of all the GameObjects at the root of the scene
List<GameObject> sceneGameObjects = new List<GameObject>();
SceneManager.GetActiveScene().GetRootGameObjects(sceneGameObjects);
var genericVariableType = type.GetGenericArguments()[0];
// Iterate over all the GameObjects at the root of the scene
foreach (var gameObject in sceneGameObjects)
{
// For each scene object, iterate over all components (and children's components)
// and look for any properties or fields that match our Variable's generic type
foreach (var component in gameObject.GetComponentsInChildren<Component>())
{
// GameObjects are special type because we can get at them from components..
var showComponentsForGameObjects = (genericVariableType == typeof(GameObject));
foreach (PropertyInfo componentPropertyInfo in component.GetType().GetProperties())
{
var propertyType = componentPropertyInfo.PropertyType;
// Do we actually want to bind to the gameObject property on a component, rather than the component itself?
var bindGameObjectFromMonoBehaviour = (showComponentsForGameObjects && typeof(MonoBehaviour).IsAssignableFrom(propertyType));
if (bindGameObjectFromMonoBehaviour || propertyType == genericVariableType || genericVariableType.IsAssignableFrom(propertyType))
{
var menuPath = "Properties/" + GetComponentPath(component) + "/" + componentPropertyInfo.Name;
menu.AddItem(new GUIContent(menuPath), false, () =>
{
BindVariable(value, component, componentPropertyInfo);
});
}
}
foreach (FieldInfo componentFieldInfo in component.GetType().GetFields())
{
var fieldType = componentFieldInfo.FieldType;
var bindGameObjectFromComponent = (showComponentsForGameObjects && typeof(MonoBehaviour).IsAssignableFrom(fieldType));
if (bindGameObjectFromComponent || fieldType == genericVariableType || genericVariableType.IsAssignableFrom(fieldType))
{
var menuPath = "Fields/" + GetComponentPath(component) + "/" + componentFieldInfo.Name;
menu.AddItem(new GUIContent(menuPath), false, () =>
{
MemberInfo memberToBind = componentFieldInfo;
// Do we actually want to bind to the gameObject property on a component, rather than the component itself?
if (bindGameObjectFromComponent)
{
memberToBind = component.GetType().GetProperty("gameObject");
}
BindVariable(value, component, memberToBind);
});
}
}
}
}
}
menu.ShowAsContext();
}
EditorGUILayout.EndHorizontal();
}
else
{
EditorGUILayout.LabelField("Couldn't display inspector for '" + name + "' of type: " + type.ToString());
}
return value;
}
// -------------------------------------------------------------------------------
public static string GetComponentPath(Component component)
{
Transform transform = component.transform;
string path = transform.name;
while (transform.parent != null)
{
transform = transform.parent;
path = transform.name + "/" + path;
}
return path;
}
// -------------------------------------------------------------------------------
public static FieldInfo GetPrivateFieldInfo(object owner, string fieldName)
{
return owner.GetType().GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
}
// -------------------------------------------------------------------------------
public static void CreateConditionButton(Action<Condition> callback, string buttonText = "Add Condition")
{
Condition newCondition = null;
if (GUILayout.Button(buttonText))
{
// Get a list of all possible Conditions via reflection
Type baseConditionType = typeof(Condition);
Assembly assembly = Assembly.GetAssembly(baseConditionType);
List<Type> allConditionTypes = assembly.GetTypes().Where(type => type != baseConditionType && baseConditionType.IsAssignableFrom(type)).ToList();
// Create a menu item for each type so we can create new instances from the GUI
var menu = new GenericMenu();
foreach (var conditionType in allConditionTypes)
{
menu.AddItem(new GUIContent(conditionType.Name), false, () =>
{
newCondition = (Activator.CreateInstance(conditionType) as Condition);
callback(newCondition as Condition);
});
}
menu.ShowAsContext();
}
}
// -------------------------------------------------------------------------------
public static void ShowConditionInspector(Condition condition)
{
// Show inspector GUI for all public fields on the Condition
foreach (FieldInfo fieldInfo in condition.GetType().GetFields())
{
if (fieldInfo != null && !fieldInfo.IsStatic)
{
var value = fieldInfo.GetValue(condition);
value = ShowInspectorForType(fieldInfo.FieldType, value, fieldInfo.Name);
fieldInfo.SetValue(condition, value);
}
}
}
// -------------------------------------------------------------------------------
private static void BindVariable(object variableObject, Component owner, MemberInfo memberInfo)
{
var variableBoundMemberNameField = GetPrivateFieldInfo(variableObject, "mBoundMemberName");
var variableBoundOwnerField = GetPrivateFieldInfo(variableObject, "mBoundOwner");
var variableBoundOwnerTypeField = GetPrivateFieldInfo(variableObject, "mBoundOwnerType");
var variableValueField = GetPrivateFieldInfo(variableObject, "mValue");
variableBoundMemberNameField.SetValue(variableObject, memberInfo.Name);
variableBoundOwnerField.SetValue(variableObject, owner);
variableBoundOwnerTypeField.SetValue(variableObject, owner.GetType());
variableValueField.SetValue(variableObject, null);
}
// -------------------------------------------------------------------------------
private static void UnbindVariable(object variableObject)
{
var variableBoundMemberNameField = GetPrivateFieldInfo(variableObject, "mBoundMemberName");
var variableBoundOwnerField = GetPrivateFieldInfo(variableObject, "mBoundOwner");
var variableBoundOwnerTypeField = GetPrivateFieldInfo(variableObject, "mBoundOwnerType");
variableBoundMemberNameField.SetValue(variableObject, null);
variableBoundOwnerField.SetValue(variableObject, null);
variableBoundOwnerTypeField.SetValue(variableObject, null);
}
// -------------------------------------------------------------------------------
}
}
#endif