Skip to content

Instantly share code, notes, and snippets.

@chall3ng3r
Created January 26, 2018 17:45
Show Gist options
  • Save chall3ng3r/071bf541b245173bbaab5e57d0e00c26 to your computer and use it in GitHub Desktop.
Save chall3ng3r/071bf541b245173bbaab5e57d0e00c26 to your computer and use it in GitHub Desktop.
Audio Fade Script for Unity3D
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