ParticleEffectsLibrary.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace ETFXPEL
  5. {
  6. public class ParticleEffectsLibrary : MonoBehaviour {
  7. public static ParticleEffectsLibrary GlobalAccess;
  8. void Awake () {
  9. GlobalAccess = this;
  10. currentActivePEList = new List<Transform> ();
  11. TotalEffects = ParticleEffectPrefabs.Length;
  12. CurrentParticleEffectNum = 1;
  13. // Warn About Lengths of Arrays not matching
  14. if (ParticleEffectSpawnOffsets.Length != TotalEffects) {
  15. Debug.LogError ("ParticleEffectsLibrary-ParticleEffectSpawnOffset: Not all arrays match length, double check counts.");
  16. }
  17. if (ParticleEffectPrefabs.Length != TotalEffects) {
  18. Debug.LogError ("ParticleEffectsLibrary-ParticleEffectPrefabs: Not all arrays match length, double check counts.");
  19. }
  20. // Setup Starting PE Name String
  21. effectNameString = ParticleEffectPrefabs [CurrentParticleEffectIndex].name + " (" + CurrentParticleEffectNum.ToString() + " of " + TotalEffects.ToString() + ")";
  22. }
  23. // Stores total number of effects in arrays - NOTE: All Arrays must match length.
  24. public int TotalEffects = 0;
  25. public int CurrentParticleEffectIndex = 0;
  26. public int CurrentParticleEffectNum = 0;
  27. // public string[] ParticleEffectDisplayNames;
  28. public Vector3[] ParticleEffectSpawnOffsets;
  29. // How long until Particle Effect is Destroyed - 0 = never
  30. public float[] ParticleEffectLifetimes;
  31. public GameObject[] ParticleEffectPrefabs;
  32. // Storing for deleting if looping particle effect
  33. #pragma warning disable 414
  34. private string effectNameString = "";
  35. #pragma warning disable 414
  36. private List<Transform> currentActivePEList;
  37. void Start () {
  38. }
  39. public string GetCurrentPENameString() {
  40. return ParticleEffectPrefabs [CurrentParticleEffectIndex].name + " (" + CurrentParticleEffectNum.ToString() + " of " + TotalEffects.ToString() + ")";
  41. }
  42. public void PreviousParticleEffect() {
  43. // Destroy Looping Particle Effects
  44. if (ParticleEffectLifetimes [CurrentParticleEffectIndex] == 0) {
  45. if (currentActivePEList.Count > 0) {
  46. for (int i = 0; i < currentActivePEList.Count; i++) {
  47. if (currentActivePEList [i] != null) {
  48. Destroy (currentActivePEList [i].gameObject);
  49. }
  50. }
  51. currentActivePEList.Clear ();
  52. }
  53. }
  54. // Select Previous Particle Effect
  55. if (CurrentParticleEffectIndex > 0) {
  56. CurrentParticleEffectIndex -= 1;
  57. } else {
  58. CurrentParticleEffectIndex = TotalEffects - 1;
  59. }
  60. CurrentParticleEffectNum = CurrentParticleEffectIndex + 1;
  61. // Update PE Name String
  62. effectNameString = ParticleEffectPrefabs [CurrentParticleEffectIndex].name + " (" + CurrentParticleEffectNum.ToString() + " of " + TotalEffects.ToString() + ")";
  63. }
  64. public void NextParticleEffect() {
  65. // Destroy Looping Particle Effects
  66. if (ParticleEffectLifetimes [CurrentParticleEffectIndex] == 0) {
  67. if (currentActivePEList.Count > 0) {
  68. for (int i = 0; i < currentActivePEList.Count; i++) {
  69. if (currentActivePEList [i] != null) {
  70. Destroy (currentActivePEList [i].gameObject);
  71. }
  72. }
  73. currentActivePEList.Clear ();
  74. }
  75. }
  76. // Select Next Particle Effect
  77. if (CurrentParticleEffectIndex < TotalEffects - 1) {
  78. CurrentParticleEffectIndex += 1;
  79. } else {
  80. CurrentParticleEffectIndex = 0;
  81. }
  82. CurrentParticleEffectNum = CurrentParticleEffectIndex + 1;
  83. // Update PE Name String
  84. effectNameString = ParticleEffectPrefabs [CurrentParticleEffectIndex].name + " (" + CurrentParticleEffectNum.ToString() + " of " + TotalEffects.ToString() + ")";
  85. }
  86. private Vector3 spawnPosition = Vector3.zero;
  87. public void SpawnParticleEffect(Vector3 positionInWorldToSpawn) {
  88. // Spawn Currently Selected Particle Effect
  89. spawnPosition = positionInWorldToSpawn + ParticleEffectSpawnOffsets[CurrentParticleEffectIndex];
  90. GameObject newParticleEffect = GameObject.Instantiate(ParticleEffectPrefabs[CurrentParticleEffectIndex], spawnPosition, ParticleEffectPrefabs[CurrentParticleEffectIndex].transform.rotation) as GameObject;
  91. newParticleEffect.name = "PE_" + ParticleEffectPrefabs[CurrentParticleEffectIndex];
  92. // Store Looping Particle Effects Systems
  93. if (ParticleEffectLifetimes [CurrentParticleEffectIndex] == 0) {
  94. currentActivePEList.Add (newParticleEffect.transform);
  95. }
  96. currentActivePEList.Add(newParticleEffect.transform);
  97. // Destroy Particle Effect After Lifetime expired
  98. if (ParticleEffectLifetimes [CurrentParticleEffectIndex] != 0) {
  99. Destroy(newParticleEffect, ParticleEffectLifetimes[CurrentParticleEffectIndex]);
  100. }
  101. }
  102. }
  103. }