Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Created April 28, 2013 17:08
Show Gist options
  • Save tsubaki/5477526 to your computer and use it in GitHub Desktop.
Save tsubaki/5477526 to your computer and use it in GitHub Desktop.
サウンドをフェードアウト/フェードインするサンプル
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