ParticlePlayback.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. 
  2. // =================================
  3. // Namespaces.
  4. // =================================
  5. using UnityEngine;
  6. using UnityEditor;
  7. using System.Collections.Generic;
  8. // =================================
  9. // Define namespace.
  10. // =================================
  11. namespace MirzaBeig
  12. {
  13. namespace EditorExtensions
  14. {
  15. namespace Utilities
  16. {
  17. // =================================
  18. // Classes.
  19. // =================================
  20. public class ParticlePlayback
  21. {
  22. // =================================
  23. // Nested classes and structures.
  24. // =================================
  25. // ...
  26. public enum ParticlePlaybackState
  27. {
  28. NULL,
  29. Playing, Paused, Stopped
  30. }
  31. // =================================
  32. // Variables.
  33. // =================================
  34. // Button sizes.
  35. public float playbackButtonHeight = 25.0f;
  36. // Selected objects in editor and all the particle systems components.
  37. //List<ParticleSystem> particleSystems = new List<ParticleSystem>();
  38. // Playback.
  39. public float particlePlaybackPosition = 0.0f;
  40. public float particlePlaybackPositionSliderMax = 2.0f;
  41. // Sets the entire particle system set to a single randomized value.
  42. bool randomizeParticleSeed = false;
  43. // Sets different random seeds for each particle system.
  44. bool randomizeMultiLevelParticleSeed = false;
  45. int particleRandomSeed = 0;
  46. // Because of interval jumps when adjusting the slider,
  47. // the value Unity applies to the slider tends to overflow
  48. // the actual maximum and resets itself.
  49. // Simple solution: I limit the max to half.
  50. // Downside? It's only HALF as random as before... which doesn't even make a difference.
  51. // EDIT: There's another issue where calling inSlider with a large value
  52. // causes it to change slightly when passed through itself.
  53. // The issue was apparent when using the randomize button.
  54. // Setting it to max / 32 seems to solve the problem...
  55. const int intSliderMaxValue = int.MaxValue / 32;
  56. // Clear all particles on screen from selection when selection changes?
  57. public bool clearParticlesOnSelectionChange = false;
  58. // Button pressed state.
  59. ParticlePlaybackState particlePlaybackState = ParticlePlaybackState.NULL;
  60. // For calculating deltaTime in Update().
  61. float lastUpdateTime = 0.0f;
  62. // Active editor windows.
  63. EditorWindow gameView;
  64. EditorWindow sceneView;
  65. // Scrolling view position for selected objects.
  66. Vector2 scrollPosition;
  67. // For labeling and tooltips.
  68. GUIContent guiContentLabel;
  69. // =================================
  70. // Functions.
  71. // =================================
  72. // ...
  73. // Return an editor window by name.
  74. public EditorWindow getEditorWindow(string name)
  75. {
  76. System.Reflection.Assembly assembly = typeof(UnityEditor.EditorWindow).Assembly;
  77. Object[] editorWindows = Resources.FindObjectsOfTypeAll(assembly.GetType(name));
  78. // If window doesn't exist, GetWindow() creates it.
  79. // This is not what I want. So if the window doesn't exist, skip this.
  80. if (editorWindows.Length != 0)
  81. {
  82. return EditorWindow.GetWindow(assembly.GetType(name), false, null, false);
  83. }
  84. return null;
  85. }
  86. // Update/refresh/repaint all NON-EDITOR windows.
  87. public void repaintEditorCameraWindows()
  88. {
  89. if (gameView)
  90. {
  91. gameView.Repaint();
  92. }
  93. if (sceneView)
  94. {
  95. sceneView.Repaint();
  96. }
  97. }
  98. // ...
  99. public void updateEditorCameraWindowReferences()
  100. {
  101. gameView = getEditorWindow("UnityEditor.GameView");
  102. sceneView = getEditorWindow("UnityEditor.SceneView");
  103. }
  104. // ...
  105. public void pause(List<ParticleSystem> particleSystems)
  106. {
  107. lastUpdateTime = 0.0f;
  108. for (int i = 0; i < particleSystems.Count; i++)
  109. {
  110. particleSystems[i].Pause(false);
  111. }
  112. }
  113. // Simulates full-circle back to current position.
  114. public void loopback(ParticleSystem particleSystem)
  115. {
  116. // Lock random seed.
  117. bool autoRandomSeed = particleSystem.useAutoRandomSeed;
  118. // Save state before set.
  119. ParticlePlaybackState state;
  120. if (particleSystem.isPlaying)
  121. {
  122. state = ParticlePlaybackState.Playing;
  123. }
  124. else if (particleSystem.isPaused)
  125. {
  126. state = ParticlePlaybackState.Paused;
  127. }
  128. else
  129. {
  130. state = ParticlePlaybackState.Stopped;
  131. }
  132. // DON'T RESTART the simulation.
  133. // Instead, stop, play, then simulate to time.
  134. // Keep last false in Simulate to prevent restarts.
  135. // Else, particles will pop in and out...
  136. // Also requires clear in that case.
  137. particleSystem.Stop(false);
  138. particleSystem.Clear(false);
  139. particleSystem.randomSeed = (uint)particleRandomSeed;
  140. particleSystem.Play(false);
  141. particleSystem.Simulate(particlePlaybackPosition, false, false);
  142. // Resume from saved playback state.
  143. particleSystem.Pause();
  144. particleSystem.useAutoRandomSeed = autoRandomSeed;
  145. switch (state)
  146. {
  147. case ParticlePlaybackState.Playing:
  148. {
  149. particleSystem.Play(false);
  150. break;
  151. }
  152. case ParticlePlaybackState.Paused:
  153. {
  154. particleSystem.Pause(false);
  155. break;
  156. }
  157. case ParticlePlaybackState.Stopped:
  158. {
  159. particleSystem.Stop(false);
  160. break;
  161. }
  162. }
  163. }
  164. // ...
  165. public void GUIPlaybackSettings(List<ParticleSystem> particleSystems)
  166. {
  167. // Get windows.
  168. updateEditorCameraWindowReferences();
  169. // Playback settings.
  170. EditorGUILayout.LabelField("- Playback Settings:", EditorStyles.boldLabel);
  171. EditorGUILayout.Separator();
  172. // Timer options.
  173. GUI.enabled = particleSystems.Count != 0;
  174. EditorGUI.BeginChangeCheck();
  175. {
  176. guiContentLabel = new GUIContent("Playback Position (s)", "Playback position of all selected particle systems in seconds.");
  177. particlePlaybackPosition = EditorGUILayout.Slider(guiContentLabel, particlePlaybackPosition, 0.0f, particlePlaybackPositionSliderMax);
  178. }
  179. // Only update if slider changed.
  180. if (EditorGUI.EndChangeCheck())
  181. {
  182. // Set playback position.
  183. for (int i = 0; i < particleSystems.Count; i++)
  184. {
  185. loopback(particleSystems[i]);
  186. }
  187. // Refresh.
  188. repaintEditorCameraWindows();
  189. }
  190. GUI.enabled = true;
  191. EditorGUILayout.Separator();
  192. guiContentLabel = new GUIContent("Playback Position Max (s)", "Playback position maximum value in seconds.");
  193. particlePlaybackPositionSliderMax = EditorGUILayout.Slider(guiContentLabel, particlePlaybackPositionSliderMax, 0.0f, 32.0f);
  194. EditorGUILayout.Separator();
  195. // Playback buttons.
  196. GUI.enabled = particleSystems.Count != 0;
  197. EditorGUILayout.BeginHorizontal();
  198. {
  199. guiContentLabel = new GUIContent("Play",
  200. "Play all selected particles.");
  201. if (GUILayout.Button(guiContentLabel, GUILayout.Height(playbackButtonHeight)))
  202. {
  203. if (randomizeParticleSeed)
  204. {
  205. particleRandomSeed = Random.Range(0, intSliderMaxValue);
  206. }
  207. for (int i = 0; i < particleSystems.Count; i++)
  208. {
  209. if (randomizeParticleSeed && randomizeMultiLevelParticleSeed)
  210. {
  211. particleRandomSeed = Random.Range(0, intSliderMaxValue);
  212. }
  213. bool autoRandomSeed = particleSystems[i].useAutoRandomSeed;
  214. particleSystems[i].randomSeed = (uint)particleRandomSeed;
  215. particleSystems[i].useAutoRandomSeed = autoRandomSeed;
  216. if (particlePlaybackState != ParticlePlaybackState.Paused)
  217. {
  218. particleSystems[i].Stop(false);
  219. particleSystems[i].Clear(false);
  220. }
  221. particleSystems[i].Play(false);
  222. }
  223. particlePlaybackState = ParticlePlaybackState.Playing;
  224. }
  225. guiContentLabel = new GUIContent("Pause",
  226. "Pause all selected particles.");
  227. if (GUILayout.Button(guiContentLabel, GUILayout.Height(playbackButtonHeight)))
  228. {
  229. for (int i = 0; i < particleSystems.Count; i++)
  230. {
  231. particleSystems[i].Pause(false);
  232. lastUpdateTime = 0.0f;
  233. }
  234. particlePlaybackState = ParticlePlaybackState.Paused;
  235. }
  236. guiContentLabel = new GUIContent("Stop",
  237. "Stop all selected particles.");
  238. if (GUILayout.Button(guiContentLabel, GUILayout.Height(playbackButtonHeight)))
  239. {
  240. for (int i = 0; i < particleSystems.Count; i++)
  241. {
  242. particleSystems[i].Stop(false);
  243. particleSystems[i].Clear(false);
  244. lastUpdateTime = 0.0f;
  245. }
  246. particlePlaybackState = ParticlePlaybackState.Stopped;
  247. }
  248. }
  249. EditorGUILayout.EndHorizontal();
  250. //EditorGUILayout.Separator();
  251. // Button to clear all particles.
  252. guiContentLabel = new GUIContent("Clear",
  253. "Clear all particles on screen.");
  254. if (GUILayout.Button(guiContentLabel, GUILayout.Height(playbackButtonHeight)))
  255. {
  256. for (int i = 0; i < particleSystems.Count; i++)
  257. {
  258. particleSystems[i].Clear(false);
  259. particleSystems[i].Stop();
  260. }
  261. repaintEditorCameraWindows();
  262. }
  263. GUI.enabled = true;
  264. EditorGUILayout.Separator();
  265. // Display playback state.
  266. //EditorGUILayout.HelpBox("> " + particlePlaybackState.ToString(), MessageType.None);
  267. //EditorGUILayout.Separator();
  268. // Particle seed options.
  269. guiContentLabel = new GUIContent("Randomize Seed", "Set all particle systems' seed to a single random value.");
  270. randomizeParticleSeed = EditorGUILayout.Toggle(guiContentLabel, randomizeParticleSeed);
  271. GUI.enabled = randomizeParticleSeed;
  272. guiContentLabel = new GUIContent("Multi-Level Random Seed", "Randomize particle seed per system.");
  273. randomizeMultiLevelParticleSeed = EditorGUILayout.Toggle(guiContentLabel, randomizeMultiLevelParticleSeed);
  274. GUI.enabled = !randomizeParticleSeed;
  275. EditorGUILayout.Separator();
  276. EditorGUI.BeginChangeCheck();
  277. {
  278. guiContentLabel = new GUIContent("Particle Random Seed", "Set the \"random\" particle seed. 0 == auto-randomize on awake.");
  279. particleRandomSeed = EditorGUILayout.IntSlider(guiContentLabel, particleRandomSeed, 0, intSliderMaxValue);
  280. }
  281. if (EditorGUI.EndChangeCheck())
  282. {
  283. for (int i = 0; i < particleSystems.Count; i++)
  284. {
  285. loopback(particleSystems[i]);
  286. }
  287. }
  288. // Randomize seed.
  289. guiContentLabel = new GUIContent("Randomize Seed",
  290. "Generate a random seed value for the slider.");
  291. if (GUILayout.Button(guiContentLabel, GUILayout.Height(playbackButtonHeight)))
  292. {
  293. particleRandomSeed = Random.Range(0, intSliderMaxValue);
  294. for (int i = 0; i < particleSystems.Count; i++)
  295. {
  296. loopback(particleSystems[i]);
  297. }
  298. }
  299. GUI.enabled = true;
  300. EditorGUILayout.Separator();
  301. // Clear emitted particles on-screen from selection on selection change?
  302. guiContentLabel = new GUIContent("Auto-Clear Particles", "Clear all emitted particles from previous selection on selection change.");
  303. clearParticlesOnSelectionChange = EditorGUILayout.Toggle(guiContentLabel, clearParticlesOnSelectionChange);
  304. }
  305. // ...
  306. public void GUIParticleSelection(List<GameObject> selectedGameObjectsWithParticleSystems)
  307. {
  308. EditorGUILayout.Separator();
  309. // Selected objects.
  310. guiContentLabel = new GUIContent("- Selected:",
  311. "Selected GameObjects must be active and contain at least one active ParticleSystem component in their hierarchy.");
  312. EditorGUILayout.LabelField(guiContentLabel, EditorStyles.boldLabel);
  313. EditorGUILayout.Separator();
  314. scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
  315. {
  316. if (selectedGameObjectsWithParticleSystems.Count == 0)
  317. {
  318. EditorGUILayout.LabelField("Please select GameObjects with at least one ParticleSystem component.", EditorStyles.miniBoldLabel);
  319. }
  320. else
  321. {
  322. // Selection will be ordered based on scene hierarchy.
  323. // Reverse here, else they will be listed in reverse.
  324. for (int i = selectedGameObjectsWithParticleSystems.Count - 1; i != -1; i--)
  325. {
  326. EditorGUILayout.BeginHorizontal();
  327. {
  328. EditorGUILayout.LabelField("> " + selectedGameObjectsWithParticleSystems[i].name, EditorStyles.miniLabel);
  329. }
  330. EditorGUILayout.EndHorizontal();
  331. }
  332. }
  333. }
  334. EditorGUILayout.EndScrollView();
  335. }
  336. // ...
  337. public void update(List<ParticleSystem> particleSystems)
  338. {
  339. if (particleSystems.Count != 0 &&
  340. particlePlaybackState == ParticlePlaybackState.Playing)
  341. {
  342. // Get windows.
  343. //updateEditorCameraWindowReferences();
  344. // Calculate time passed since last call to function.
  345. float deltaTime = Time.realtimeSinceStartup - lastUpdateTime;
  346. // Force game view to refresh if not in play mode
  347. // so ALL particles will play (and not just selected ones).
  348. if (!Application.isPlaying && lastUpdateTime != 0.0f)
  349. {
  350. for (int i = 0; i < particleSystems.Count; i++)
  351. {
  352. particleSystems[i].Simulate(deltaTime, false, false);
  353. }
  354. // Update slider.
  355. //particlePlaybackPosition += deltaTime;
  356. // Update window displays.
  357. repaintEditorCameraWindows();
  358. //switch (particlePlaybackState)
  359. //{
  360. // case ParticlePlaybackState.Playing:
  361. // {
  362. // for (int i = 0; i < particleSystems.Count; i++)
  363. // {
  364. // particleSystems[i].Simulate(1.0f / 60.0f, false, false);
  365. // }
  366. // repaintCameraViews();
  367. // break;
  368. // }
  369. // case ParticlePlaybackState.Paused:
  370. // {
  371. // break;
  372. // }
  373. // default:
  374. // {
  375. // break;
  376. // }
  377. //}
  378. }
  379. // Set to time at end of function.
  380. lastUpdateTime = Time.realtimeSinceStartup;
  381. }
  382. }
  383. // =================================
  384. // End functions.
  385. // =================================
  386. }
  387. // =================================
  388. // End namespace.
  389. // =================================
  390. }
  391. }
  392. }
  393. // =================================
  394. // --END-- //
  395. // =================================