ETFXEffectCycler.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using UnityEngine;
  2. using UnityEngine.EventSystems;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. namespace EpicToonFX
  6. {
  7. public class ETFXEffectCycler : MonoBehaviour
  8. {
  9. public List<GameObject> listOfEffects;
  10. int effectIndex = 0;
  11. [Header("Spawn Settings")]
  12. [SerializeField]
  13. [Space(10)]
  14. public float loopLength = 1.0f;
  15. public float startDelay = 1.0f;
  16. public bool disableLights = true;
  17. public bool disableSound = true;
  18. void Start ()
  19. {
  20. Invoke("PlayEffect", startDelay);
  21. }
  22. public void PlayEffect()
  23. {
  24. StartCoroutine("EffectLoop");
  25. if (effectIndex < listOfEffects.Count - 1)
  26. {
  27. effectIndex++;
  28. }
  29. else
  30. {
  31. effectIndex = 0;
  32. }
  33. }
  34. private IEnumerator EffectLoop()
  35. {
  36. GameObject instantiatedEffect = (GameObject) Instantiate(listOfEffects[effectIndex], transform.position, transform.rotation * Quaternion.Euler (0, 0, 0));
  37. if (disableLights && instantiatedEffect.GetComponent<Light>())
  38. {
  39. instantiatedEffect.GetComponent<Light>().enabled = false;
  40. }
  41. if (disableSound && instantiatedEffect.GetComponent<AudioSource>())
  42. {
  43. instantiatedEffect.GetComponent<AudioSource>().enabled = false;
  44. }
  45. yield return new WaitForSeconds(loopLength);
  46. Destroy(instantiatedEffect);
  47. PlayEffect();
  48. }
  49. }
  50. }