private IEnumerator XOut(float duration, AudioSource a1, SongCallBack runOnEndFunction) { var startTime = Time.realtimeSinceStartup; var endTime = startTime + duration; float maxVolume = a1.volume, deltaVolume = 0f, volumePercent = 1f; while (Time.realtimeSinceStartup < endTime) { volumePercent = 1f; if (endTime - Time.realtimeSinceStartup > duration) { startTime = Time.realtimeSinceStartup; endTime = startTime + duration; } deltaVolume = ((Time.realtimeSinceStartup - startTime) / duration) * maxVolume; a1.volume = Mathf.Clamp01((maxVolume - deltaVolume) * volumePercent); yield return(null); } a1.volume = 0f; a1.Stop(); if (runOnEndFunction != null) { runOnEndFunction(); } }
/// <summary> /// Crossin from a1 for duration. /// </summary> private IEnumerator XIn(float duration, AudioSource a1, SongCallBack runOnEndFunction) { var startTime = Time.realtimeSinceStartup; var endTime = startTime + duration; float a1StartVolume = 0f, startMaxMusicVolume = a1.volume, volumePercent = 1f; a1.volume = a1StartVolume; if (!a1.isPlaying) { a1.Play(); } float deltaVolume = 0f; while (Time.realtimeSinceStartup < endTime) { volumePercent = 1f; if (endTime - Time.realtimeSinceStartup > duration) { startTime = Time.realtimeSinceStartup; endTime = startTime + duration; } deltaVolume = ((Time.realtimeSinceStartup - startTime) / duration) * (startMaxMusicVolume - a1StartVolume); a1.volume = Mathf.Clamp01((deltaVolume + a1StartVolume) * volumePercent); yield return(null); } a1.volume = startMaxMusicVolume; if (runOnEndFunction != null) { runOnEndFunction(); } }
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; source.pitch = pitch; 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); }
/// <summary> /// Crossin from a1 for duration. /// </summary> private IEnumerator XIn(float duration, AudioSource a1, SongCallBack runOnEndFunction) { var startTime = Time.realtimeSinceStartup; var endTime = startTime + duration; float a1StartVolume = 0f, startMaxMusicVolume = a1.volume, volumePercent = 1f; a1.volume = a1StartVolume; if (!a1.isPlaying) { a1.Play(); } float deltaVolume = 0f; bool passedFirstPause = false, passedFirstUnpause = true; float pauseTimeRemaining = 0f; while (isPaused || passedFirstPause || Time.realtimeSinceStartup < endTime) { if (isPaused) { if (!passedFirstPause) { pauseTimeRemaining = endTime - Time.realtimeSinceStartup; passedFirstPause = true; passedFirstUnpause = false; } yield return(new WaitForFixedUpdate()); } else { if (!passedFirstUnpause) { float oldEndTime = endTime; endTime = Time.realtimeSinceStartup + pauseTimeRemaining; startTime += (endTime - oldEndTime); passedFirstPause = false; passedFirstUnpause = true; } volumePercent = 1f; if (endTime - Time.realtimeSinceStartup > duration) { startTime = Time.realtimeSinceStartup; endTime = startTime + duration; } deltaVolume = ((Time.realtimeSinceStartup - startTime) / duration) * (startMaxMusicVolume - a1StartVolume); a1.volume = Mathf.Clamp01((deltaVolume + a1StartVolume) * volumePercent); yield return(null); } } a1.volume = startMaxMusicVolume; if (runOnEndFunction != null) { runOnEndFunction(); } }
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(); aSource.pitch = pitch; 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 IEnumerator XFade(float duration, AudioSource a1, AudioSource a2, SongCallBack runOnEndFunction) { var startTime = Time.realtimeSinceStartup; var endTime = startTime + duration; if (!a2.isPlaying) { a2.Play(); } float a1StartVolume = a1.volume, a2StartVolume = a2.volume, deltaPercent = 0f, a1DeltaVolume = 0f, a2DeltaVolume = 0f, startMaxMusicVolume = a2.volume, volumePercent = 1f; while (Time.realtimeSinceStartup < endTime) { volumePercent = 1f; if (endTime - Time.realtimeSinceStartup > duration) { startTime = Time.realtimeSinceStartup; endTime = startTime + duration; } deltaPercent = ((Time.realtimeSinceStartup - startTime) / duration); a1DeltaVolume = deltaPercent * a1StartVolume; a2DeltaVolume = deltaPercent * (startMaxMusicVolume - a2StartVolume); a1.volume = Mathf.Clamp01((a1StartVolume - a1DeltaVolume) * volumePercent); a2.volume = Mathf.Clamp01((a2DeltaVolume + a2StartVolume) * volumePercent); yield return(null); } a1.volume = 0f; a2.volume = a2StartVolume; a1.Stop(); if (runOnEndFunction != null) { runOnEndFunction(); } }
/// <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)); }
public static void CrossOut(float duration, AudioSource source, SongCallBack runOnEndFunction = null) { Instance.StartCoroutine(Instance.XOut(duration, source, runOnEndFunction)); }
public static void Crossfade(float duration, AudioSource fromSource, AudioSource toSource, SongCallBack runOnEndFunction = null) { Instance.StartCoroutine(Instance.XFade(duration, fromSource, toSource, runOnEndFunction)); }
public static void Crossfade(float duration, GameObject fromSFXObject, GameObject toSFXObject, SongCallBack runOnEndFunction = null) { Crossfade(duration, fromSFXObject.GetComponent <AudioSource>(), toSFXObject.GetComponent <AudioSource>(), runOnEndFunction); }
/// <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)); }
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)); }
public static void CrossOut(float duration, GameObject sfxObject, SongCallBack runOnEndFunction = null) { CrossOut(duration, sfxObject.GetComponent <AudioSource>(), runOnEndFunction); }
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)); }
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; } aSource.PlayDelayed(delay); } 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); }
/// <summary> /// Plays the clip by crossing out what's currently playing regardless of a playing SoundConnection, with an option to loop. Calls an event once the clip is done. /// You can resume a SoundConnection afterwards if you so choose, using Ins.currentSoundConnection. However, it will not resume on it's own. /// Callbacks will only fire once. /// </summary> /// <param name='clip2play'> /// The clip to play. /// </param> /// <param name='runOnEndFunction'> /// Function to run once the clip is done. Is added to OnSongEnd /// </param> /// <param name='loop'> /// Whether the clip should loop /// </param> public static void Play(AudioClip clip2play, bool loop = false, SongCallBack runOnEndFunction = null) { Ins._Play(clip2play, loop, runOnEndFunction); }
/// <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)); }