Created
January 26, 2018 17:45
-
-
Save chall3ng3r/071bf541b245173bbaab5e57d0e00c26 to your computer and use it in GitHub Desktop.
Audio Fade Script for Unity3D
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class AudioFadeScript | |
{ | |
public static IEnumerator FadeOut(AudioSource audioSource, float FadeTime) | |
{ | |
float startVolume = audioSource.volume; | |
while (audioSource.volume > 0) | |
{ | |
audioSource.volume -= startVolume * Time.deltaTime / FadeTime; | |
yield return null; | |
} | |
audioSource.Stop(); | |
audioSource.volume = startVolume; | |
} | |
public static IEnumerator FadeIn(AudioSource audioSource, float FadeTime) | |
{ | |
float startVolume = 0.2f; | |
audioSource.volume = 0; | |
audioSource.Play(); | |
while (audioSource.volume < 1.0f) | |
{ | |
audioSource.volume += startVolume * Time.deltaTime / FadeTime; | |
yield return null; | |
} | |
audioSource.volume = 1f; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment