DestroyObjectDelayed.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace AdonGameKit
  5. {
  6. /// <summary>
  7. /// 延迟销毁
  8. /// </summary>
  9. public class DestroyObjectDelayed : MonoBehaviour
  10. {
  11. public float m_Delay = 5;
  12. public bool m_DelayDespawn;///粒子延迟淡出再销毁
  13. // Start is called before the first frame update
  14. ParticleSystem[] particles;//粒子系统数组
  15. void Awake()
  16. {
  17. particles = GetComponentsInChildren<ParticleSystem>();
  18. }
  19. void Start()
  20. {
  21. // n秒后销毁当前gameobject
  22. Invoke("DestroyObject", m_Delay);
  23. }
  24. void DestroyObject()
  25. {
  26. if(m_DelayDespawn)
  27. {
  28. Delay();
  29. }
  30. else
  31. {
  32. Destroy(gameObject);
  33. }
  34. }
  35. /// <summary>
  36. /// 停止粒子释放并逐渐消逝
  37. /// </summary>
  38. void Delay()
  39. {
  40. if (particles.Length > 0)
  41. {
  42. for (int i = 0; i < particles.Length; i++)
  43. {
  44. particles[i].Stop(false);
  45. }
  46. }
  47. Destroy(gameObject,2);
  48. }
  49. }
  50. }