/** * After the fade it makes a call to config.OnComplete to stop the sounds. * At the end restore the volume to the starterValue. **/ protected IEnumerator FadeVolumeAndStop(FadeVolumeConfiguration config) { float inverseDuration = 1.0f / config.duration; float elapsed = 0; float progress; while (elapsed < config.duration) { elapsed += Time.deltaTime; progress = elapsed * inverseDuration; //Mathf.Lerp are already clamping the progress between 0-1 SetVolumeTo(config.mixerProperty, Mathf.Lerp(config.starterVolume, config.volume, progress)); UpdateGlobalVolumes(); yield return(0); } //Should be the stop function if (config.OnCompleteStop != null) { //A little hardcoding on the parameters config.OnCompleteStop(false); } //Restore volume SetVolumeTo(config.mixerProperty, config.starterVolume); }
protected IEnumerator FadeVolume(FadeVolumeConfiguration config) { float inverseDuration = 1.0f / config.duration; float elapsed = 0; float progress; while (elapsed < config.duration) { elapsed += Time.deltaTime; progress = elapsed * inverseDuration; //Mathf.Lerp are already clamping the progress between 0-1 SetVolumeTo(config.mixerProperty, Mathf.Lerp(config.starterVolume, config.volume, progress)); UpdateGlobalVolumes(); yield return(0); } }
/** * Fade Music volume to the value specified during the specified time; **/ public void FadeMusicVolume(float toVolume, float fadeDuration) { //Stop previous fade if (CFadeMusic != null) { StopCoroutine(CFadeMusic); } //Parameters to the fade FadeVolumeConfiguration fadeConfiguration = new FadeVolumeConfiguration(); fadeConfiguration.volume = toVolume; fadeConfiguration.starterVolume = _musicVolume; fadeConfiguration.duration = fadeDuration; fadeConfiguration.mixerProperty = "musicVolume"; CFadeMusic = StartCoroutine("FadeVolume", fadeConfiguration); }
/** * FadeOut the music volume to 0 in the specified time. * After the fadeOut all the sounds are stopped and the volume is restored. * **/ public void FadeOutAndStopMusic(float fadeDuration) { //Stop previous fade if (CFadeMusic != null) { StopCoroutine(CFadeMusic); } //Parameters to the fade FadeVolumeConfiguration fadeConfiguration = new FadeVolumeConfiguration(); fadeConfiguration.volume = 0; fadeConfiguration.starterVolume = _musicVolume; fadeConfiguration.duration = fadeDuration; fadeConfiguration.mixerProperty = "musicVolume"; fadeConfiguration.OnCompleteStop += StopAllMusic; CFadeMusic = StartCoroutine("FadeVolumeAndStop", fadeConfiguration); }