CFX_AutoDestructShuriken.cs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using UnityEngine;
  2. using System.Collections;
  3. // Cartoon FX - (c) 2013, Jean Moreno
  4. // Automatically destructs an object when it has stopped emitting particles and when they have all disappeared from the screen.
  5. // Check is performed every 0.5 seconds to not query the particle system's state every frame.
  6. // (only deactivates the object if the OnlyDeactivate flag is set, automatically used with CFX Spawn System)
  7. [RequireComponent(typeof(ParticleSystem))]
  8. public class CFX_AutoDestructShuriken : MonoBehaviour
  9. {
  10. // If true, deactivate the object instead of destroying it
  11. public bool OnlyDeactivate;
  12. void OnEnable()
  13. {
  14. StartCoroutine("CheckIfAlive");
  15. }
  16. IEnumerator CheckIfAlive ()
  17. {
  18. while(true)
  19. {
  20. yield return new WaitForSeconds(0.5f);
  21. if(!this.GetComponent<ParticleSystem>().IsAlive(true))
  22. {
  23. if(OnlyDeactivate)
  24. {
  25. #if UNITY_3_5
  26. this.gameObject.SetActiveRecursively(false);
  27. #else
  28. this.gameObject.SetActive(false);
  29. #endif
  30. }
  31. else
  32. GameObject.Destroy(this.gameObject);
  33. break;
  34. }
  35. }
  36. }
  37. }