// ------------------------------------------------------------------------------- // PlayBGM // ------------------------------------------------------------------------------- public static void PlayBGM(AudioClip bgmClip, float volume, float fadeDuration, bool loop) { MusicController instance = GetInstance(); //AudioSource curBgm = instance.getCurrentBGMPlaying(); AudioSource source = instance.GetBGMSource(); // -- fade-out current BGM foreach (AudioSource csource in instance.bgmSources) { if (csource.isPlaying) { if (fadeDuration > 0) { instance.FadeBGMOut(csource, fadeDuration / 2); instance.StartCoroutine(instance.RemoveBGMSource(csource, fadeDuration / 2)); } else { instance.FadeBGMOut(csource, 0); instance.StartCoroutine(instance.RemoveBGMSource(csource)); } } } // -- start new BGM if (bgmClip != null) { source.volume = GetVolume(volume); source.clip = bgmClip; source.loop = loop; source.Play(); // -- adjust new BGM (either fade-in or not) if (fadeDuration > 0) { instance.FadeBGMIn(source, fadeDuration / 2, fadeDuration / 2, volume); } else { float delay = 0f; instance.FadeBGMIn(source, delay, fadeDuration, volume); } if (!loop) { instance.StartCoroutine(instance.RemoveBGMSource(source)); } } }
// ------------------------------------------------------------------------------- // FadeBGMIn // ------------------------------------------------------------------------------- public void FadeBGMIn(AudioSource source, float delay, float fadeDuration, float volume) { MusicController instance = GetInstance(); float toVolume = GetVolume(volume); instance.StartCoroutine(FadeBGM(source, toVolume, delay, fadeDuration)); }
// ------------------------------------------------------------------------------- // FadeBGMOut // ------------------------------------------------------------------------------- public void FadeBGMOut(AudioSource source, float fadeDuration) { MusicController instance = GetInstance(); float delay = 0f; float toVolume = 0f; instance.StartCoroutine(instance.FadeBGM(source, toVolume, delay, fadeDuration)); }
// ------------------------------------------------------------------------------- // StopBGM // ------------------------------------------------------------------------------- public static void StopBGM(AudioClip bgmClip, float fadeDuration) { MusicController instance = GetInstance(); if (instance.bgmSources != null) { foreach (AudioSource source in instance.bgmSources) { if (source.clip == bgmClip && source.isPlaying) { instance.FadeBGMOut(source, fadeDuration); instance.StartCoroutine(instance.RemoveBGMSource(source, fadeDuration)); } } } }