OneshotParticleSystemsManager.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. 
  2. // =================================
  3. // Namespaces.
  4. // =================================
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. using System.Collections.Generic;
  8. // =================================
  9. // Define namespace.
  10. // =================================
  11. namespace MirzaBeig
  12. {
  13. namespace ParticleSystems
  14. {
  15. namespace Demos
  16. {
  17. // =================================
  18. // Classes.
  19. // =================================
  20. public class OneshotParticleSystemsManager : ParticleManager
  21. {
  22. // =================================
  23. // Nested classes and structures.
  24. // =================================
  25. // ...
  26. // =================================
  27. // Variables.
  28. // =================================
  29. public LayerMask mouseRaycastLayerMask;
  30. List<ParticleSystems> spawnedPrefabs;
  31. // Don't allow spawning if true.
  32. // Used for button clicks vs. empty-space clicks.
  33. public bool disableSpawn { get; set; }
  34. // =================================
  35. // Functions.
  36. // =================================
  37. // ...
  38. protected override void Awake()
  39. {
  40. base.Awake();
  41. }
  42. // ...
  43. protected override void Start()
  44. {
  45. base.Start();
  46. // ...
  47. disableSpawn = false;
  48. spawnedPrefabs = new List<ParticleSystems>();
  49. }
  50. // Get rid of spawned systems when re-activated.
  51. void OnEnable()
  52. {
  53. //clear();
  54. }
  55. // ...
  56. public void Clear()
  57. {
  58. if (spawnedPrefabs != null)
  59. {
  60. for (int i = 0; i < spawnedPrefabs.Count; i++)
  61. {
  62. if (spawnedPrefabs[i])
  63. {
  64. Destroy(spawnedPrefabs[i].gameObject);
  65. }
  66. }
  67. spawnedPrefabs.Clear();
  68. }
  69. }
  70. // ...
  71. protected override void Update()
  72. {
  73. base.Update();
  74. }
  75. // ...
  76. public void InstantiateParticlePrefab(Vector2 mousePosition, float maxDistance)
  77. {
  78. if (spawnedPrefabs != null)
  79. {
  80. if (!disableSpawn)
  81. {
  82. Vector3 position = mousePosition;
  83. position.z = maxDistance;
  84. Vector3 worldMousePosition = Camera.main.ScreenToWorldPoint(position);
  85. Vector3 directionToWorldMouse = worldMousePosition - Camera.main.transform.position;
  86. RaycastHit rayHit;
  87. // Start the raycast a little bit ahead of the camera because the camera starts right where a cube's edge is
  88. // and that causes the raycast to hit... spawning a prefab right at the camera position. It's fixed by moving the camera,
  89. // or I can just add this forward to prevent it from happening at all.
  90. Physics.Raycast(Camera.main.transform.position + Camera.main.transform.forward * 0.01f, directionToWorldMouse, out rayHit, maxDistance);
  91. Vector3 spawnPosition;
  92. if (rayHit.collider)
  93. {
  94. spawnPosition = rayHit.point;
  95. }
  96. else
  97. {
  98. spawnPosition = worldMousePosition;
  99. }
  100. ParticleSystems prefab = particlePrefabs[currentParticlePrefabIndex];
  101. ParticleSystems newParticlePrefab = Instantiate(
  102. prefab, spawnPosition, prefab.transform.rotation) as ParticleSystems;
  103. newParticlePrefab.gameObject.SetActive(true);
  104. // Parent to spawner.
  105. newParticlePrefab.transform.parent = transform;
  106. spawnedPrefabs.Add(newParticlePrefab);
  107. }
  108. }
  109. }
  110. // ...
  111. //public void instantiateParticlePrefabRandom()
  112. //{
  113. // if (!disableSpawn)
  114. // {
  115. // instantiateParticlePrefab(new Vector3(
  116. // Random.Range(0.0f, Screen.width), Random.Range(0.0f, Screen.height), 0.0f));
  117. // }
  118. //}
  119. // ...
  120. public void Randomize()
  121. {
  122. currentParticlePrefabIndex = Random.Range(0, particlePrefabs.Count);
  123. }
  124. // Get particle count from all spawned.
  125. public override int GetParticleCount()
  126. {
  127. int pcount = 0;
  128. if (spawnedPrefabs != null)
  129. {
  130. for (int i = 0; i < spawnedPrefabs.Count; i++)
  131. {
  132. if (spawnedPrefabs[i])
  133. {
  134. pcount += spawnedPrefabs[i].getParticleCount();
  135. }
  136. else
  137. {
  138. spawnedPrefabs.RemoveAt(i);
  139. }
  140. }
  141. }
  142. return pcount;
  143. }
  144. // =================================
  145. // End functions.
  146. // =================================
  147. }
  148. // =================================
  149. // End namespace.
  150. // =================================
  151. }
  152. }
  153. }
  154. // =================================
  155. // --END-- //
  156. // =================================