private void Duck(SoundDuckingSetting duckingSetting, float duckVolume, float duckPitch, float unDuckedVolume, float unDuckedPitch, params AudioSource[] exceptions) { if (exceptions.Length <= 0 || duckingSetting == SoundDuckingSetting.DoNotDuck) { return; } if (!isDucking) { isDucking = true; preDuckVolume = maxVolume; preDuckVolumeMusic = maxMusicVolume; preDuckVolumeSFX = maxSFXVolume; preDuckPitch = GetPitch(); preDuckPitchMusic = GetPitchMusic(); preDuckPitchSFX = GetPitchSFX(); } else { duckNumber++; } duckSource = exceptions[0]; StartCoroutine(DuckTheTrack(duckingSetting, duckVolume, duckPitch, unDuckedVolume, unDuckedPitch, exceptions)); }
private IEnumerator DuckTheTrack(SoundDuckingSetting duckingSetting, float duckVolume, float duckPitch, float unDuckedVolume, float unDuckedPitch, AudioSource[] exceptions) { int thisNumber = duckNumber; /* crossfade in */ var startTime = Time.realtimeSinceStartup; var endTime = startTime + duckStartSpeed; float startMaxVolume = 0f; float duckStartVolume = 0f; float startMaxPitch = 1f; float duckStartPitch = 1f; switch (duckingSetting) { case SoundDuckingSetting.DuckAll: duckStartVolume = startMaxVolume = maxVolume; duckStartPitch = startMaxPitch = GetPitch(); break; case SoundDuckingSetting.OnlyDuckMusic: duckStartVolume = startMaxVolume = maxMusicVolume; duckStartPitch = startMaxPitch = GetPitchMusic(); break; case SoundDuckingSetting.OnlyDuckSFX: duckStartVolume = startMaxVolume = maxSFXVolume; duckStartPitch = startMaxPitch = GetPitchSFX(); break; case SoundDuckingSetting.DoNotDuck: default: yield break; } float unDuckStartVolume = 0f; float deltaPercent = 0f; float duckDeltaVolume = 0f; float unDuckDeltaVolume = 0f; float volumePercent = 1f; float unDuckStartPitch = 1f; float duckDeltaPitch = 0f; float unDuckDeltaPitch = 0f; while (Time.realtimeSinceStartup < endTime) { if (thisNumber != duckNumber) { yield break; } float maxSoundVolume = 1f; switch (duckingSetting) { case SoundDuckingSetting.DuckAll: maxSoundVolume = maxVolume; break; case SoundDuckingSetting.OnlyDuckMusic: maxSoundVolume = maxMusicVolume; break; case SoundDuckingSetting.OnlyDuckSFX: maxSoundVolume = maxSFXVolume; break; case SoundDuckingSetting.DoNotDuck: default: yield break; } if (startMaxVolume == 0f) { volumePercent = 1f; } else { volumePercent = maxSoundVolume / startMaxVolume; } if (endTime - Time.realtimeSinceStartup > duckStartSpeed) { startTime = Time.realtimeSinceStartup; endTime = startTime + duckStartSpeed; } deltaPercent = ((Time.realtimeSinceStartup - startTime) / duckStartSpeed); duckDeltaVolume = deltaPercent * (duckStartVolume - duckVolume); unDuckDeltaVolume = deltaPercent * (startMaxVolume - unDuckStartVolume); duckDeltaPitch = deltaPercent * (duckStartPitch - duckPitch); unDuckDeltaPitch = deltaPercent * (startMaxPitch - unDuckStartPitch); switch (duckingSetting) { case SoundDuckingSetting.DuckAll: SetVolume(Mathf.Clamp01((duckStartVolume - duckDeltaVolume) * volumePercent)); SetPitch(Mathf.Clamp01((duckStartPitch - duckDeltaPitch) * volumePercent)); foreach (AudioSource exception in exceptions) { SetVolumeSFX(Mathf.Clamp01((unDuckDeltaVolume + unDuckStartVolume) * volumePercent), true, exception); SetPitchSFX(Mathf.Clamp01((unDuckDeltaPitch + unDuckStartPitch) * volumePercent), exception); } break; case SoundDuckingSetting.OnlyDuckMusic: SetVolumeMusic(Mathf.Clamp01((duckStartVolume - duckDeltaVolume) * volumePercent)); SetPitchMusic(Mathf.Clamp01((duckStartPitch - duckDeltaPitch) * volumePercent)); foreach (AudioSource exception in exceptions) { SetVolumeSFX(Mathf.Clamp01((unDuckDeltaVolume + unDuckStartVolume) * volumePercent), true, exception); SetPitchSFX(Mathf.Clamp01((unDuckDeltaPitch + unDuckStartPitch) * volumePercent), exception); } break; case SoundDuckingSetting.OnlyDuckSFX: SetVolumeSFX(Mathf.Clamp01((duckStartVolume - duckDeltaVolume) * volumePercent)); SetPitchSFX(Mathf.Clamp01((duckStartPitch - duckDeltaPitch) * volumePercent)); foreach (AudioSource exception in exceptions) { SetVolumeSFX(Mathf.Clamp01((unDuckDeltaVolume + unDuckStartVolume) * volumePercent), true, exception); SetPitchSFX(Mathf.Clamp01((unDuckDeltaPitch + unDuckStartPitch) * volumePercent), exception); } break; case SoundDuckingSetting.DoNotDuck: default: yield break; } yield return(null); } switch (duckingSetting) { case SoundDuckingSetting.DuckAll: SetVolume(duckVolume); SetPitch(duckPitch); foreach (AudioSource exception in exceptions) { SetVolumeSFX(unDuckedVolume, true, exception); SetPitchSFX(unDuckedPitch, exception); } break; case SoundDuckingSetting.OnlyDuckMusic: SetVolumeMusic(duckVolume); SetPitchMusic(duckPitch); foreach (AudioSource exception in exceptions) { SetVolumeSFX(unDuckedVolume, true, exception); SetPitchSFX(unDuckedPitch, exception); } break; case SoundDuckingSetting.OnlyDuckSFX: SetVolumeSFX(duckVolume); SetPitchSFX(duckPitch); foreach (AudioSource exception in exceptions) { SetVolumeSFX(unDuckedVolume, true, exception); SetPitchSFX(unDuckedPitch, exception); } break; case SoundDuckingSetting.DoNotDuck: default: yield break; } /* end crossfade in */ /* wait for clip to stop playing */ while (exceptions[0].isPlaying) { if (thisNumber != duckNumber) { yield break; } yield return(null); } /* cross rest back in and call run on end */ startTime = Time.realtimeSinceStartup; endTime = startTime + duckEndSpeed; switch (duckingSetting) { case SoundDuckingSetting.DuckAll: duckStartVolume = maxVolume; startMaxVolume = preDuckVolume; duckStartPitch = GetPitch(); startMaxPitch = preDuckPitch; break; case SoundDuckingSetting.OnlyDuckMusic: duckStartVolume = maxMusicVolume; startMaxVolume = preDuckVolumeMusic; duckStartPitch = GetPitchMusic(); startMaxPitch = preDuckPitchMusic; break; case SoundDuckingSetting.OnlyDuckSFX: duckStartVolume = maxSFXVolume; startMaxVolume = preDuckVolumeSFX; duckStartPitch = GetPitchSFX(); startMaxPitch = preDuckPitchSFX; break; case SoundDuckingSetting.DoNotDuck: default: yield break; } volumePercent = 1f; deltaPercent = 0f; while (Time.realtimeSinceStartup < endTime) { if (thisNumber != duckNumber) { yield break; } float maxSoundVolume = 1f; switch (duckingSetting) { case SoundDuckingSetting.DuckAll: maxSoundVolume = preDuckVolume; break; case SoundDuckingSetting.OnlyDuckMusic: maxSoundVolume = preDuckVolumeMusic; break; case SoundDuckingSetting.OnlyDuckSFX: maxSoundVolume = preDuckVolumeSFX; break; case SoundDuckingSetting.DoNotDuck: default: yield break; } if (startMaxVolume == 0f) { volumePercent = 1f; } else { volumePercent = maxSoundVolume / startMaxVolume; } if (endTime - Time.realtimeSinceStartup > duckEndSpeed) { startTime = Time.realtimeSinceStartup; endTime = startTime + duckEndSpeed; } deltaPercent = ((Time.realtimeSinceStartup - startTime) / duckEndSpeed) * (startMaxVolume - duckStartVolume); switch (duckingSetting) { case SoundDuckingSetting.DuckAll: SetVolume(Mathf.Clamp01((deltaPercent + duckStartVolume) * volumePercent)); SetPitch(Mathf.Clamp01((deltaPercent + duckStartPitch) * volumePercent)); break; case SoundDuckingSetting.OnlyDuckMusic: SetVolumeMusic(Mathf.Clamp01((deltaPercent + duckStartVolume) * volumePercent)); SetPitchMusic(Mathf.Clamp01((deltaPercent + duckStartPitch) * volumePercent)); break; case SoundDuckingSetting.OnlyDuckSFX: SetVolumeSFX(Mathf.Clamp01((deltaPercent + duckStartVolume) * volumePercent)); SetPitchSFX(Mathf.Clamp01((deltaPercent + duckStartPitch) * volumePercent)); break; case SoundDuckingSetting.DoNotDuck: default: yield break; } yield return(null); } switch (duckingSetting) { case SoundDuckingSetting.DuckAll: SetVolume(preDuckVolume); SetPitch(preDuckPitch); break; case SoundDuckingSetting.OnlyDuckMusic: SetVolumeMusic(preDuckVolumeMusic); SetPitchMusic(preDuckPitchMusic); break; case SoundDuckingSetting.OnlyDuckSFX: SetVolumeSFX(preDuckVolumeSFX); SetPitchSFX(preDuckPitchSFX); break; case SoundDuckingSetting.DoNotDuck: default: yield break; } if (thisNumber != duckNumber) { yield break; } duckNumber = 0; if (isDucking) { isDucking = false; } }
private AudioSource PlaySFXBase(AudioSource aSource, AudioClip clip, float volume, float pitch, bool capped = false, string cappedID = "", bool looping = false, float delay = 0f, SongCallBack runOnEndFunction = null, SoundDuckingSetting duckingSetting = SoundDuckingSetting.DoNotDuck, float duckVolume = 0f, float duckPitch = 1f) { aSource.Stop(); string clipName = clip.name; if (pitchVariations.ContainsKey(clipName)) { aSource.pitch = pitch.Vary(pitchVariations[clipName]); } else { aSource.pitch = pitch; } if (baseVolumes.ContainsKey(clipName)) { volume = volume * baseVolumes[clipName]; } if (volumeVariations.ContainsKey(clipName)) { aSource.volume = volume.VaryWithRestrictions(volumeVariations[clipName]); } else { aSource.volume = volume; } if (!capped) { aSource.loop = looping; } aSource.mute = mutedSFX; if (delay <= 0f) { aSource.Play(); } else { if (!delayedAudioSources.ContainsKey(aSource)) { delayedAudioSources.Add(aSource, delay); } else { delayedAudioSources[aSource] = delay; } #if UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 aSource.Play((ulong)(44100f * delay)); #else aSource.PlayDelayed(delay); #endif } if (runOnEndFunction != null) { if (runOnEndFunctions.ContainsKey(aSource)) { runOnEndFunctions[aSource] = runOnEndFunction; } else { runOnEndFunctions.Add(aSource, runOnEndFunction); } } Duck(duckingSetting, duckVolume, duckPitch, volume, pitch, aSource); if (capped && !string.IsNullOrEmpty(cappedID)) { cappedSFXObjects.Add(aSource.gameObject.GetInstanceID(), cappedID); } return(aSource); }
private AudioSource PlaySFXLoopOn(AudioSource source, AudioClip clip, bool tillDestroy, float volume, float pitch, float maxDuration, SongCallBack runOnEndFunction = null, SoundDuckingSetting duckingSetting = SoundDuckingSetting.DoNotDuck, float duckVolume = 0f, float duckPitch = 1f) { source.Stop(); source.clip = clip; string clipName = clip.name; if (pitchVariations.ContainsKey(clipName)) { source.pitch = pitch.Vary(pitchVariations[clipName]); } else { source.pitch = pitch; } if (baseVolumes.ContainsKey(clipName)) { volume = volume * baseVolumes[clipName]; } if (volumeVariations.ContainsKey(clipName)) { source.volume = volume.VaryWithRestrictions(volumeVariations[clipName]); } else { source.volume = volume; } source.mute = Instance.mutedSFX; source.loop = true; source.Play(); if (runOnEndFunction != null) { if (runOnEndFunctions.ContainsKey(source)) { runOnEndFunctions[source] = runOnEndFunction; } else { runOnEndFunctions.Add(source, runOnEndFunction); } } Duck(duckingSetting, duckVolume, duckPitch, volume, pitch, source); Instance.StartCoroutine(Instance._PlaySFXLoopTillDestroy(source.gameObject, source, tillDestroy, maxDuration)); return(source); }
private AudioSource PlaySFXAt(AudioClip clip, float volume, float pitch, Vector3 location = default(Vector3), bool capped = false, string cappedID = "", bool looping = false, float delay = 0f, SongCallBack runOnEndFunction = null, SoundDuckingSetting duckingSetting = SoundDuckingSetting.DoNotDuck, float duckVolume = 0f, float duckPitch = 1f) { GameObject tempGO = GetNextInactiveSFXObject(clip); if (tempGO == null) { return(null); } AudioSource aSource = tempGO.GetComponent <AudioSource>(); aSource.transform.position = location; #if UNITY_3_4 || UNITY_3_5 aSource.gameObject.SetActiveRecursively(true); #else aSource.gameObject.SetActive(true); #endif return(PlaySFXBase(aSource, clip, volume, pitch, capped, cappedID, looping, delay, runOnEndFunction, duckingSetting, duckVolume, duckPitch)); }
private AudioSource PlaySFXOn(AudioSource aSource, AudioClip clip, float volume, float pitch, bool capped = false, string cappedID = "", bool looping = false, float delay = 0f, SongCallBack runOnEndFunction = null, SoundDuckingSetting duckingSetting = SoundDuckingSetting.DoNotDuck, float duckVolume = 0f, float duckPitch = 1f) { aSource.clip = clip; return(PlaySFXBase(aSource, clip, volume, pitch, capped, cappedID, looping, delay, runOnEndFunction, duckingSetting, duckVolume, duckPitch)); }
/// <summary> /// Plays the SFX in a loop on another gameObject of your choice. This function is cattered more towards customizing a loop. /// You can set the loop to end when the object dies or a maximum duration, whichever comes first. /// tillDestroy defaults to true, volume to 1f, pitch to 1f, maxDuration to 0f /// </summary> public static AudioSource PlaySFXLoop(GameObject gO, AudioClip clip, bool tillDestroy, float volume, float pitch, float maxDuration, SongCallBack runOnEndFunction = null, SoundDuckingSetting duckingSetting = SoundDuckingSetting.DoNotDuck, float duckVolume = 0f, float duckPitch = 1f) { if (Instance.offTheSFX) { return(null); } if ((clip == null) || (gO == null)) { return(null); } if (gO.GetComponent <AudioSource>() == null) { gO.AddComponent <AudioSource>(); } Instance.CheckInsertionIntoUnownedSFXObjects(gO.GetComponent <AudioSource>()); return(Instance.PlaySFXLoopOn(gO.GetComponent <AudioSource>(), clip, tillDestroy, volume, pitch, maxDuration, runOnEndFunction, duckingSetting, duckVolume, duckPitch)); }
/// <summary> /// Plays the SFX another gameObject of your choice, will default the looping to false, pitch to 1f, volume to 1f /// </summary> public static AudioSource PlaySFX(GameObject gO, AudioClip clip, bool looping, float volume, float pitch, float delay = 0f, SongCallBack runOnEndFunction = null, SoundDuckingSetting duckingSetting = SoundDuckingSetting.DoNotDuck, float duckVolume = 0f, float duckPitch = 1f) { if (Instance.offTheSFX) { return(null); } if ((clip == null) || (gO == null)) { return(null); } if (gO.GetComponent <AudioSource>() == null) { gO.AddComponent <AudioSource>(); } return(PlaySFX(gO.GetComponent <AudioSource>(), clip, looping, volume, pitch, delay, runOnEndFunction, duckingSetting, duckVolume, duckPitch)); }
/// <summary> /// Plays the SFX another audiosource of your choice, will default the looping to false, pitch to 1f, volume to 1f /// </summary> public static AudioSource PlaySFX(AudioSource aS, AudioClip clip, bool looping, float volume, float pitch, float delay = 0f, SongCallBack runOnEndFunction = null, SoundDuckingSetting duckingSetting = SoundDuckingSetting.DoNotDuck, float duckVolume = 0f, float duckPitch = 1f) { if (Instance.offTheSFX) { return(null); } if ((clip == null) || (aS == null)) { return(null); } // Keep reference of unownedsfx objects Instance.CheckInsertionIntoUnownedSFXObjects(aS); return(Instance.PlaySFXOn(aS, clip, volume, pitch, false, "", looping, delay, runOnEndFunction, duckingSetting, duckVolume, duckPitch)); }
/// <summary> /// Plays the SFX on an owned object, will default the location to (0,0,0), pitch to 1f, volume to 1f /// </summary> public static AudioSource PlaySFX(AudioClip clip, float volume, float pitch, Vector3 location = default(Vector3), bool looping = false, float delay = 0f, SongCallBack runOnEndFunction = null, SoundDuckingSetting duckingSetting = SoundDuckingSetting.DoNotDuck, float duckVolume = 0f, float duckPitch = 1f) { if (Instance.offTheSFX) { return(null); } if (clip == null) { return(null); } return(Instance.PlaySFXAt(clip, volume, pitch, location, false, "", looping, delay, runOnEndFunction, duckingSetting, duckVolume, duckPitch)); }
/// <summary> /// Plays the SFX on an owned & pooled object by clipname reference on the SoundManager, will default the location to (0,0,0), pitch to SoundManager.Instance.pitchSFX, volume to SoundManager.Instance.volumeSFX /// </summary> /// <returns> /// The resulting <a href="http://docs.unity3d.com/ScriptReference/AudioSource.html">AudioSource</a>. /// </returns> /// <param name='clipName'> /// Name of the clip on the SoundManager. /// </param> /// <param name='looping'> /// Whether it is looping. /// </param> /// <param name='delay'> /// Delay. /// </param> /// <param name='volume'> /// Volume. If set to float.MaxValue, it will become the default volume currently set. /// </param> /// <param name='pitch'> /// Pitch. If set to float.MaxValue, it will become the default pitch currently set. /// </param> /// <param name='location'> /// Location. /// </param> /// <param name='runOnEndFunction'> /// Run on end function. /// </param> /// <param name='duckingSetting'> /// Ducking setting. /// </param> /// <param name='duckVolume'> /// Duck volume. /// </param> /// <param name='duckPitch'> /// Duck pitch. /// </param> public static AudioSource PlaySFX(string clipName, bool looping = false, float delay = 0f, float volume = float.MaxValue, float pitch = float.MaxValue, Vector3 location = default(Vector3), SongCallBack runOnEndFunction = null, SoundDuckingSetting duckingSetting = SoundDuckingSetting.DoNotDuck, float duckVolume = 0f, float duckPitch = 1f) { if (Instance.offTheSFX || Instance.isPaused) { return(null); } //if (!SoundManager.ClipNameIsValid(clipName)) // return null; if (volume == float.MaxValue) { volume = Instance.volumeSFX; } if (pitch == float.MaxValue) { pitch = Instance.pitchSFX; } return(Instance.PlaySFXAt(SoundManager.Load(clipName), volume, pitch, location, false, "", looping, delay, runOnEndFunction, duckingSetting, duckVolume, duckPitch)); }