123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
-
- using UnityEngine;
- using System.Collections;
- namespace Valve.VR.InteractionSystem
- {
-
- [RequireComponent( typeof( AudioSource ) )]
- public class PlaySound : MonoBehaviour
- {
- [Tooltip( "List of audio clips to play." )]
- public AudioClip[] waveFile;
- [Tooltip( "Stops the currently playing clip in the audioSource. Otherwise clips will overlap/mix." )]
- public bool stopOnPlay;
- [Tooltip( "After the audio clip finishes playing, disable the game object the sound is on." )]
- public bool disableOnEnd;
- [Tooltip( "Loop the sound after the wave file variation has been chosen." )]
- public bool looping;
- [Tooltip( "If the sound is looping and updating it's position every frame, stop the sound at the end of the wav/clip length. " )]
- public bool stopOnEnd;
- [Tooltip( "Start a wave file playing on awake, but after a delay." )]
- public bool playOnAwakeWithDelay;
- [Header ( "Random Volume" )]
- public bool useRandomVolume = true;
- [Tooltip( "Minimum volume that will be used when randomly set." )]
- [Range( 0.0f, 1.0f )]
- public float volMin = 1.0f;
- [Tooltip( "Maximum volume that will be used when randomly set." )]
- [Range( 0.0f, 1.0f )]
- public float volMax = 1.0f;
- [Header ( "Random Pitch" )]
- [Tooltip( "Use min and max random pitch levels when playing sounds." )]
- public bool useRandomPitch = true;
- [Tooltip( "Minimum pitch that will be used when randomly set." )]
- [Range( -3.0f, 3.0f )]
- public float pitchMin = 1.0f;
- [Tooltip( "Maximum pitch that will be used when randomly set." )]
- [Range( -3.0f, 3.0f )]
- public float pitchMax = 1.0f;
- [Header( "Random Time" )]
- [Tooltip( "Use Retrigger Time to repeat the sound within a time range" )]
- public bool useRetriggerTime = false;
- [Tooltip( "Inital time before the first repetion starts" )]
- [Range( 0.0f, 360.0f )]
- public float timeInitial = 0.0f;
- [Tooltip( "Minimum time that will pass before the sound is retriggered" )]
- [Range( 0.0f, 360.0f )]
- public float timeMin = 0.0f;
- [Tooltip( "Maximum pitch that will be used when randomly set." )]
- [Range( 0.0f, 360.0f )]
- public float timeMax = 0.0f;
- [Header ( "Random Silence" )]
- [Tooltip( "Use Retrigger Time to repeat the sound within a time range" )]
- public bool useRandomSilence = false;
- [Tooltip( "Percent chance that the wave file will not play" )]
- [Range( 0.0f, 1.0f )]
- public float percentToNotPlay = 0.0f;
- [Header( "Delay Time" )]
- [Tooltip( "Time to offset playback of sound" )]
- public float delayOffsetTime = 0.0f;
- private AudioSource audioSource;
- private AudioClip clip;
-
- void Awake()
- {
- audioSource = GetComponent<AudioSource>();
- clip = audioSource.clip;
-
- if ( audioSource.playOnAwake )
- {
- if ( useRetriggerTime )
- InvokeRepeating( "Play", timeInitial, Random.Range( timeMin, timeMax ) );
- else
- Play();
- }
-
- else if ( !audioSource.playOnAwake && playOnAwakeWithDelay )
- {
- PlayWithDelay( delayOffsetTime );
- if ( useRetriggerTime )
- InvokeRepeating( "Play", timeInitial, Random.Range( timeMin, timeMax ) );
- }
-
- else if ( audioSource.playOnAwake && playOnAwakeWithDelay )
- {
- PlayWithDelay( delayOffsetTime );
- if ( useRetriggerTime )
- InvokeRepeating( "Play", timeInitial, Random.Range( timeMin, timeMax ) );
- }
- }
-
-
-
- public void Play()
- {
- if ( looping )
- {
- PlayLooping();
- }
- else PlayOneShotSound();
- }
-
- public void PlayWithDelay( float delayTime )
- {
- if ( looping )
- Invoke( "PlayLooping", delayTime );
- else
- Invoke( "PlayOneShotSound", delayTime );
- }
-
-
-
- public AudioClip PlayOneShotSound()
- {
- if ( !this.audioSource.isActiveAndEnabled )
- return null;
- SetAudioSource();
- if ( this.stopOnPlay )
- audioSource.Stop();
- if ( this.disableOnEnd )
- Invoke( "Disable", clip.length );
- this.audioSource.PlayOneShot( this.clip );
- return this.clip;
- }
-
- public AudioClip PlayLooping()
- {
-
- SetAudioSource();
-
- if ( !audioSource.loop )
- audioSource.loop = true;
-
- this.audioSource.Play();
-
- if ( stopOnEnd )
- Invoke( "Stop", audioSource.clip.length );
- return this.clip;
- }
-
- public void Disable()
- {
- gameObject.SetActive( false );
- }
-
- public void Stop()
- {
- audioSource.Stop();
- }
-
- private void SetAudioSource()
- {
- if ( this.useRandomVolume )
- {
-
- this.audioSource.volume = Random.Range( this.volMin, this.volMax );
- if ( useRandomSilence && ( Random.Range( 0, 1 ) < percentToNotPlay ) )
- {
- this.audioSource.volume = 0;
- }
- }
- if ( this.useRandomPitch )
- {
-
- this.audioSource.pitch = Random.Range( this.pitchMin, this.pitchMax );
- }
- if ( this.waveFile.Length > 0 )
- {
-
- audioSource.clip = this.waveFile[Random.Range( 0, waveFile.Length )];
- clip = audioSource.clip;
- }
- }
- }
- }
|