-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathParticleEffectUI.cs
More file actions
1337 lines (1149 loc) · 53.7 KB
/
ParticleEffectUI.cs
File metadata and controls
1337 lines (1149 loc) · 53.7 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
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using UnityEditor.Overlays;
using UnityEditor.ShortcutManagement;
using UnityEngine;
using UnityEngine.Rendering;
// The ParticleEffectUI displays one or more ParticleSystemUIs.
namespace UnityEditor
{
internal interface ParticleEffectUIOwner
{
void Repaint();
Editor customEditor { get; }
}
internal class ParticleEffectUI
{
public ParticleEffectUIOwner m_Owner; // Can be InspectorWindow or ParticleSystemWindow
static ParticleEffectUI s_EffectUi;
public ParticleSystemUI[] m_Emitters; // Contains UI for all ParticleSystem children of the root ParticleSystem for this effect
bool m_EmittersActiveInHierarchy;
public bool m_SubEmitterSelected;
ParticleSystemCurveEditor m_ParticleSystemCurveEditor; // The curve editor used by ParticleSystem modules
List<ParticleSystem> m_SelectedParticleSystems; // This is the array of selected particle systems and used to find the root ParticleSystem and for the inspector
TimeHelper m_TimeHelper = new TimeHelper();
public static ParticleSystem m_MainPlaybackSystem;
public static bool m_ShowBounds = false;
public static bool m_ShowOnlySelected = false;
public static bool m_VerticalLayout;
const string k_SimulationStateId = "SimulationState";
enum PlayState { Stopped = 0, Playing = 1, Paused = 2 }
// ParticleSystemWindow Layout
static readonly Vector2 k_MinEmitterAreaSize = new Vector2(125f, 100);
static readonly Vector2 k_MinCurveAreaSize = new Vector2(100, 100);
float m_EmitterAreaWidth = 230; // Only used in ParticleSystemWindow for horizontal layout
float m_CurveEditorAreaHeight = 330; // Only used in ParticleSystemWindow for vertical layout
Vector2 m_EmitterAreaScrollPos = Vector2.zero;
static readonly Color k_DarkSkinDisabledColor = new Color(0.66f, 0.66f, 0.66f, 0.95f);
static readonly Color k_LightSkinDisabledColor = new Color(0.84f, 0.84f, 0.84f, 0.95f);
private enum OwnerType { Inspector, ParticleSystemWindow }
internal class Texts
{
public GUIContent previewSpeed = EditorGUIUtility.TrTextContent("Playback Speed", "Playback Speed is also affected by the Time Scale setting in the Time Manager.");
public GUIContent previewSpeedDisabled = EditorGUIUtility.TrTextContent("Playback Speed", "Playback Speed is locked to 0.0, because the Time Scale in the Time Manager is set to 0.0.");
public GUIContent previewTime = EditorGUIUtility.TrTextContent("Playback Time", "Playback Time since the Particle System has started");
public GUIContent particleCount = EditorGUIUtility.TrTextContent("Particles", "Particles count");
public GUIContent subEmitterParticleCount = EditorGUIUtility.TrTextContent("Sub Emitter Particles");
public GUIContent particleSpeeds = EditorGUIUtility.TrTextContent("Speed Range", "Start speed minimum and maximum values");
public GUIContent play = EditorGUIUtility.TrTextContent("Play", "Play Particle System");
public GUIContent playDisabled = EditorGUIUtility.TrTextContent("Play", "Play is disabled, because the Time Scale in the Time Manager is set to 0.0.");
public GUIContent stop = EditorGUIUtility.TrTextContent("Stop", "Stop Particle System");
public GUIContent pause = EditorGUIUtility.TrTextContent("Pause", "Pause Particle System");
public GUIContent restart = EditorGUIUtility.TrTextContent("Restart", "Restart Particle System");
public GUIContent addParticleSystem = EditorGUIUtility.TrTextContent("", "Create Particle System");
public GUIContent showBounds = EditorGUIUtility.TrTextContent("Show Bounds", "Show world space bounding boxes.");
public GUIContent showOnlySelected = EditorGUIUtility.TrTextContent("Show Only Selected", "Hide all unselected Particle Systems in the current Effect.");
public GUIContent resimulation = EditorGUIUtility.TrTextContent("Resimulate", "If resimulate is enabled, the Particle System will show changes made to the system immediately (including changes made to the Particle System Transform).");
public GUIContent previewLayers = EditorGUIUtility.TrTextContent("Simulate Layers", "Automatically preview all looping Particle Systems on the chosen layers, in addition to the selected Game Objects.");
public string secondsFloatFieldFormatString = "f2";
public string speedFloatFieldFormatString = "f1";
}
private static Texts s_Texts;
internal static Texts texts
{
get
{
if (s_Texts == null)
s_Texts = new Texts();
return s_Texts;
}
}
static Event CreateCommandEvent(string commandName)
{
return new Event { type = EventType.ExecuteCommand, commandName = "ParticleSystem/" + commandName };
}
static Event s_PlayEvent = CreateCommandEvent("Play");
static Event s_StopEvent = CreateCommandEvent("Stop");
static Event s_RestartEvent = CreateCommandEvent("Restart");
static Event s_ResimulationEvent = CreateCommandEvent("Resimulation");
static Event s_ShowBoundsEvent = CreateCommandEvent("ShowBounds");
static Event s_ShowOnlySelectedEvent = CreateCommandEvent("ShowOnlySelected");
static Event s_ForwardBeginEvent = CreateCommandEvent("ForwardBegin");
static Event s_ForwardEndEvent = CreateCommandEvent("ForwardEnd");
static Event s_ReverseBeginEvent = CreateCommandEvent("ReverseBegin");
static Event s_ReverseEndEvent = CreateCommandEvent("ReverseEnd");
static void DispatchShortcutEvent(Event evt)
{
var sceneView = SceneView.lastActiveSceneView;
if (sceneView != null)
{
if (sceneView.SendEvent(evt))
return;
}
var inspectors = Resources.FindObjectsOfTypeAll<ParticleSystemInspector>();
foreach (var inspector in inspectors)
{
if (inspector != null)
{
if (inspector.HandleShortcutEvent(evt))
return;
}
}
var windows = Resources.FindObjectsOfTypeAll<ParticleSystemWindow>();
foreach (var window in windows)
{
if (window != null)
{
if (window.HandleShortcutEvent(evt))
return;
}
}
}
[FormerlyPrefKeyAs("ParticleSystem/Play", ",")]
[Shortcut("ParticleSystem/Play", typeof(ParticleSystemInspector.ShortcutContext), KeyCode.Comma)]
static void PlayPauseShortcut(ShortcutArguments args)
{
DispatchShortcutEvent(s_PlayEvent);
}
[FormerlyPrefKeyAs("ParticleSystem/Stop", ".")]
[Shortcut("ParticleSystem/Stop", typeof(ParticleSystemInspector.ShortcutContext), KeyCode.Period)]
static void StopShortcut(ShortcutArguments args)
{
DispatchShortcutEvent(s_StopEvent);
}
[Shortcut("ParticleSystem/Restart", typeof(ParticleSystemInspector.ShortcutContext), KeyCode.Slash)]
static void RestartShortcut(ShortcutArguments args)
{
DispatchShortcutEvent(s_RestartEvent);
}
[Shortcut("ParticleSystem/Resimulation", typeof(ParticleSystemInspector.ShortcutContext))]
static void ResimulationShortcut(ShortcutArguments args)
{
DispatchShortcutEvent(s_ResimulationEvent);
}
[Shortcut("ParticleSystem/ShowBounds", typeof(ParticleSystemInspector.ShortcutContext))]
static void ShowBoundsShortcut(ShortcutArguments args)
{
DispatchShortcutEvent(s_ShowBoundsEvent);
}
[Shortcut("ParticleSystem/ShowOnlySelected", typeof(ParticleSystemInspector.ShortcutContext))]
static void ShowOnlySelectedShortcut(ShortcutArguments args)
{
DispatchShortcutEvent(s_ShowOnlySelectedEvent);
}
[FormerlyPrefKeyAs("ParticleSystem/Forward", "m")]
[ClutchShortcut("ParticleSystem/Forward", typeof(ParticleSystemInspector.ShortcutContext), KeyCode.M)]
static void ForwardShortcut(ShortcutArguments args)
{
DispatchShortcutEvent(args.stage == ShortcutStage.Begin ? s_ForwardBeginEvent : s_ForwardEndEvent);
}
[FormerlyPrefKeyAs("ParticleSystem/Reverse", "n")]
[ClutchShortcut("ParticleSystem/Reverse", typeof(ParticleSystemInspector.ShortcutContext), KeyCode.N)]
static void ReverseShortcut(ShortcutArguments args)
{
DispatchShortcutEvent(args.stage == ShortcutStage.Begin ? s_ReverseBeginEvent : s_ReverseEndEvent);
}
public ParticleEffectUI(ParticleEffectUIOwner owner)
{
m_Owner = owner;
System.Diagnostics.Debug.Assert(m_Owner is ParticleSystemInspector || m_Owner is ParticleSystemWindow);
}
public bool multiEdit { get { return (m_SelectedParticleSystems != null) && (m_SelectedParticleSystems.Count > 1); } }
private bool ShouldManagePlaybackState(ParticleSystem root)
{
bool active = false;
if (root != null)
active = root.gameObject.activeInHierarchy;
return active && !Application.IsPlaying(root.gameObject);
}
static Color GetDisabledColor()
{
return (!EditorGUIUtility.isProSkin) ? k_LightSkinDisabledColor : k_DarkSkinDisabledColor;
}
// Returns a list with 'root' and all its direct children.
static internal ParticleSystem[] GetParticleSystems(ParticleSystem root)
{
List<ParticleSystem> particleSystems = new List<ParticleSystem>();
particleSystems.Add(root);
GetDirectParticleSystemChildrenRecursive(root.transform, particleSystems);
return particleSystems.ToArray();
}
// Adds only active Particle Systems
static private void GetDirectParticleSystemChildrenRecursive(Transform transform, List<ParticleSystem> particleSystems)
{
foreach (Transform childTransform in transform)
{
ParticleSystem ps = childTransform.gameObject.GetComponent<ParticleSystem>();
if (ps != null)
{
// Note: we do not check for if the gameobject is active (we want inactive particle systems as well due prefabs)
particleSystems.Add(ps);
GetDirectParticleSystemChildrenRecursive(childTransform, particleSystems);
}
}
}
// Should be called often to ensure we catch if selected Particle System is dragged in/out of root hierarchy
public bool InitializeIfNeeded(IEnumerable<ParticleSystem> systems)
{
bool anyAdded = false;
ParticleSystem[] allSystems = systems.ToArray();
bool usingMultiEdit = (allSystems.Length > 1);
bool initializeRequired = false;
ParticleSystem mainSystem = null;
foreach (ParticleSystem shuriken in allSystems)
{
ParticleSystem[] shurikens;
if (!usingMultiEdit)
{
ParticleSystem root = ParticleSystemEditorUtils.GetRoot(shuriken);
if (root == null)
continue;
shurikens = GetParticleSystems(root);
mainSystem = root;
// Check if we need to re-initialize?
if (m_SelectedParticleSystems != null && m_SelectedParticleSystems.Count > 0)
{
if (root == ParticleSystemEditorUtils.GetRoot(m_SelectedParticleSystems[0]))
{
if (m_ParticleSystemCurveEditor != null && m_Emitters != null && shurikens.Length == m_Emitters.Length && shuriken.gameObject.activeInHierarchy == m_EmittersActiveInHierarchy)
{
m_SelectedParticleSystems = new List<ParticleSystem>();
m_SelectedParticleSystems.Add(shuriken);
if (m_ShowOnlySelected)
SetShowOnlySelectedMode(m_ShowOnlySelected); // always refresh
continue;
}
}
}
}
else
{
// in multi-edit mode, we explicitly choose the systems to edit, so don't automatically add child systems or search for the root
shurikens = new ParticleSystem[] { shuriken };
mainSystem = shuriken;
}
// Cleanup before initializing
if (m_ParticleSystemCurveEditor != null)
Clear();
// Now initialize
initializeRequired = true;
if (!anyAdded)
{
m_SelectedParticleSystems = new List<ParticleSystem>();
anyAdded = true;
}
m_SelectedParticleSystems.Add(shuriken);
// Single edit emitter setup
if (!usingMultiEdit)
{
// Init CurveEditor before modules (they may add curves during construction)
m_ParticleSystemCurveEditor = new ParticleSystemCurveEditor();
m_ParticleSystemCurveEditor.Init();
int numEmitters = shurikens.Length;
if (numEmitters > 0)
{
m_Emitters = new ParticleSystemUI[numEmitters];
for (int i = 0; i < numEmitters; ++i)
{
m_Emitters[i] = new ParticleSystemUI();
m_Emitters[i].Init(this, new ParticleSystem[] { shurikens[i] });
}
m_EmittersActiveInHierarchy = shuriken.gameObject.activeInHierarchy;
}
}
}
if (initializeRequired)
{
// Multi-edit emitter setup
if (usingMultiEdit)
{
// Init CurveEditor before modules (they may add curves during construction)
m_ParticleSystemCurveEditor = new ParticleSystemCurveEditor();
m_ParticleSystemCurveEditor.Init();
int numEmitters = m_SelectedParticleSystems.Count;
if (numEmitters > 0)
{
m_Emitters = new ParticleSystemUI[1];
m_Emitters[0] = new ParticleSystemUI();
m_Emitters[0].Init(this, m_SelectedParticleSystems.ToArray());
m_EmittersActiveInHierarchy = m_SelectedParticleSystems[0].gameObject.activeInHierarchy;
}
}
// Allow modules to validate their state (the user can have moved emitters around in the hierarchy)
foreach (ParticleSystemUI e in m_Emitters)
foreach (ModuleUI m in e.m_Modules)
if (m != null)
m.Validate();
// Sync to state
if (GetAllModulesVisible())
SetAllModulesVisible(true);
m_EmitterAreaWidth = EditorPrefs.GetFloat("ParticleSystemEmitterAreaWidth", k_MinEmitterAreaSize.x);
m_CurveEditorAreaHeight = EditorPrefs.GetFloat("ParticleSystemCurveEditorAreaHeight", k_MinCurveAreaSize.y);
SetShowOnlySelectedMode(m_ShowOnlySelected);
m_EmitterAreaScrollPos.x = SessionState.GetFloat("CurrentEmitterAreaScroll", 0.0f);
if (ShouldManagePlaybackState(mainSystem))
{
TryRestorePlayState(mainSystem);
// Play when selecting a new particle effect
if (m_MainPlaybackSystem != mainSystem)
Play();
}
}
m_MainPlaybackSystem = mainSystem;
return initializeRequired;
}
void SavePlayState(ParticleSystem particleSystem)
{
if (particleSystem == null)
return;
// Store simulation state of current effect as Vector3 (rootInstanceID, PlayState, playBackTime) in Session cache
int rootInstanceId = particleSystem.GetInstanceID();
Vector3 state = new Vector3(rootInstanceId, (int)GetCurrentPlayState(), ParticleSystemEditorUtils.playbackTime);
SessionState.SetVector3(k_SimulationStateId + rootInstanceId, state);
}
void TryRestorePlayState(ParticleSystem particleSystem)
{
if (particleSystem == null)
return;
Vector3 simulationState = SessionState.GetVector3(k_SimulationStateId + particleSystem.GetInstanceID(), Vector3.zero);
if (particleSystem.GetInstanceID() == (int)simulationState.x)
{
float lastPlayBackTime = simulationState.z;
if (lastPlayBackTime > 0f)
{
if (m_MainPlaybackSystem != particleSystem)
ParticleSystemEditorUtils.PerformCompleteResimulation();
ParticleSystemEditorUtils.playbackTime = lastPlayBackTime;
}
PlayState playState = (PlayState)simulationState.y;
switch (playState)
{
case PlayState.Stopped:
Stop();
break;
case PlayState.Playing:
Play();
break;
case PlayState.Paused:
Pause();
break;
}
}
}
PlayState GetCurrentPlayState()
{
PlayState playState;
if (IsPlaying())
playState = PlayState.Playing;
else if (IsPaused())
playState = PlayState.Paused;
else
playState = PlayState.Stopped;
return playState;
}
internal void UndoRedoPerformed(in UndoRedoInfo info)
{
Refresh();
foreach (ParticleSystemUI e in m_Emitters)
{
foreach (ModuleUI moduleUI in e.m_Modules)
{
if (moduleUI != null)
{
moduleUI.CheckVisibilityState();
if (moduleUI.foldout)
moduleUI.UndoRedoPerformed(info);
}
}
}
// Undo will deactivate the ParticleSystem and therefore stop it, here we resume the playback state for the user (UUM-28514)
RestorePlayBackStateForCurrentSelectedParticleSystem();
m_Owner.Repaint();
}
internal void PrefabInstanceUpdated(GameObject instance)
{
// Any update to prefab instances will have stopped the play back here we resume the playback state for the user (UUM-28514)
RestorePlayBackStateForCurrentSelectedParticleSystem();
}
internal void PrefabInstanceReverted(GameObject instance)
{
// Any update to prefab instances will have stopped the play back here we resume the playback state for the user (UUM-28514)
RestorePlayBackStateForCurrentSelectedParticleSystem();
}
void RestorePlayBackStateForCurrentSelectedParticleSystem()
{
if (m_SelectedParticleSystems == null || m_SelectedParticleSystems.Count == 0)
return;
ParticleSystem root = ParticleSystemEditorUtils.GetRoot(m_SelectedParticleSystems[0]);
if (ShouldManagePlaybackState(root))
{
TryRestorePlayState(root);
}
}
public void Clear()
{
ParticleSystem root = ParticleSystemEditorUtils.GetRoot(m_SelectedParticleSystems[0]); // root can have been deleted
if (ShouldManagePlaybackState(root))
{
SavePlayState(root);
// Stop the ParticleSystem here (prevents it being frozen on screen)
//Stop();
}
m_ParticleSystemCurveEditor.OnDisable();
Tools.s_Hidden = false; // The collisionmodule might have hidden the tools
if (s_EffectUi == this)
s_EffectUi = null;
SetShowOnlySelectedMode(false);
PlayModeView.RepaintAll();
SceneView.RepaintAll();
}
public void ClearSelectedSystems()
{
// We cant clear the selected systems inside of Clear as this method is also used during InitializeIfNeeded when
// the list is created and calling Clear each time would corrupt the list when multi editing. (case 1254599)
m_SelectedParticleSystems?.Clear();
}
static public Vector2 GetMinSize()
{
return k_MinEmitterAreaSize + k_MinCurveAreaSize;
}
public void Refresh()
{
UpdateProperties();
m_ParticleSystemCurveEditor.Refresh();
}
public string GetNextParticleSystemName()
{
string nextName = "";
for (int i = 2; i < 50; ++i)
{
nextName = L10n.Tr("Particle System ") + i;
bool found = false;
foreach (ParticleSystemUI e in m_Emitters)
{
if (e.m_ParticleSystems.FirstOrDefault(o => o.name == nextName) != null)
{
found = true;
break;
}
}
if (!found)
return nextName;
}
return L10n.Tr("Particle System");
}
public bool IsParticleSystemUIVisible(ParticleSystemUI psUI)
{
OwnerType ownerType = m_Owner is ParticleSystemInspector ? OwnerType.Inspector : OwnerType.ParticleSystemWindow;
if (ownerType == OwnerType.ParticleSystemWindow)
return true;
// ownerType == OwnerType.Inspector
foreach (ParticleSystem ps in psUI.m_ParticleSystems)
{
if (m_SelectedParticleSystems.FirstOrDefault(o => o == ps) != null)
return true;
}
return false;
}
public void PlayOnAwakeChanged(bool newPlayOnAwake)
{
foreach (ParticleSystemUI psUI in m_Emitters)
{
InitialModuleUI initialModule = psUI.m_Modules[0] as InitialModuleUI;
System.Diagnostics.Debug.Assert(initialModule != null);
initialModule.m_PlayOnAwake.boolValue = newPlayOnAwake;
psUI.ApplyProperties();
}
}
public GameObject CreateParticleSystem(ParticleSystem parentOfNewParticleSystem, SubModuleUI.SubEmitterType defaultType)
{
string name = GetNextParticleSystemName();
GameObject go = new GameObject(name, typeof(ParticleSystem));
if (go)
{
if (parentOfNewParticleSystem)
go.transform.parent = parentOfNewParticleSystem.transform;
go.transform.localPosition = Vector3.zero;
go.transform.localRotation = Quaternion.identity;
// Setup particle system based on type
ParticleSystem ps = go.GetComponent<ParticleSystem>();
if (defaultType != SubModuleUI.SubEmitterType.None)
ps.SetupDefaultType((ParticleSystemSubEmitterType)defaultType);
SessionState.SetFloat("CurrentEmitterAreaScroll", m_EmitterAreaScrollPos.x);
// Assign default material
ParticleSystemRenderer renderer = go.GetComponent<ParticleSystemRenderer>();
Material particleMat = GraphicsSettings.GetDefaultMaterial(DefaultMaterialType.Particle);
if (particleMat == null)
particleMat = AssetDatabase.GetBuiltinExtraResource<Material>("Default-ParticleSystem.mat");
renderer.material = particleMat;
Undo.RegisterCreatedObjectUndo(go, "Create ParticleSystem");
return go;
}
return null;
}
public ParticleSystemCurveEditor GetParticleSystemCurveEditor()
{
return m_ParticleSystemCurveEditor;
}
public void OnSceneViewGUI()
{
ParticleSystem root = ParticleSystemEditorUtils.GetRoot(m_SelectedParticleSystems[0]);
if (root && root.gameObject.activeInHierarchy)
s_EffectUi = this;
else
s_EffectUi = null;
foreach (ParticleSystemUI e in m_Emitters)
e.OnSceneViewGUI();
}
int m_IsDraggingTimeHotControlID = -1;
internal void PlayBackInfoGUI(bool isPlayMode)
{
EventType oldEventType = Event.current.type;
int oldHotControl = GUIUtility.hotControl;
string oldFormat = EditorGUI.kFloatFieldFormatString;
EditorGUIUtility.labelWidth = 110.0f;
if (!isPlayMode)
{
EditorGUI.kFloatFieldFormatString = s_Texts.secondsFloatFieldFormatString;
if (Time.timeScale == 0.0f)
{
using (new EditorGUI.DisabledScope(true))
{
EditorGUILayout.FloatField(s_Texts.previewSpeedDisabled, 0.0f);
}
}
else
{
ParticleSystemEditorUtils.simulationSpeed = Mathf.Clamp(EditorGUILayout.FloatField(s_Texts.previewSpeed, ParticleSystemEditorUtils.simulationSpeed), 0f, 10f);
}
EditorGUI.kFloatFieldFormatString = oldFormat;
EditorGUI.BeginChangeCheck();
EditorGUI.kFloatFieldFormatString = s_Texts.secondsFloatFieldFormatString;
float playbackTime = EditorGUILayout.FloatField(s_Texts.previewTime, ParticleSystemEditorUtils.playbackTime);
EditorGUI.kFloatFieldFormatString = oldFormat;
if (EditorGUI.EndChangeCheck())
{
if (oldEventType == EventType.MouseDrag)
{
ParticleSystemEditorUtils.playbackIsScrubbing = true;
float previewSpeed = ParticleSystemEditorUtils.simulationSpeed;
float oldplaybackTime = ParticleSystemEditorUtils.playbackTime;
float timeDiff = playbackTime - oldplaybackTime;
playbackTime = oldplaybackTime + timeDiff * (0.05F * previewSpeed);
}
playbackTime = Mathf.Max(playbackTime, 0.0F);
ParticleSystemEditorUtils.playbackTime = playbackTime;
foreach (ParticleSystem ps in m_SelectedParticleSystems)
{
ParticleSystem root = ParticleSystemEditorUtils.GetRoot(ps);
if (root.isStopped)
{
root.Play();
root.Pause();
}
}
ParticleSystemEditorUtils.PerformCompleteResimulation();
}
// Detect start dragging
if (oldEventType == EventType.MouseDown && GUIUtility.hotControl != oldHotControl)
{
m_IsDraggingTimeHotControlID = GUIUtility.hotControl;
ParticleSystemEditorUtils.playbackIsScrubbing = true;
}
// Detect stop dragging
if (m_IsDraggingTimeHotControlID != -1 && GUIUtility.hotControl != m_IsDraggingTimeHotControlID)
{
m_IsDraggingTimeHotControlID = -1;
ParticleSystemEditorUtils.playbackIsScrubbing = false;
}
}
int particleCount = 0;
float fastestParticle = 0.0f;
float slowestParticle = Mathf.Infinity;
foreach (ParticleSystem ps in m_SelectedParticleSystems)
{
if (ps != null)
ps.CalculateEffectUIData(ref particleCount, ref fastestParticle, ref slowestParticle);
}
EditorGUILayout.LabelField(s_Texts.particleCount, GUIContent.Temp(particleCount.ToString()));
bool hasSubEmitters = false;
int subEmitterParticles = 0;
foreach (ParticleSystem ps in m_SelectedParticleSystems)
{
int subEmitterParticlesCurrent = 0;
if (ps != null && ps.CalculateEffectUISubEmitterData(ref subEmitterParticlesCurrent, ref fastestParticle, ref slowestParticle))
{
hasSubEmitters = true;
subEmitterParticles += subEmitterParticlesCurrent;
}
}
if (hasSubEmitters)
EditorGUILayout.LabelField(s_Texts.subEmitterParticleCount, GUIContent.Temp(subEmitterParticles.ToString()));
if (fastestParticle >= slowestParticle)
EditorGUILayout.LabelField(s_Texts.particleSpeeds, GUIContent.Temp(slowestParticle.ToString(s_Texts.speedFloatFieldFormatString, CultureInfo.InvariantCulture.NumberFormat) + " - " + fastestParticle.ToString(s_Texts.speedFloatFieldFormatString, CultureInfo.InvariantCulture.NumberFormat)));
else
EditorGUILayout.LabelField(s_Texts.particleSpeeds, GUIContent.Temp("0.0 - 0.0"));
if (!isPlayMode)
{
ParticleSystemEditorUtils.previewLayers = EditorGUILayout.LayerMaskField(ParticleSystemEditorUtils.previewLayers, s_Texts.previewLayers);
ParticleSystemEditorUtils.resimulation = GUILayout.Toggle(ParticleSystemEditorUtils.resimulation, s_Texts.resimulation, EditorStyles.toggle);
}
m_ShowBounds = GUILayout.Toggle(m_ShowBounds, texts.showBounds, EditorStyles.toggle);
EditorGUI.BeginChangeCheck();
m_ShowOnlySelected = GUILayout.Toggle(m_ShowOnlySelected, texts.showOnlySelected, EditorStyles.toggle);
if (EditorGUI.EndChangeCheck())
SetShowOnlySelectedMode(m_ShowOnlySelected);
EditorGUIUtility.labelWidth = 0.0f;
}
bool m_ScrubForward;
bool m_ScrubReverse;
float m_ScrubNextUpdate;
void HandleScrubbing()
{
if ((!m_ScrubForward && !m_ScrubReverse) || Time.realtimeSinceStartup < m_ScrubNextUpdate)
return;
var evt = Event.current;
var changeTime = 0;
if (m_ScrubForward)
changeTime++;
if (m_ScrubReverse)
changeTime--;
ParticleSystemEditorUtils.playbackIsScrubbing = true;
float previewSpeed = ParticleSystemEditorUtils.simulationSpeed;
float timeDiff = (evt.shift ? 3f : 1f) * m_TimeHelper.deltaTime * (changeTime * 3f);
ParticleSystemEditorUtils.playbackTime = Mathf.Max(0f, ParticleSystemEditorUtils.playbackTime + timeDiff * (previewSpeed));
foreach (ParticleSystem ps in m_SelectedParticleSystems)
{
ParticleSystem root = ParticleSystemEditorUtils.GetRoot(ps);
if (root.isStopped)
{
root.Play();
root.Pause();
}
}
ParticleSystemEditorUtils.PerformCompleteResimulation();
// Mimic previous behavior that relied on key repeat
m_ScrubNextUpdate = Time.realtimeSinceStartup + 1f / 15f;
}
internal bool HandleShortcutEvent(Event evt)
{
if (evt.commandName == s_PlayEvent.commandName)
{
if (EditorApplication.isPlaying)
{
// If world is playing Pause is not handled, just restart instead
Stop();
Play();
}
else
{
// In Edit mode we have full play/pause functionality
if (!ParticleSystemEditorUtils.playbackIsPlaying)
Play();
else
Pause();
}
return true;
}
else if (evt.commandName == s_StopEvent.commandName)
{
Stop();
return true;
}
else if (evt.commandName == s_RestartEvent.commandName)
{
Stop();
Play();
return true;
}
else if (evt.commandName == s_ResimulationEvent.commandName)
{
ParticleSystemEditorUtils.resimulation = !ParticleSystemEditorUtils.resimulation;
return true;
}
else if (evt.commandName == s_ShowBoundsEvent.commandName)
{
m_ShowBounds = !m_ShowBounds;
return true;
}
else if (evt.commandName == s_ShowOnlySelectedEvent.commandName)
{
m_ShowOnlySelected = !m_ShowOnlySelected;
return true;
}
else if (evt.commandName == s_ForwardBeginEvent.commandName)
{
m_ScrubForward = true;
return true;
}
else if (evt.commandName == s_ForwardEndEvent.commandName)
{
m_ScrubForward = false;
if (!m_ScrubReverse)
ParticleSystemEditorUtils.playbackIsScrubbing = false;
return true;
}
else if (evt.commandName == s_ReverseBeginEvent.commandName)
{
m_ScrubReverse = true;
return true;
}
else if (evt.commandName == s_ReverseEndEvent.commandName)
{
m_ScrubReverse = false;
if (!m_ScrubForward)
ParticleSystemEditorUtils.playbackIsScrubbing = false;
return true;
}
return false;
}
private void HandleKeyboardShortcuts()
{
var evt = Event.current;
if (evt.type == EventType.ExecuteCommand)
{
if (HandleShortcutEvent(evt))
evt.Use();
}
HandleScrubbing();
}
internal static bool IsStopped(ParticleSystem root)
{
return (!ParticleSystemEditorUtils.playbackIsPlaying && !ParticleSystemEditorUtils.playbackIsPaused) && !ParticleSystemEditorUtils.playbackIsScrubbing;
}
internal bool IsPaused()
{
return !IsPlaying() && !IsStopped(ParticleSystemEditorUtils.GetRoot(m_SelectedParticleSystems[0]));
}
internal bool IsPlaying()
{
return ParticleSystemEditorUtils.playbackIsPlaying;
}
internal void Play()
{
bool anyPlayed = false;
foreach (ParticleSystem ps in m_SelectedParticleSystems)
{
ParticleSystem root = ParticleSystemEditorUtils.GetRoot(ps);
if (root)
{
root.Play();
anyPlayed = true;
}
}
if (anyPlayed)
{
ParticleSystemEditorUtils.playbackIsScrubbing = false;
m_Owner.Repaint();
}
}
internal void Pause()
{
bool anyPaused = false;
foreach (ParticleSystem ps in m_SelectedParticleSystems)
{
ParticleSystem root = ParticleSystemEditorUtils.GetRoot(ps);
if (root)
{
root.Pause();
anyPaused = true;
}
}
if (anyPaused)
{
ParticleSystemEditorUtils.playbackIsScrubbing = true;
m_Owner.Repaint();
}
}
internal void Stop()
{
ParticleSystemEditorUtils.playbackIsScrubbing = false;
ParticleSystemEditorUtils.playbackTime = 0.0F;
ParticleSystemEffectUtils.StopEffect();
m_Owner.Repaint();
}
internal void PlayStopGUI()
{
if (s_Texts == null)
s_Texts = new Texts();
Event evt = Event.current;
if (evt.type == EventType.Layout)
m_TimeHelper.Update();
bool disablePlayButton = (Time.timeScale == 0.0f);
GUIContent playText = disablePlayButton ? s_Texts.playDisabled : s_Texts.play;
if (!EditorApplication.isPlaying)
{
// Edit Mode: Play/Stop buttons
GUILayout.BeginHorizontal(GUILayout.Width(220.0f));
{
using (new EditorGUI.DisabledScope(disablePlayButton))
{
bool isPlaying = ParticleSystemEditorUtils.playbackIsPlaying && !ParticleSystemEditorUtils.playbackIsPaused && !disablePlayButton;
if (GUILayout.Button(isPlaying ? s_Texts.pause : playText, "ButtonLeft"))
{
if (isPlaying)
Pause();
else
Play();
}
}
if (GUILayout.Button(s_Texts.restart, "ButtonMid"))
{
Stop();
Play();
}
if (GUILayout.Button(s_Texts.stop, "ButtonRight"))
{
Stop();
}
}
GUILayout.EndHorizontal();
}
else
{
// Play mode: we only handle play/stop (due to problems with determining if a system with subemitters is playing we cannot pause)
GUILayout.BeginHorizontal();
{
using (new EditorGUI.DisabledScope(disablePlayButton))
{
if (GUILayout.Button(playText))
{
Stop();
Play();
}
}
if (GUILayout.Button(s_Texts.stop))
{
Stop();
}
}
GUILayout.EndHorizontal();
}
// Playback info
PlayBackInfoGUI(EditorApplication.isPlaying);
// Handle shortcut keys last so we do not activate them if inputfield has used the event
HandleKeyboardShortcuts();
}
private void InspectorParticleSystemGUI()
{
GUILayout.BeginVertical(ParticleSystemStyles.Get().effectBgStyle);
{
ParticleSystem selectedSystem = (m_SelectedParticleSystems.Count > 0) ? m_SelectedParticleSystems[0] : null;
if (selectedSystem != null)
{
ParticleSystemUI psUI = m_Emitters.FirstOrDefault(o => o.m_ParticleSystems[0] == selectedSystem);
if (psUI != null)
{
float width = GUIClip.visibleRect.width - 18; // -10 is effect_bg padding, -8 is inspector padding
psUI.OnGUI(width, false);
}
}
}
GUILayout.EndVertical();
GUILayout.FlexibleSpace();
HandleKeyboardShortcuts();