ParticleManager.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. 
  2. // =================================
  3. // Namespaces.
  4. // =================================
  5. using UnityEngine;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. // =================================
  9. // Define namespace.
  10. // =================================
  11. namespace MirzaBeig
  12. {
  13. namespace ParticleSystems
  14. {
  15. namespace Demos
  16. {
  17. // =================================
  18. // Classes.
  19. // =================================
  20. public class ParticleManager : MonoBehaviour
  21. {
  22. // =================================
  23. // Nested classes and structures.
  24. // =================================
  25. // =================================
  26. // Variables.
  27. // =================================
  28. protected List<ParticleSystems> particlePrefabs;
  29. public int currentParticlePrefabIndex;
  30. // Used for adding prefabs from the project view for
  31. // live testing while playing in editor. When finished,
  32. // add them to the hierarchy and remove them from this
  33. // list.
  34. public List<ParticleSystems> particlePrefabsAppend;
  35. // Take only the part of the prefab name string after these many underscores.
  36. public int prefabNameUnderscoreCountCutoff = 4;
  37. // Since I may have prefabs as children I was using to set values.
  38. // But I don't want to disable/enable them each time I want to run
  39. // the build or change values. This will auto-disable all at start.
  40. public bool disableChildrenAtStart = true;
  41. // Already initialized?
  42. bool initialized = false;
  43. // =================================
  44. // Functions.
  45. // =================================
  46. // ...
  47. public void Init()
  48. {
  49. // Default.
  50. //currentParticlePrefab = 0;
  51. // Get all particles.
  52. particlePrefabs = GetComponentsInChildren<ParticleSystems>(true).ToList();
  53. particlePrefabs.AddRange(particlePrefabsAppend);
  54. // Disable all particle prefab object children.
  55. if (disableChildrenAtStart)
  56. {
  57. for (int i = 0; i < particlePrefabs.Count; i++)
  58. {
  59. particlePrefabs[i].gameObject.SetActive(false);
  60. }
  61. }
  62. initialized = true;
  63. }
  64. // ...
  65. protected virtual void Awake()
  66. {
  67. }
  68. // ...
  69. protected virtual void Start()
  70. {
  71. if (initialized)
  72. {
  73. Init();
  74. }
  75. }
  76. // ...
  77. public virtual void Next()
  78. {
  79. currentParticlePrefabIndex++;
  80. if (currentParticlePrefabIndex > particlePrefabs.Count - 1)
  81. {
  82. currentParticlePrefabIndex = 0;
  83. }
  84. }
  85. public virtual void Previous()
  86. {
  87. currentParticlePrefabIndex--;
  88. if (currentParticlePrefabIndex < 0)
  89. {
  90. currentParticlePrefabIndex = particlePrefabs.Count - 1;
  91. }
  92. }
  93. // ...
  94. public string GetCurrentPrefabName(bool shorten = false)
  95. {
  96. // Save object name for clarity.
  97. string particleSystemName = particlePrefabs[currentParticlePrefabIndex].name;
  98. // Only take name from after the last underscore to the end.
  99. if (shorten)
  100. {
  101. int lastIndexOfUnderscore = 0;
  102. for (int i = 0; i < prefabNameUnderscoreCountCutoff; i++)
  103. {
  104. // -1 if not found, 0 to n otherwise. +1 to move position forward.
  105. lastIndexOfUnderscore = particleSystemName.IndexOf("_", lastIndexOfUnderscore) + 1;
  106. // Not found. -1 + 1 == 0.
  107. if (lastIndexOfUnderscore == 0)
  108. {
  109. // "Error"... sort of.
  110. print("Iteration of underscore not found.");
  111. break;
  112. }
  113. }
  114. particleSystemName = particleSystemName.Substring(lastIndexOfUnderscore, particleSystemName.Length - lastIndexOfUnderscore);
  115. }
  116. // Return display text.
  117. return "PARTICLE SYSTEM: #" + (currentParticlePrefabIndex + 1).ToString("00") +
  118. " / " + particlePrefabs.Count.ToString("00") + " (" + particleSystemName + ")";
  119. }
  120. // ...
  121. public virtual int GetParticleCount()
  122. {
  123. return 0;
  124. }
  125. // ...
  126. protected virtual void Update()
  127. {
  128. }
  129. // =================================
  130. // End functions.
  131. // =================================
  132. }
  133. // =================================
  134. // End namespace.
  135. // =================================
  136. }
  137. }
  138. }
  139. // =================================
  140. // --END-- //
  141. // =================================