ETFXEffectController.cs 4.4 KB

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