ETFXEffectControllerPooled.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace EpicToonFX
  6. {
  7. public class ETFXEffectControllerPooled : MonoBehaviour
  8. {
  9. public GameObject[] effects;
  10. private List<GameObject> effectsPool;
  11. private int effectIndex = 0;
  12. [Space(10)]
  13. [Header("Spawn Settings")]
  14. public bool disableLights = true;
  15. public bool disableSound = true;
  16. public float startDelay = 0.2f;
  17. public float respawnDelay = 0.5f;
  18. public bool slideshowMode = false;
  19. public bool autoRotation = false;
  20. [Range(0.001f, 0.5f)]
  21. public float autoRotationSpeed = 0.1f;
  22. private GameObject currentEffect;
  23. private Text effectNameText;
  24. private Text effectIndexText;
  25. private ETFXMouseOrbit etfxMouseOrbit;
  26. //Caching components
  27. private void Awake()
  28. {
  29. effectNameText = GameObject.Find("EffectName").GetComponent<Text>();
  30. effectIndexText = GameObject.Find("EffectIndex").GetComponent<Text>();
  31. etfxMouseOrbit = Camera.main.GetComponent<ETFXMouseOrbit>();
  32. etfxMouseOrbit.etfxEffectControllerPooled = this;
  33. //Pooling
  34. effectsPool = new List<GameObject>();
  35. for (int i = 0; i < effects.Length; i++)
  36. {
  37. GameObject effect = Instantiate(effects[i], transform.position, Quaternion.identity);
  38. effect.transform.parent = transform;
  39. effectsPool.Add(effect);
  40. effect.SetActive(false);
  41. }
  42. }
  43. private void Start()
  44. {
  45. Invoke("InitializeLoop", startDelay);
  46. }
  47. private void Update()
  48. {
  49. if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D))
  50. {
  51. NextEffect();
  52. }
  53. if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))
  54. {
  55. PreviousEffect();
  56. }
  57. }
  58. private void FixedUpdate()
  59. {
  60. if (autoRotation)
  61. {
  62. etfxMouseOrbit.SetAutoRotationSpeed(autoRotationSpeed);
  63. if (!etfxMouseOrbit.isAutoRotating)
  64. etfxMouseOrbit.InitializeAutoRotation();
  65. }
  66. }
  67. public void InitializeLoop()
  68. {
  69. StartCoroutine(EffectLoop());
  70. }
  71. public void NextEffect()
  72. {
  73. if (effectIndex < effects.Length - 1)
  74. {
  75. effectIndex++;
  76. }
  77. else
  78. {
  79. effectIndex = 0;
  80. }
  81. CleanCurrentEffect();
  82. }
  83. public void PreviousEffect()
  84. {
  85. if (effectIndex > 0)
  86. {
  87. effectIndex--;
  88. }
  89. else
  90. {
  91. effectIndex = effects.Length - 1;
  92. }
  93. CleanCurrentEffect();
  94. }
  95. private void CleanCurrentEffect()
  96. {
  97. StopAllCoroutines();
  98. if (currentEffect != null)
  99. {
  100. currentEffect.SetActive(false);
  101. }
  102. StartCoroutine(EffectLoop());
  103. }
  104. private IEnumerator EffectLoop()
  105. {
  106. //Pooling effect
  107. currentEffect = effectsPool[effectIndex];
  108. currentEffect.SetActive(true);
  109. if (disableLights && currentEffect.GetComponent<Light>())
  110. {
  111. currentEffect.GetComponent<Light>().enabled = false;
  112. }
  113. if (disableSound && currentEffect.GetComponent<AudioSource>())
  114. {
  115. currentEffect.GetComponent<AudioSource>().enabled = false;
  116. }
  117. //Update UI
  118. effectNameText.text = effects[effectIndex].name;
  119. effectIndexText.text = (effectIndex + 1) + " of " + effects.Length;
  120. ParticleSystem particleSystem = currentEffect.GetComponent<ParticleSystem>();
  121. while (true)
  122. {
  123. yield return new WaitForSeconds(particleSystem.main.duration + respawnDelay);
  124. if (!slideshowMode)
  125. {
  126. if (!particleSystem.main.loop)
  127. {
  128. currentEffect.SetActive(false);
  129. currentEffect.SetActive(true);
  130. }
  131. }
  132. else
  133. {
  134. //Double delay for looping effects
  135. if (particleSystem.main.loop)
  136. {
  137. yield return new WaitForSeconds(respawnDelay);
  138. }
  139. NextEffect();
  140. }
  141. }
  142. }
  143. }
  144. }