AutoDestroyAfterAudioPlay.cs 599 B

123456789101112131415161718192021222324252627282930313233343536
  1. using System;
  2. using UnityEngine;
  3. [RequireComponent(typeof(AudioSource))]
  4. public class AutoDestroyAfterAudioPlay : MonoBehaviour
  5. {
  6. private void Awake()
  7. {
  8. this._source = base.GetComponent<AudioSource>();
  9. }
  10. private void Update()
  11. {
  12. if (!this._source.isPlaying)
  13. {
  14. this.Disable();
  15. }
  16. }
  17. private void OnDisable()
  18. {
  19. this.Disable();
  20. }
  21. private void Disable()
  22. {
  23. base.transform.localPosition = Vector3.zero;
  24. this._source.Stop();
  25. this._source.clip = null;
  26. }
  27. [SerializeField]
  28. private float delayTime = 0.5f;
  29. private AudioSource _source;
  30. }