Skip to content

Instantly share code, notes, and snippets.

@tsubaki tsubaki/AudioFade.cs
Created Apr 28, 2013

Embed
What would you like to do?
サウンドをフェードアウト/フェードインするサンプル
using UnityEngine;
using System.Collections;
using System;
[RequireComponent( typeof(AudioSource))]
public class AudioFade : MonoBehaviour
{
int volume = 0;
#region Unity_delegate
void OnEnable ()
{
StopAllCoroutines();
if( gameObject.activeSelf )
StartCoroutine (Fadein ());
}
void OnDisable ()
{
StopAllCoroutines();
if( gameObject.activeSelf )
StartCoroutine (Fadeout ());
}
#endregion
IEnumerator Fadein ()
{
audio.enabled = true;
for (; volume<100; volume++) {
audio.volume = (float)volume / 100;
yield return null;
}
audio.volume = 1;
}
IEnumerator Fadeout ()
{
for (; volume>0; volume--) {
audio.volume = (float)(volume) / 100;
yield return null;
}
audio.volume = 0;
audio.enabled = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.