PlaySound.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: Plays one of many audio files with possible randomized parameters
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. using System.Collections;
  8. namespace Valve.VR.InteractionSystem
  9. {
  10. //-------------------------------------------------------------------------
  11. [RequireComponent( typeof( AudioSource ) )]
  12. public class PlaySound : MonoBehaviour
  13. {
  14. [Tooltip( "List of audio clips to play." )]
  15. public AudioClip[] waveFile;
  16. [Tooltip( "Stops the currently playing clip in the audioSource. Otherwise clips will overlap/mix." )]
  17. public bool stopOnPlay;
  18. [Tooltip( "After the audio clip finishes playing, disable the game object the sound is on." )]
  19. public bool disableOnEnd;
  20. [Tooltip( "Loop the sound after the wave file variation has been chosen." )]
  21. public bool looping;
  22. [Tooltip( "If the sound is looping and updating it's position every frame, stop the sound at the end of the wav/clip length. " )]
  23. public bool stopOnEnd;
  24. [Tooltip( "Start a wave file playing on awake, but after a delay." )]
  25. public bool playOnAwakeWithDelay;
  26. [Header ( "Random Volume" )]
  27. public bool useRandomVolume = true;
  28. [Tooltip( "Minimum volume that will be used when randomly set." )]
  29. [Range( 0.0f, 1.0f )]
  30. public float volMin = 1.0f;
  31. [Tooltip( "Maximum volume that will be used when randomly set." )]
  32. [Range( 0.0f, 1.0f )]
  33. public float volMax = 1.0f;
  34. [Header ( "Random Pitch" )]
  35. [Tooltip( "Use min and max random pitch levels when playing sounds." )]
  36. public bool useRandomPitch = true;
  37. [Tooltip( "Minimum pitch that will be used when randomly set." )]
  38. [Range( -3.0f, 3.0f )]
  39. public float pitchMin = 1.0f;
  40. [Tooltip( "Maximum pitch that will be used when randomly set." )]
  41. [Range( -3.0f, 3.0f )]
  42. public float pitchMax = 1.0f;
  43. [Header( "Random Time" )]
  44. [Tooltip( "Use Retrigger Time to repeat the sound within a time range" )]
  45. public bool useRetriggerTime = false;
  46. [Tooltip( "Inital time before the first repetion starts" )]
  47. [Range( 0.0f, 360.0f )]
  48. public float timeInitial = 0.0f;
  49. [Tooltip( "Minimum time that will pass before the sound is retriggered" )]
  50. [Range( 0.0f, 360.0f )]
  51. public float timeMin = 0.0f;
  52. [Tooltip( "Maximum pitch that will be used when randomly set." )]
  53. [Range( 0.0f, 360.0f )]
  54. public float timeMax = 0.0f;
  55. [Header ( "Random Silence" )]
  56. [Tooltip( "Use Retrigger Time to repeat the sound within a time range" )]
  57. public bool useRandomSilence = false;
  58. [Tooltip( "Percent chance that the wave file will not play" )]
  59. [Range( 0.0f, 1.0f )]
  60. public float percentToNotPlay = 0.0f;
  61. [Header( "Delay Time" )]
  62. [Tooltip( "Time to offset playback of sound" )]
  63. public float delayOffsetTime = 0.0f;
  64. private AudioSource audioSource;
  65. private AudioClip clip;
  66. //-------------------------------------------------
  67. void Awake()
  68. {
  69. audioSource = GetComponent<AudioSource>();
  70. clip = audioSource.clip;
  71. // audio source play on awake is true, just play the PlaySound immediately
  72. if ( audioSource.playOnAwake )
  73. {
  74. if ( useRetriggerTime )
  75. InvokeRepeating( "Play", timeInitial, Random.Range( timeMin, timeMax ) );
  76. else
  77. Play();
  78. }
  79. // if playOnAwake is false, but the playOnAwakeWithDelay on the PlaySound is true, play the sound on away but with a delay
  80. else if ( !audioSource.playOnAwake && playOnAwakeWithDelay )
  81. {
  82. PlayWithDelay( delayOffsetTime );
  83. if ( useRetriggerTime )
  84. InvokeRepeating( "Play", timeInitial, Random.Range( timeMin, timeMax ) );
  85. }
  86. // in the case where both playOnAwake and playOnAwakeWithDelay are both set to true, just to the same as above, play the sound but with a delay
  87. else if ( audioSource.playOnAwake && playOnAwakeWithDelay )
  88. {
  89. PlayWithDelay( delayOffsetTime );
  90. if ( useRetriggerTime )
  91. InvokeRepeating( "Play", timeInitial, Random.Range( timeMin, timeMax ) );
  92. }
  93. }
  94. //-------------------------------------------------
  95. // Play a random clip from those available
  96. //-------------------------------------------------
  97. public void Play()
  98. {
  99. if ( looping )
  100. {
  101. PlayLooping();
  102. }
  103. else PlayOneShotSound();
  104. }
  105. //-------------------------------------------------
  106. public void PlayWithDelay( float delayTime )
  107. {
  108. if ( looping )
  109. Invoke( "PlayLooping", delayTime );
  110. else
  111. Invoke( "PlayOneShotSound", delayTime );
  112. }
  113. //-------------------------------------------------
  114. // Play random wave clip on audiosource as a one shot
  115. //-------------------------------------------------
  116. public AudioClip PlayOneShotSound()
  117. {
  118. if ( !this.audioSource.isActiveAndEnabled )
  119. return null;
  120. SetAudioSource();
  121. if ( this.stopOnPlay )
  122. audioSource.Stop();
  123. if ( this.disableOnEnd )
  124. Invoke( "Disable", clip.length );
  125. this.audioSource.PlayOneShot( this.clip );
  126. return this.clip;
  127. }
  128. //-------------------------------------------------
  129. public AudioClip PlayLooping()
  130. {
  131. // get audio source properties, and do any special randomizations
  132. SetAudioSource();
  133. // if the audio source has forgotten to be set to looping, set it to looping
  134. if ( !audioSource.loop )
  135. audioSource.loop = true;
  136. // play the clip in the audio source, all the meanwhile updating it's location
  137. this.audioSource.Play();
  138. // if disable on end is checked, stop playing the wave file after the first loop has finished.
  139. if ( stopOnEnd )
  140. Invoke( "Stop", audioSource.clip.length );
  141. return this.clip;
  142. }
  143. //-------------------------------------------------
  144. public void Disable()
  145. {
  146. gameObject.SetActive( false );
  147. }
  148. //-------------------------------------------------
  149. public void Stop()
  150. {
  151. audioSource.Stop();
  152. }
  153. //-------------------------------------------------
  154. private void SetAudioSource()
  155. {
  156. if ( this.useRandomVolume )
  157. {
  158. //randomly apply a volume between the volume min max
  159. this.audioSource.volume = Random.Range( this.volMin, this.volMax );
  160. if ( useRandomSilence && ( Random.Range( 0, 1 ) < percentToNotPlay ) )
  161. {
  162. this.audioSource.volume = 0;
  163. }
  164. }
  165. if ( this.useRandomPitch )
  166. {
  167. //randomly apply a pitch between the pitch min max
  168. this.audioSource.pitch = Random.Range( this.pitchMin, this.pitchMax );
  169. }
  170. if ( this.waveFile.Length > 0 )
  171. {
  172. // randomly assign a wave file from the array into the audioSource clip property
  173. audioSource.clip = this.waveFile[Random.Range( 0, waveFile.Length )];
  174. clip = audioSource.clip;
  175. }
  176. }
  177. }
  178. }