-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGalaxyPrefabEditor.cs
434 lines (387 loc) · 16.1 KB
/
GalaxyPrefabEditor.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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//#define Debug
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using Galaxia;
using System.Reflection;
namespace GalaxyGeneratorEditor
{
[CustomEditor(typeof(GalaxyPrefab))]
[CanEditMultipleObjects]
public sealed class GalaxyPrefabEditor : Editor
{
private const string DISTRIBUTOR = "m_distributor";
private const string ACTIVE = "m_active";
private const string PARTICLE_PREFABS = "m_particlePrefabs";
private GUIStyle lightBgStyle;
private GUIStyle darkBackgroundStyle;
private GUIStyle moduleBgStyle;
private GUIStyle titleStyle;
private GUIStyle buttonLeftStyle;
private GUIStyle buttonRightStyle;
private GUIStyle buttonMidStyle;
List<ParticlesPrefabEditor> Editors;
Editor ParticlesDistributionEditor;
void OnEnable()
{
RefreshParticlesPrefabEditors();
}
internal void RefreshParticlesPrefabEditors()
{
GalaxyPrefab prefab = target as GalaxyPrefab;
Editors = new List<ParticlesPrefabEditor>();
foreach (ParticlesPrefab p in prefab)
{
Editors.Add(CreateEditor(p) as ParticlesPrefabEditor);
}
//if (!EditorApplication.isPlayingOrWillChangePlaymode)
//{
// RecreateAllGalaxies();
//}
}
internal void RefreshParticlesDistributionEditor()
{
serializedObject.Update();
if (serializedObject.FindProperty(DISTRIBUTOR).objectReferenceValue != null)
{
ParticlesDistributionEditor = CreateEditor(serializedObject.FindProperty(DISTRIBUTOR).objectReferenceValue);
}
else
{
ParticlesDistributionEditor = null;
}
RecreateAllGalaxies();
}
internal void UpdateDistributorEditor()
{
if (ParticlesDistributionEditor == null)
{
RefreshParticlesDistributionEditor();
return;
}
if (ParticlesDistributionEditor != null && ParticlesDistributionEditor.target != serializedObject.FindProperty(DISTRIBUTOR).objectReferenceValue)
{
RefreshParticlesDistributionEditor();
}
}
public override void OnInspectorGUI()
{
OnGUI();
}
private void CheckStyles()
{
if (darkBackgroundStyle == null)
{
lightBgStyle = "PreferencesSectionBox";
darkBackgroundStyle = "CurveEditorBackground";
moduleBgStyle = "ShurikenModuleBg";
titleStyle = "ShurikenEmitterTitle";
buttonLeftStyle = "ButtonLeft";
buttonMidStyle = "ButtonMid";
buttonRightStyle = "ButtonRight";
}
}
internal bool OnGUI()
{
CheckStyles();
serializedObject.UpdateIfRequiredOrScript();
SerializedProperty particlePrefabs = serializedObject.FindProperty(PARTICLE_PREFABS);
SerializedProperty distributor = serializedObject.FindProperty(DISTRIBUTOR);
serializedObject.targetObject.name = EditorGUILayout.TextField("Name",serializedObject.targetObject.name);
if (DrawDefaultInspector())
{
UpdateAllGalaxies();
}
EditorGUILayout.BeginVertical(lightBgStyle);
EditorGUILayout.Space();
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PrefixLabel(new GUIContent("Type", distributor.tooltip));
if (GUILayout.Button(distributor.objectReferenceValue != null ? distributor.objectReferenceValue.name : "Distribution", EditorStyles.popup))
{
NewDistributorMenu();
}
EditorGUILayout.EndHorizontal();
if (distributor.objectReferenceValue != null)
{
/*SerializedObject Distributor = new SerializedObject(distributor.objectReferenceValue);
SerializedProperty prop = Distributor.GetIterator();
prop.NextVisible(true);
while (prop.NextVisible(false))
{
EditorGUILayout.PropertyField(prop, true);
}*/
EditorGUI.BeginChangeCheck();
UpdateDistributorEditor();
ParticlesDistributionEditor.OnInspectorGUI();
if (EditorGUI.EndChangeCheck())
{
UpdateAllGalaxies();
}
/*if (Distributor.ApplyModifiedProperties())
{
UpdateAllGalaxies();
}*/
}
#if Debug
if (GUILayout.Button("Change Flags"))
{
foreach(ParticlesPrefab p in target as GalaxyPrefab)
{
p.hideFlags = HideFlags.HideInHierarchy;
}
(target as GalaxyPrefab).Distributor.hideFlags = HideFlags.HideInHierarchy;
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
#endif
if(GUILayout.Button("Recreate curves"))
{
(distributor.objectReferenceValue as ParticleDistributor).RecreateCurves();
}
if (serializedObject.ApplyModifiedProperties())
{
UpdateAllGalaxies();
}
EditorGUILayout.Space();
EditorGUILayout.EndVertical();
EditorGUILayout.Space();
EditorGUILayout.BeginVertical(darkBackgroundStyle);
serializedObject.Update();
EditorGUILayout.Space();
EditorGUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
if (GUILayout.Button("Add", buttonLeftStyle))
{
//PopupWindow.Show(new Rect(Event.current.mousePosition.x, Event.current.mousePosition.y, 0, 0), popup);
PopupWindow.Show(new Rect(Event.current.mousePosition.x, Event.current.mousePosition.y, 0, 0), new PopupWindows.CreateNewParticlesWindow(this));
}
if (GUILayout.Button("Update", buttonMidStyle))
{
foreach(Galaxy g in GameObject.FindObjectsOfType<Galaxy>())
{
if (g.GalaxyPrefab == target as GalaxyPrefab)
{
g.UpdateParticles();
}
}
}
if (GUILayout.Button("ReCreate", buttonMidStyle))
{
RecreateAllGalaxies();
}
if (GUILayout.Button("Clear", buttonRightStyle))
{
if (EditorUtility.DisplayDialog("Clear All Particle Prefabs", "Are you sure you want to delete all Particle Prefabs from the galaxy?", "Yes", "Cancel"))
{
foreach (Object asset in AssetDatabase.LoadAllAssetsAtPath(AssetDatabase.GetAssetPath(target)))
{
if (asset is ParticlesPrefab)
{
DestoryParticlesPrefab(asset as ParticlesPrefab);
}
}
}
}
GUILayout.FlexibleSpace();
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space();
EditorGUILayout.BeginHorizontal();
EditorGUILayout.Space();
EditorGUILayout.BeginVertical();
for (int i = 0; i < particlePrefabs.arraySize; i++)
{
if (particlePrefabs.GetArrayElementAtIndex(i).objectReferenceValue == null)
{
particlePrefabs.DeleteArrayElementAtIndex(i);
break;
}
else if (Editors.Count > i && Editors[i].target != null)
{
Editors[i].serializedObject.Update();
//serializedObject.FindProperty("ParticlePrefabs").GetArrayElementAtIndex(i).isExpanded = EditorGUILayout.InspectorTitlebar(serializedObject.FindProperty("ParticlePrefabs").GetArrayElementAtIndex(i).isExpanded, Editors[i].target);
GUIStyle s = new GUIStyle(titleStyle);
s.padding.left += 40;
s.padding.right = 0;
EditorGUILayout.BeginHorizontal(s, GUILayout.MinHeight(28));
particlePrefabs.GetArrayElementAtIndex(i).isExpanded = EditorGUILayout.Foldout(particlePrefabs.GetArrayElementAtIndex(i).isExpanded, particlePrefabs.GetArrayElementAtIndex(i).objectReferenceValue.name);
s = new GUIStyle((GUIStyle)"Button");
s.normal.background = null;
s.active.background = null;
s.margin.left = s.margin.right = 0;
s.padding = new RectOffset(0, 0, 0, 0);
s.fixedWidth = 24;
GUILayout.Button(EditorGUIUtility.IconContent("_Help"), s);
if (GUILayout.Button(EditorGUIUtility.IconContent("d__Popup"), s))
ParticlesPrefabContextMenu(Editors[i].target as ParticlesPrefab).ShowAsContext();
s = new GUIStyle((GUIStyle)"OL Toggle");
s.focused.background = null;
Editors[i].serializedObject.FindProperty(ACTIVE).boolValue = EditorGUILayout.Toggle(Editors[i].serializedObject.FindProperty(ACTIVE).boolValue, s, GUILayout.Width(28));
if(Editors[i].serializedObject.ApplyModifiedProperties())
{
UpdateAllGalaxies(Editors[i].target as ParticlesPrefab);
}
EditorGUILayout.EndHorizontal();
if (particlePrefabs.GetArrayElementAtIndex(i).isExpanded)
{
GUI.enabled = Editors[i].serializedObject.FindProperty(ACTIVE).boolValue;
EditorGUILayout.BeginVertical(moduleBgStyle);
EditorGUILayout.Space();
if (Editors[i].OnGUI() || Event.current.commandName == "UndoRedoPerformed" && Event.current.type == EventType.ExecuteCommand)
{
UpdateAllGalaxies(Editors[i].target as ParticlesPrefab);
}
EditorGUILayout.Space();
EditorGUILayout.EndVertical();
EditorGUILayout.Space();
GUI.enabled = true;
}
if(Editors[i].serializedObject.ApplyModifiedProperties())
{
UpdateAllGalaxies(Editors[i].target as ParticlesPrefab);
}
}
}
EditorGUILayout.EndVertical();
EditorGUILayout.Space();
EditorGUILayout.EndHorizontal();
if(serializedObject.ApplyModifiedProperties())
{
UpdateAllGalaxies();
}
GUILayout.FlexibleSpace();
EditorGUILayout.EndVertical();
return false;
}
void NewDistributorMenu()
{
GenericMenu menu = new GenericMenu();
foreach(Assembly ass in System.AppDomain.CurrentDomain.GetAssemblies())
{
foreach(System.Type type in ass.GetTypes())
{
if (type.IsSubclassOf(typeof(ParticleDistributor)))
{
menu.AddItem(new GUIContent(type.Name), false, ChangeDistribution, type);
}
}
}
menu.ShowAsContext();
}
void ChangeDistribution(object typeObj)
{
System.Type type = typeObj as System.Type;
GalaxyPrefab galaxy = target as GalaxyPrefab;
if (galaxy.Distributor != null)
{
DestroyImmediate(galaxy.Distributor,true);
}
galaxy.Distributor = ScriptableObject.CreateInstance(type) as ParticleDistributor;
galaxy.Distributor.name = type.Name;
AssetDatabase.AddObjectToAsset(galaxy.Distributor,galaxy);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
RefreshParticlesDistributionEditor();
}
void UpdateAllGalaxies()
{
GalaxyPrefab galaxyPrefab = target as GalaxyPrefab;
if (EditorApplication.isPlayingOrWillChangePlaymode)
{
galaxyPrefab.UpdateAllGalaxies();
}
else
{
foreach (Galaxy galaxy in GameObject.FindObjectsOfType<Galaxy>())
{
if (galaxy.GalaxyPrefab == target as GalaxyPrefab && galaxy.GenerationType == Galaxy.GalaxyGenerationType.Automatic)
{
galaxy.UpdateParticlesImmediately();
}
}
}
}
void UpdateAllGalaxies(ParticlesPrefab prefab)
{
GalaxyPrefab galaxyPrefab = target as GalaxyPrefab;
if (EditorApplication.isPlayingOrWillChangePlaymode)
{
galaxyPrefab.UpdateAllGalaxies(prefab);
}
else
{
foreach (Galaxy galaxy in GameObject.FindObjectsOfType<Galaxy>())
{
if (galaxy.GalaxyPrefab == target as GalaxyPrefab && galaxy.GenerationType == Galaxy.GalaxyGenerationType.Automatic)
{
galaxy.UpdateParticlesImmediately();
}
}
}
}
void RecreateAllGalaxies()
{
GalaxyPrefab galaxyPrefab = target as GalaxyPrefab;
foreach (Galaxy galaxy in GameObject.FindObjectsOfType<Galaxy>())
{
if (galaxy.GalaxyPrefab == target as GalaxyPrefab)
{
galaxy.UpdateParticlesImmediately();
}
}
}
[MenuItem("Assets/Create/Galaxy Prefab")]
public static void CreateGalaxyPrefab()
{
GalaxyPrefab prefab = ScriptableObjectUtility.CreateAsset<GalaxyPrefab>();
prefab.Distributor = ScriptableObject.CreateInstance<DensityWaveDistributor>();
prefab.Distributor.name = "Density Wave Distribution";
AssetDatabase.AddObjectToAsset(prefab.Distributor,prefab);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
internal void CreateParticlePrefab(string name,ParticlesPrefab.Preset Preset)
{
GalaxyPrefab galaxyPrefab = target as GalaxyPrefab;
ParticlesPrefab prefab = galaxyPrefab.Create(name, Preset);
UnityEditor.AssetDatabase.AddObjectToAsset(prefab, galaxyPrefab);
UnityEditor.AssetDatabase.SaveAssets();
UnityEditor.AssetDatabase.Refresh();
RefreshParticlesPrefabEditors();
}
GenericMenu ParticlesPrefabContextMenu(ParticlesPrefab prefab)
{
GenericMenu menu = new GenericMenu();
menu.AddItem(new GUIContent("Delete"), false, DestoryParticlesPrefab, prefab);
menu.AddItem(new GUIContent("Duplicate"), false, Dublicate, prefab);
menu.AddItem(new GUIContent("Reset"), false, Reset, prefab);
return menu;
}
void DestoryParticlesPrefab(object prefabObj)
{
GalaxyPrefab galaxy = target as GalaxyPrefab;
ParticlesPrefab prefab = prefabObj as ParticlesPrefab;
galaxy.Remove(prefab);
prefab.DestoryPrefab();
AssetDatabase.Refresh();
AssetDatabase.SaveAssets();
RefreshParticlesPrefabEditors();
}
void Dublicate(object prefab)
{
GalaxyPrefab galaxy = target as GalaxyPrefab;
ParticlesPrefab newPrefab = Instantiate(prefab as ParticlesPrefab) as ParticlesPrefab;
newPrefab.hideFlags = HideFlags.HideInHierarchy;
galaxy.Insert(prefab as ParticlesPrefab, newPrefab);
AssetDatabase.AddObjectToAsset(newPrefab, galaxy);
AssetDatabase.Refresh();
AssetDatabase.SaveAssets();
RefreshParticlesPrefabEditors();
}
void Reset(object prefabObj)
{
ParticlesPrefab prefab = prefabObj as ParticlesPrefab;
GalaxyPrefab galaxy = target as GalaxyPrefab;
galaxy.PopulatePreset(prefab, prefab.OriginalPreset);
}
}
}