// PlayRandomRandomizedSFX chooses a passed AudioClip randomly and plays it with a randomized pitch. // Must create an explicit AudioClip[], but allows for customization of parameters. public AudioSource PlayRandomRandomizedSFX(AudioClip[] clips, bool loop = false, float positionOffset = 0, AudioSourceManager.SoundEventCallback soundEventCallback = null, float pan = 0, ulong delay = 0) { // Generate a random number between 0 and the length of our array of clips passed in. int randomIndex = Random.Range(0, clips.Length); // Play a single randomized clip randomly selected from the passed in args. AudioSource sfx = PlayRandomizedSFX(clips[randomIndex], loop, positionOffset, soundEventCallback, pan, delay); // Return a reference to the AudioSource for later use. return(sfx); }
// Used to play single sound clips and randomly slightly changes the pitch. public AudioSource PlayRandomizedSFX(SoundEffect sfx, bool loop = false, float positionOffset = 0, AudioSourceManager.SoundEventCallback soundEventCallback = null, float pan = 0, ulong delay = 0) { return(PlayRandomizedSFX(sfxClips[(int)sfx], loop, positionOffset, soundEventCallback, pan, delay)); }
// Used to play single sound clips and randomly slightly changes the pitch. public AudioSource PlayRandomizedSFX(AudioClip clip, bool loop = false, float positionOffset = 0, AudioSourceManager.SoundEventCallback soundEventCallback = null, float pan = 0, ulong delay = 0) { // Create a new sfx AudioSource sfx = PlaySFX(clip, loop, positionOffset, soundEventCallback, pan, delay); // Choose a random pitch to play back our clip at between our high and low pitch ranges. float randomPitch = Random.Range(lowPitchRange, highPitchRange); // Set the pitch of the audio source to the randomly chosen pitch. sfx.pitch = randomPitch; // Return a reference to the AudioSource for later use. return(sfx); }
// The number of ms to play a sfx for. // Will automatically loop the sfx until the duration is over. public AudioSource PlaySFXFor(AudioClip clip, int durationMS, float positionOffset = 0, AudioSourceManager.SoundEventCallback soundEventCallback = null, float pan = 0, ulong delay = 0) { // Start our sound effect. AudioSource sfx = PlaySFX(clip, true, positionOffset, soundEventCallback, pan, delay); // Then start our coroutine which will stop it StartCoroutine(StopSFXInMS(sfx, durationMS)); // Return a reference to our AudioSource return(sfx); }
// The number of ms to play a sfx for. // Will automatically loop the sfx until the duration is over. public AudioSource PlaySFXFor(SoundEffect sfx, int durationMS, float positionOffset = 0, AudioSourceManager.SoundEventCallback soundEventCallback = null, float pan = 0, ulong delay = 0) { return(PlaySFXFor(sfxClips[(int)sfx], durationMS, positionOffset, soundEventCallback, pan, delay)); }
// Used to play single sound clips. public AudioSource PlaySFX(AudioClip clip, bool loop = false, float positionOffset = 0, AudioSourceManager.SoundEventCallback soundEventCallback = null, float pan = 0, ulong delay = 0, GameObject target = null, float vol = 1) { // Create a new sfx. AudioSource sfx = createAudioSource(clip, sfxVolume, loop, positionOffset, soundEventCallback, pan, target, vol); // Play the clip if we are in sfx playing or stopped state. // Change sfx state to playing if it was stopped before if (sfxState != SoundPlayerState.Paused) { sfx.Play(delay); sfxState = SoundPlayerState.Playing; } // Return a reference to the AudioSource for later use. return(sfx); }
// Used to play single sound clips. public AudioSource PlaySFX(SoundEffect sfx, bool loop = false, float positionOffset = 0, AudioSourceManager.SoundEventCallback soundEventCallback = null, float pan = 0, ulong delay = 0, GameObject target = null, float vol = 1) { return(PlaySFX(sfxClips[(int)sfx], loop, positionOffset, soundEventCallback, pan, delay, target, vol)); }
// Dynamically creates a new AudioSource with specified clip, volume, and panning. public AudioSource createAudioSource(AudioClip clip, float volume, bool loop = false, float positionOffset = 0, AudioSourceManager.SoundEventCallback soundEventCallback = null, float pan = 0, GameObject target = null, float vol = 1) { // Create a new AudioSource to play the clip on. // Spatial blend is 0, meaning 2D. AudioSource audio; if (target == null) { audio = gameObject.AddComponent <AudioSource>(); audio.spatialBlend = 0; } else { audio = target.AddComponent <AudioSource>(); audio.spatialBlend = 1; audio.rolloffMode = AudioRolloffMode.Linear; } // Set the clip of our AudioSource to the clip passed in as a parameter. audio.clip = clip; // Set the volume of the sound. audio.volume = volume * vol; // Is the sound going to loop indefinitely. audio.loop = loop; // What time offset do we want the sound to start at. audio.time = positionOffset; // Are we panning the sound? audio.panStereo = pan; // Add sfx to arraylist. sfxSources.Add(new AudioSourceManager(audio, soundEventCallback)); // Return a reference to the AudioSource for later use. return(audio); }