TempAudioObject.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// After Setup() is called, plays a sound file once and then destroys itself.
  6. /// Used by ZEDXRGrabber to play a sound file that will continue to play even if it's disabled.
  7. /// <para>To use, instantiate a gameObject and put this on it (or use a prefab) and call Setup() with the clip to be played.</para>
  8. /// </summary>
  9. public class TempAudioObject : MonoBehaviour
  10. {
  11. private AudioSource source;
  12. private bool isSetup = false;
  13. /// <summary>
  14. /// Tells this object which clip to play, and causes it to be destroyed as soon as it's done playing.
  15. /// </summary>
  16. public void Setup(AudioClip clip)
  17. {
  18. source = gameObject.AddComponent<AudioSource>();
  19. //source.clip = clip;
  20. source.PlayOneShot(clip);
  21. isSetup = true;
  22. }
  23. /// <summary>
  24. /// If we've started playing, make sure we haven't finished playing it. If we have, destroy this object.
  25. /// </summary>
  26. private void Update()
  27. {
  28. if(isSetup == true && source.isPlaying == false)
  29. {
  30. Destroy(gameObject);
  31. }
  32. }
  33. }