-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMetadataEditorInstance.cs
More file actions
343 lines (299 loc) · 10.8 KB
/
MetadataEditorInstance.cs
File metadata and controls
343 lines (299 loc) · 10.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.Pool;
using System.Collections.Generic;
/// <summary>
/// An Editor for a metadata instance that's currently shown in the inspector
/// Holds a "metadata" for each selected "target" in the inspector
/// </summary>
sealed class MetadataEditorInstance : IDisposable
{
string metadataKeyName;
GUIContent metadataContent;
UnityEngine.Object[] targets;
bool showMetadata;
CustomAssetMetadata[] metadata;
Editor metaDataEditor;
bool isGenericInspector;
SerializedObject serializedObject;
private MetadataEditorInstance() { }
public static MetadataEditorInstance Create(UnityEngine.Object[] targets, CustomAssetMetadata[] metadata, Type type)
{
var metadataName = AssetMetadataUtility.GetDisplayName(type);
var metadataKeyName = type.FullName;
return new MetadataEditorInstance
{
targets = targets,
metadata = metadata,
metadataKeyName = metadataKeyName,
metadataContent = new GUIContent(metadataName),
showMetadata = EditorPrefs.GetBool(metadataKeyName, false)
};
}
public void Dispose()
{
EditorPrefs.SetBool(metadataKeyName, showMetadata);
metadata = null;
if (metaDataEditor != null)
UnityEngine.Object.DestroyImmediate(metaDataEditor);
metaDataEditor = null;
}
public bool Destroyed { get { return metadata == null; } }
public bool MixedValues
{
get
{
if (metadata == null ||
metadata.Length != targets.Length)
return true;
if (metadata.Length == 0)
return false;
foreach (var item in metadata)
{
if (item == null)
return true;
}
return false;
}
}
void ShowMetadataEditor()
{
if (metaDataEditor == null)
{
metaDataEditor = Editor.CreateEditor(metadata);
isGenericInspector = metaDataEditor.GetType().FullName == "UnityEditor.GenericInspector";
if (isGenericInspector)
{
serializedObject = new SerializedObject(metadata);
}
}
if (metaDataEditor != null)
{
if (isGenericInspector)
{
serializedObject.UpdateIfRequiredOrScript();
SerializedProperty iterator = serializedObject.GetIterator();
bool enterChildren = true;
while (iterator.NextVisible(enterChildren))
{
if (iterator.propertyPath == "m_Script")
continue;
EditorGUILayout.PropertyField(iterator, true);
enterChildren = false;
}
serializedObject.ApplyModifiedProperties();
} else
metaDataEditor.OnInspectorGUI();
}
}
public void OnInspectorGUI()
{
if (targets == null || targets.Length == 0 || metadata == null || metadata.Length == 0)
return;
EditorGUI.indentLevel = 0;
EditorGUIUtility.labelWidth = 0;
var rect = GUILayoutUtility.GetRect(GUIContent.none, InspectorLayout.InspectorTitlebar);
showMetadata = InspectorLayout.FoldoutTitlebar(rect, showMetadata, metadataContent, false, InspectorLayout.InspectorTitlebar, InspectorLayout.InspectorTitlebarText);
var controlId = InspectorLayout.LastControlId;
if (showMetadata)
{
if (MixedValues)
{
EditorGUI.showMixedValue = true;
EditorGUILayout.LabelField("\u2026");
EditorGUI.showMixedValue = false;
} else
{
EditorGUILayout.Space(3);
ShowMetadataEditor();
EditorGUILayout.Space();
}
}
var evt = Event.current;
var type = evt.GetTypeForControl(controlId);
switch (type)
{
case EventType.MouseUp:
{
if (evt.button == 1 && rect.Contains(evt.mousePosition))
{
var menu = new GenericMenu();
menu.AddItem(new GUIContent("Remove"), false, DestroyMetadata);
menu.ShowAsContext();
}
break;
}
}
}
void DestroyMetadata()
{
if (metadata != null)
{
for (int i = metadata.Length - 1; i >= 0; i--)
{
AssetMetadataUtility.Destroy(metadata[i]);
}
}
metadata = null;
}
}
public sealed class MetadataEditor : IDisposable
{
readonly bool canOpenForEdit;
readonly UnityEngine.Object[] targets;
MetadataEditorInstance[] metadataEditors;
public MetadataEditor(UnityEngine.Object[] targets)
{
this.targets = targets;
canOpenForEdit = true;
foreach (var target in targets)
{
if (!AssetDatabase.CanOpenForEdit(target))
canOpenForEdit = false;
}
}
public void Dispose()
{
DestroyEditors(ref metadataEditors);
}
static void DestroyEditors(ref MetadataEditorInstance[] metadataEditors)
{
if (metadataEditors != null)
{
for (int i = 0; i < metadataEditors.Length; i++)
{
metadataEditors[i].Dispose();
}
metadataEditors = null;
}
}
public bool CanAddMetadataType(Type type)
{
if (type == null || targets == null || targets.Length == 0)
return false;
for (int i = 0; i < targets.Length; i++)
{
if (!AssetMetadataUtility.CanAddMetadataType(targets[i], type))
return false;
}
return true;
}
public void AddMetadata(Type type)
{
if (type == null || targets == null || targets.Length == 0)
return;
for (int i = 0; i < targets.Length; i++)
{
AssetMetadataUtility.Add(targets[i], type);
}
}
static MetadataEditorInstance[] CreateEditors(UnityEngine.Object[] targets)
{
if (targets == null)
{
Debug.Log("targets == null");
return null;
}
var types = ListPool<Type>.Get();
var typeMetadata = ListPool<List<CustomAssetMetadata>>.Get();
var temporaryMetadata = ListPool<CustomAssetMetadata>.Get();
AssetMetadataUtility.GetAll(targets[0], temporaryMetadata);
for (int i = 0; i < temporaryMetadata.Count; i++)
{
var metadataItem = temporaryMetadata[i];
if (metadataItem == null)
continue;
types.Add(metadataItem.GetType());
typeMetadata.Add(new List<CustomAssetMetadata>
{
metadataItem
});
}
for (int i = 1; i < targets.Length; i++)
{
AssetMetadataUtility.GetAll(targets[i], temporaryMetadata);
// TODO: do something clever to support metadata w/ multiple targets
}
var metadataEditors = new MetadataEditorInstance[typeMetadata.Count];
for (int i = 0; i < typeMetadata.Count; i++)
{
metadataEditors[i] = MetadataEditorInstance.Create(targets, typeMetadata[i].ToArray(), types[i]);
}
ListPool<Type>.Release(types);
ListPool<List<CustomAssetMetadata>>.Release(typeMetadata);
ListPool<CustomAssetMetadata>.Release(temporaryMetadata);
return metadataEditors;
}
private static GUIContent addMetadataButton = new GUIContent("+", "Add Metadata");
public void OnInspectorGUI()
{
using (new EditorGUI.DisabledScope(!canOpenForEdit))
{
if (targets == null || !AssetMetadataUtility.HaveMetadataTypes)
return;
// TODO: support multiple targets
if (targets.Length > 1)
return;
metadataEditors ??= CreateEditors(targets);
if (metadataEditors == null)
return;
// TODO: make it possible to re-order metadata like Components on GameObjects
GUILayout.BeginVertical();
List<Type> filteredMetadata = ListPool<Type>.Get();
try
{
var canAddMetadata = AssetMetadataUtility.HaveMetadataTypes;
if (canAddMetadata)
{
foreach (var type in AssetMetadataUtility.AllMetadataTypes)
{
if (!CanAddMetadataType(type))
continue;
filteredMetadata.Add(type);
}
canAddMetadata = filteredMetadata.Count > 0;
}
using (new EditorGUI.DisabledScope(!canAddMetadata))
{
// Embed the button inside the previous rect
var rect = GUILayoutUtility.GetRect(0,0,0,0);
rect.width = rect.height = EditorGUIUtility.singleLineHeight;
rect.y -= rect.height;
if (GUI.Button(rect, addMetadataButton))
{
// TODO: have a nicer dropdownmenu, more like the "add components" menu
var menu = new GenericMenu();
foreach (var type in filteredMetadata)
{
if (!CanAddMetadataType(type))
continue;
var menuName = AssetMetadataUtility.GetMenuName(type);
menu.AddItem(new GUIContent(menuName), false, delegate ()
{
AddMetadata(type);
DestroyEditors(ref metadataEditors);
metadataEditors = CreateEditors(targets);
});
}
menu.ShowAsContext();
}
}
}
finally
{
ListPool<Type>.Release(filteredMetadata);
}
if (metadataEditors.Length > 0)
{
// We seem to get the metadata in reverse order from the assetdatabase, compared to the order we add them
// So to make things feel more consistent, we reverse the order
for (int i = metadataEditors.Length - 1; i >= 0; i--)
{
metadataEditors[i].OnInspectorGUI();
}
}
GUILayout.EndVertical();
}
}
}