IEnumerator FadeOutAnim(float fadeTime) { //初期設定 AudioSource src = nowPlayingBGM; //フェードイン中にフェードアウトが呼ばれた場合 if (!src) { //フェードイン処理停止 myManager.StopCoroutine(fadeInCol); src = fadeInAudio; } src.name = "[Audio BGM - " + latestPlayBGMType.ToString() + " - FadeOut ]"; nowPlayingBGM = null; //フェードアウト float t = 0; float vol = src.volume; while (t < 1.0f) { t += Time.deltaTime / fadeTime; src.volume = (1 - t) * vol; yield return(null); } Destroy(src.gameObject); }
AudioSource _Play(BGMType type, float vol, bool isLoop) { if (nowPlayingBGM) { Destroy(nowPlayingBGM.gameObject); } AudioSource src = new GameObject("[Audio BGM - " + type.ToString() + "]").AddComponent <AudioSource>(); src.transform.SetParent(myManager.transform); src.clip = BGMclips[(int)type]; src.volume = vol; src.outputAudioMixerGroup = mixerGroups[1]; src.Play(); if (isLoop) { src.loop = true; } else { Destroy(src.gameObject, BGMclips[(int)type].length + 0.1f); } nowPlayingBGM = src; latestPlayBGMType = type; return(src); }
IEnumerator FadeInAnim(float fadeTime, BGMType type, float vol, bool isLoop) { //初期設定 fadeInAudio = new GameObject("[Audio BGM - " + type.ToString() + " - FadeIn ]").AddComponent <AudioSource>(); fadeInAudio.transform.SetParent(myManager.transform); fadeInAudio.clip = BGMclips[(int)type]; fadeInAudio.volume = 0; fadeInAudio.outputAudioMixerGroup = mixerGroups[1]; fadeInAudio.Play(); //フェードイン float t = 0; while (t < 1.0f) { t += Time.deltaTime / fadeTime; fadeInAudio.volume = t * vol; yield return(null); } fadeInAudio.volume = vol; fadeInAudio.name = "[Audio BGM - " + type.ToString() + "]"; if (nowPlayingBGM) { Destroy(nowPlayingBGM.gameObject); } if (isLoop) { fadeInAudio.loop = true; } else { Destroy(fadeInAudio.gameObject, BGMclips[(int)type].length + 0.1f); } nowPlayingBGM = fadeInAudio; }