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