-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathSubModuleUI.cs
More file actions
369 lines (317 loc) · 16.5 KB
/
SubModuleUI.cs
File metadata and controls
369 lines (317 loc) · 16.5 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
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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using UnityEngine;
using System.Collections.Generic;
using UnityEditorInternal;
using Object = UnityEngine.Object;
namespace UnityEditor
{
class SubModuleUI : ModuleUI
{
SerializedProperty m_SubEmitters;
private int m_CheckObjectIndex = -1;
private Object m_PreviousSubEmitter;
// Keep in sync with enum in ParticleSystemCommmon.h
public enum SubEmitterType
{
None = -1,
Birth = 0,
Collision,
Death,
Trigger,
Manual,
TypesMax,
}
class Texts
{
public GUIContent create = EditorGUIUtility.TrTextContent("", "Create and assign a Particle System as sub-emitter.");
public GUIContent inherit = EditorGUIUtility.TrTextContent("Inherit", "Determines what properties to inherit from the Parent System.");
public GUIContent emitProbability = EditorGUIUtility.TrTextContent("Emit Probability", "Determines the proportion of sub-emitter spawn events that successfully triggers the associated sub-emitter.");
public GUIContent invalidSubEmitterParenting = EditorGUIUtility.TrTextContent("The following sub-emitters are not descendents of their owning Particle System. Reparent the sub-emitters so that they are children of their owning system:");
public GUIContent[] subEmitterTypes = new GUIContent[]
{
EditorGUIUtility.TrTextContent("Birth"),
EditorGUIUtility.TrTextContent("Collision"),
EditorGUIUtility.TrTextContent("Death"),
EditorGUIUtility.TrTextContent("Trigger"),
EditorGUIUtility.TrTextContent("Manual")
};
// Keep in sync with SubModule::InheritedProperties
public string[] propertyTypes =
{
"Color",
"Size",
"Rotation",
"Lifetime",
"Duration"
};
}
private static Texts s_Texts;
ReorderableList m_EmittersList;
static readonly float kSubEmitterPadding = EditorGUIUtility.standardVerticalSpacing * 2;
public SubModuleUI(ParticleSystemUI owner, SerializedObject o, string displayName)
: base(owner, o, "SubModule", displayName)
{
m_ToolTip = L10n.Tr("Sub emission of particles. This allows each particle to emit particles in another system.");
Init(); // Init when created because we need to query if we have any subemitters referenced
}
protected override void Init()
{
// Already initialized?
if (m_SubEmitters != null)
return;
if (s_Texts == null)
s_Texts = new Texts();
m_SubEmitters = GetProperty("subEmitters");
m_EmittersList = new ReorderableList(m_SubEmitters.m_SerializedObject, m_SubEmitters, true, false, true, true);
m_EmittersList.headerHeight = 0;
m_EmittersList.drawElementCallback = DrawSubEmitterElementCallback;
m_EmittersList.elementHeight = kSingleLineHeight * 3 + EditorGUIUtility.standardVerticalSpacing * 4 + kSubEmitterPadding * 2;
m_EmittersList.drawElementBackgroundCallback = DrawSubEmitterElementBackgroundCallback;
m_EmittersList.onAddCallback = OnAddSubEmitterElementCallback;
}
void OnAddSubEmitterElementCallback(ReorderableList list)
{
m_SubEmitters.InsertArrayElementAtIndex(m_SubEmitters.arraySize);
SerializedProperty newSubEmitterData = m_SubEmitters.GetArrayElementAtIndex(m_SubEmitters.arraySize - 1);
SerializedProperty newSubEmitter = newSubEmitterData.FindPropertyRelative("emitter");
newSubEmitter.objectReferenceValue = null;
}
static void DrawSubEmitterElementBackgroundCallback(Rect rect, int index, bool isActive, bool isFocused)
{
GUI.Label(rect, GUIContent.none, EditorStyles.helpBox);
ReorderableList.defaultBehaviours.DrawElementBackground(rect, index, isActive, isFocused, true);
}
void DrawSubEmitterElementCallback(Rect rect, int index, bool isActive, bool isFocused)
{
var subEmitterData = m_SubEmitters.GetArrayElementAtIndex(index);
var subEmitter = subEmitterData.FindPropertyRelative("emitter");
var type = subEmitterData.FindPropertyRelative("type");
var properties = subEmitterData.FindPropertyRelative("properties");
var emitProbability = subEmitterData.FindPropertyRelative("emitProbability");
rect.height = kSingleLineHeight;
rect.y += kSubEmitterPadding;
Rect typeRect = new Rect(rect.x, rect.y, EditorGUIUtility.labelWidth - EditorGUI.kSpacing, rect.height);
Rect objectRect = new Rect(rect.x + EditorGUIUtility.labelWidth, rect.y, rect.width - EditorGUIUtility.labelWidth - EditorGUI.kSpacing * 3, rect.height);
GUIPopup(typeRect, GUIContent.none, type, s_Texts.subEmitterTypes);
GUIObject(objectRect, GUIContent.none, subEmitter, null);
if (subEmitter.objectReferenceValue == null)
{
Rect buttonRect = new Rect(objectRect.xMax + EditorGUI.kSpacing, rect.y + 4 , ParticleSystemStyles.Get().plus.fixedWidth, rect.height);
if (GUI.Button(buttonRect, s_Texts.create, ParticleSystemStyles.Get().plus))
{
CreateSubEmitter(subEmitter, index, (SubEmitterType)type.intValue);
}
}
rect.y += EditorGUIUtility.standardVerticalSpacing + rect.height;
GUIMask(rect, s_Texts.inherit, properties, s_Texts.propertyTypes);
rect.y += EditorGUIUtility.standardVerticalSpacing + rect.height;
GUIFloat(rect, s_Texts.emitProbability, emitProbability);
}
void CreateSubEmitter(SerializedProperty objectRefProp, int index, SubEmitterType type)
{
GameObject subEmitter = m_ParticleSystemUI.m_ParticleEffectUI.CreateParticleSystem(m_ParticleSystemUI.m_ParticleSystems[0], type);
subEmitter.name = "SubEmitter" + index;
objectRefProp.objectReferenceValue = subEmitter.GetComponent<ParticleSystem>();
}
void Update()
{
if (m_CheckObjectIndex >= 0)
{
// Wait until the ObjectSelector is closed before checking object
if (!ObjectSelector.isVisible)
{
SerializedProperty subEmitterData = m_SubEmitters.GetArrayElementAtIndex(m_CheckObjectIndex);
SerializedProperty subEmitter = subEmitterData.FindPropertyRelative("emitter");
Object obj = subEmitter.objectReferenceValue;
ParticleSystem newSubEmitter = obj as ParticleSystem;
if (newSubEmitter != null)
{
bool validSubemitter = true;
if (ValidateSubemitter(newSubEmitter))
{
string errorMsg = ParticleSystemEffectUtils.CheckCircularReferences(newSubEmitter);
if (errorMsg.Length == 0)
{
// Ok there is no circular references, now check if its a child
if (!CheckIfChild(obj))
validSubemitter = false;
}
else
{
// Circular references detected
string circularRefErrorMsg = string.Format("'{0}' could not be assigned as subemitter on '{1}' due to circular referencing!\nBacktrace: {2} \n\nReference will be removed.", newSubEmitter.gameObject.name, m_ParticleSystemUI.m_ParticleSystems[0].gameObject.name, errorMsg);
EditorUtility.DisplayDialog("Circular References Detected", circularRefErrorMsg, "OK");
validSubemitter = false;
}
}
else
{
validSubemitter = false;
}
if (!validSubemitter)
{
subEmitter.objectReferenceValue = m_PreviousSubEmitter; // revert invalid reference
m_ParticleSystemUI.ApplyProperties();
m_ParticleSystemUI.m_ParticleEffectUI.m_Owner.Repaint();
}
}
// Cleanup
m_CheckObjectIndex = -1;
m_PreviousSubEmitter = null;
EditorApplication.update -= Update;
}
}
}
internal static bool IsChild(ParticleSystem subEmitter, ParticleSystem root)
{
if (subEmitter == null || root == null)
return false;
ParticleSystem subRoot = ParticleSystemEditorUtils.GetRoot(subEmitter);
return subRoot == root;
}
private bool ValidateSubemitter(ParticleSystem subEmitter)
{
if (subEmitter == null)
return false;
ParticleSystem root = ParticleSystemEditorUtils.GetRoot(m_ParticleSystemUI.m_ParticleSystems[0]);
if (root.gameObject.activeInHierarchy && !subEmitter.gameObject.activeInHierarchy)
{
string kReparentText = "The assigned sub emitter is part of a prefab and can therefore not be assigned.";
EditorUtility.DisplayDialog("Invalid Sub Emitter", kReparentText, "OK");
return false;
}
if (!root.gameObject.activeInHierarchy && subEmitter.gameObject.activeInHierarchy)
{
string kReparentText = "The assigned sub emitter is part of a scene object and can therefore not be assigned to a prefab.";
EditorUtility.DisplayDialog("Invalid Sub Emitter", kReparentText, "OK");
return false;
}
return true;
}
private bool CheckIfChild(Object subEmitter)
{
ParticleSystem root = ParticleSystemEditorUtils.GetRoot(m_ParticleSystemUI.m_ParticleSystems[0]);
ParticleSystem ps = subEmitter as ParticleSystem;
if (IsChild(ps, root))
{
return true;
}
if (PrefabUtility.IsPartOfAnyPrefab(ps.gameObject) && !PrefabUtility.IsAnyPrefabInstanceRoot(ps.gameObject))
{
string kPrefabReparentWarn = $"The assigned sub emitter is not part of the current effect because it is not a child of the current root Particle System GameObject: '{root.gameObject.name}'. The Particle System cannot be moved because it is a child of a Prefab instance.";
EditorUtility.WarnPrefab(ps.gameObject, "Reparent GameObjects", kPrefabReparentWarn, "OK");
return false;
}
string kReparentText = string.Format("The assigned sub emitter is not a child of the current root particle system GameObject: '{0}' and is therefore NOT considered a part of the current effect. Do you want to reparent it?", root.gameObject.name);
if (EditorUtility.DisplayDialog(
"Reparent GameObjects",
kReparentText,
"Yes, Reparent",
"No, Remove"))
{
if (EditorUtility.IsPersistent(subEmitter))
{
var newGo = Object.Instantiate(subEmitter) as GameObject;
if (newGo != null)
{
newGo.transform.parent = m_ParticleSystemUI.m_ParticleSystems[0].transform;
newGo.transform.localPosition = Vector3.zero;
newGo.transform.localRotation = Quaternion.identity;
}
}
else
{
if (ps != null)
{
Undo.SetTransformParent(ps.gameObject.transform.transform, m_ParticleSystemUI.m_ParticleSystems[0].transform, "Reparent sub emitter");
}
}
return true;
}
else if (ps != null)
{
// Clear sub-emitters that have been deselected, to avoid having their particles left paused in the Scene View (case 946999)
ps.Clear(true);
}
return false;
}
private List<Object> GetSubEmitterProperties()
{
List<Object> props = new List<Object>();
var enumerator = m_SubEmitters.GetEnumerator();
while (enumerator.MoveNext())
{
SerializedProperty subEmitterData = (SerializedProperty)enumerator.Current;
props.Add(subEmitterData.FindPropertyRelative("emitter").objectReferenceValue);
}
return props;
}
override public void OnInspectorGUI(InitialModuleUI initial)
{
// only allow sub-emitter editing in single edit mode
if (m_ParticleSystemUI.multiEdit)
{
EditorGUILayout.HelpBox("Sub Emitter editing is only available when editing a single Particle System", MessageType.Info, true);
return;
}
// get array of subemitters
List<Object> props = GetSubEmitterProperties();
m_EmittersList.DoLayoutList();
// show warnings for any subemitters that are not a descendent of the owning system
string invalidSubEmitters = "";
foreach (var ps in m_ParticleSystemUI.m_ParticleSystems)
{
// skip this for Presets - traversing the parent hierarchy doesn't work because they aren't real GameObjects.
if ((ps.gameObject.hideFlags & HideFlags.NotEditable) != 0)
continue;
var subEmitters = ps.subEmitters;
for (int i = 0; i < subEmitters.subEmittersCount; i++)
{
var subEmitter = subEmitters.GetSubEmitterSystem(i);
if (subEmitter != null)
{
bool valid = false;
var currentParent = subEmitter.transform.parent;
while (currentParent != null)
{
if (currentParent == ps.transform)
{
valid = true;
break;
}
currentParent = currentParent.parent;
}
if (!valid)
invalidSubEmitters += string.Format(" - {0}\n", subEmitter.name);
}
}
}
if (invalidSubEmitters.Length > 0)
EditorGUILayout.HelpBox(string.Format("{0}\n{1}", s_Texts.invalidSubEmitterParenting.text, invalidSubEmitters.TrimEnd('\n')), MessageType.Error, true);
// get new list of subemitters, so we can check for changes
List<Object> props2 = GetSubEmitterProperties();
// validate any new subemitters we assigned
for (int i = 0; i < Mathf.Min(props.Count, props2.Count); i++)
{
if (props[i] != props2[i])
{
if (m_CheckObjectIndex == -1)
EditorApplication.update += Update; // ensure its not added more than once
// We need to let the ObjectSelector finish its SendEvent and therefore delay showing dialog
m_CheckObjectIndex = i;
m_PreviousSubEmitter = props[i];
// Clear sub-emitters that have been deselected, to avoid having their particles left paused in the Scene View (case 946999)
ParticleSystem ps = props[i] as ParticleSystem;
if (ps)
ps.Clear(true);
}
}
}
override public void UpdateCullingSupportedString(ref string text)
{
text += "\nSub Emitters module is enabled.";
}
}
} // namespace UnityEditor