AutoDestroy.cs 732 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. using System.Diagnostics;
  3. using UnityEngine;
  4. public class AutoDestroy : MonoBehaviour
  5. {
  6. [HideInInspector]
  7. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  8. public event Action OnDestroy;
  9. private void Start()
  10. {
  11. this.startTime = Time.time;
  12. }
  13. private void Update()
  14. {
  15. if (Time.time - this.startTime > this.destroyTime)
  16. {
  17. this.DestroySelf();
  18. }
  19. }
  20. private void DestroySelf()
  21. {
  22. if (!this.destroyed)
  23. {
  24. UnityEngine.Object.Destroy(base.gameObject);
  25. if (this.OnDestroy != null)
  26. {
  27. this.OnDestroy();
  28. }
  29. this.destroyed = true;
  30. }
  31. }
  32. [SerializeField]
  33. public float destroyTime = 2f;
  34. private float startTime;
  35. private bool destroyed;
  36. }