SoundDeparent.cs 950 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: Unparents this object and optionally destroys it after the sound
  4. // has played
  5. //
  6. //=============================================================================
  7. using UnityEngine;
  8. using System.Collections;
  9. namespace Valve.VR.InteractionSystem
  10. {
  11. //-------------------------------------------------------------------------
  12. public class SoundDeparent : MonoBehaviour
  13. {
  14. public bool destroyAfterPlayOnce = true;
  15. private AudioSource thisAudioSource;
  16. //-------------------------------------------------
  17. void Awake()
  18. {
  19. thisAudioSource = GetComponent<AudioSource>();
  20. }
  21. //-------------------------------------------------
  22. void Start()
  23. {
  24. // move the sound object out from under the parent
  25. gameObject.transform.parent = null;
  26. if ( destroyAfterPlayOnce )
  27. Destroy( gameObject, thisAudioSource.clip.length );
  28. }
  29. }
  30. }