8 using System.Collections;
10 namespace Valve.VR.InteractionSystem
13 [RequireComponent( typeof( AudioSource ) )]
16 [Tooltip(
"List of audio clips to play." )]
17 public AudioClip[] waveFile;
18 [Tooltip(
"Stops the currently playing clip in the audioSource. Otherwise clips will overlap/mix." )]
19 public bool stopOnPlay;
20 [Tooltip(
"After the audio clip finishes playing, disable the game object the sound is on." )]
21 public bool disableOnEnd;
22 [Tooltip(
"Loop the sound after the wave file variation has been chosen." )]
24 [Tooltip(
"If the sound is looping and updating it's position every frame, stop the sound at the end of the wav/clip length. " )]
25 public bool stopOnEnd;
26 [Tooltip(
"Start a wave file playing on awake, but after a delay." )]
27 public bool playOnAwakeWithDelay;
29 [Header (
"Random Volume" )]
30 public bool useRandomVolume =
true;
31 [Tooltip(
"Minimum volume that will be used when randomly set." )]
33 public float volMin = 1.0f;
34 [Tooltip(
"Maximum volume that will be used when randomly set." )]
36 public float volMax = 1.0f;
38 [Header (
"Random Pitch" )]
39 [Tooltip(
"Use min and max random pitch levels when playing sounds." )]
40 public bool useRandomPitch =
true;
41 [Tooltip(
"Minimum pitch that will be used when randomly set." )]
42 [Range( -3.0f, 3.0f )]
43 public float pitchMin = 1.0f;
44 [Tooltip(
"Maximum pitch that will be used when randomly set." )]
45 [Range( -3.0f, 3.0f )]
46 public float pitchMax = 1.0f;
48 [Header(
"Random Time" )]
49 [Tooltip(
"Use Retrigger Time to repeat the sound within a time range" )]
50 public bool useRetriggerTime =
false;
51 [Tooltip(
"Inital time before the first repetion starts" )]
52 [Range( 0.0f, 360.0f )]
53 public float timeInitial = 0.0f;
54 [Tooltip(
"Minimum time that will pass before the sound is retriggered" )]
55 [Range( 0.0f, 360.0f )]
56 public float timeMin = 0.0f;
57 [Tooltip(
"Maximum pitch that will be used when randomly set." )]
58 [Range( 0.0f, 360.0f )]
59 public float timeMax = 0.0f;
61 [Header (
"Random Silence" )]
62 [Tooltip(
"Use Retrigger Time to repeat the sound within a time range" )]
63 public bool useRandomSilence =
false;
64 [Tooltip(
"Percent chance that the wave file will not play" )]
66 public float percentToNotPlay = 0.0f;
68 [Header(
"Delay Time" )]
69 [Tooltip(
"Time to offset playback of sound" )]
70 public float delayOffsetTime = 0.0f;
73 private AudioSource audioSource;
74 private AudioClip clip;
79 audioSource = GetComponent<AudioSource>();
80 clip = audioSource.clip;
83 if ( audioSource.playOnAwake )
85 if ( useRetriggerTime )
86 InvokeRepeating(
"Play", timeInitial, Random.Range( timeMin, timeMax ) );
92 else if ( !audioSource.playOnAwake && playOnAwakeWithDelay )
94 PlayWithDelay( delayOffsetTime );
96 if ( useRetriggerTime )
97 InvokeRepeating(
"Play", timeInitial, Random.Range( timeMin, timeMax ) );
101 else if ( audioSource.playOnAwake && playOnAwakeWithDelay )
103 PlayWithDelay( delayOffsetTime );
105 if ( useRetriggerTime )
106 InvokeRepeating(
"Play", timeInitial, Random.Range( timeMin, timeMax ) );
122 else PlayOneShotSound();
127 public void PlayWithDelay(
float delayTime )
130 Invoke(
"PlayLooping", delayTime );
132 Invoke(
"PlayOneShotSound", delayTime );
139 public AudioClip PlayOneShotSound()
141 if ( !this.audioSource.isActiveAndEnabled )
145 if ( this.stopOnPlay )
147 if ( this.disableOnEnd )
148 Invoke(
"Disable", clip.length );
149 this.audioSource.PlayOneShot( this.clip );
155 public AudioClip PlayLooping()
161 if ( !audioSource.loop )
162 audioSource.loop =
true;
165 this.audioSource.Play();
169 Invoke(
"Stop", audioSource.clip.length );
175 public void Disable()
177 gameObject.SetActive( false );
189 private void SetAudioSource()
191 if ( this.useRandomVolume )
194 this.audioSource.volume = Random.Range( this.volMin, this.volMax );
196 if ( useRandomSilence && ( Random.Range( 0, 1 ) < percentToNotPlay ) )
198 this.audioSource.volume = 0;
202 if ( this.useRandomPitch )
205 this.audioSource.pitch = Random.Range( this.pitchMin, this.pitchMax );
208 if ( this.waveFile.Length > 0 )
211 audioSource.clip = this.waveFile[Random.Range( 0, waveFile.Length )];
212 clip = audioSource.clip;