private IEnumerator FadeMusicCoroutine(SoundController.SoundEvent soundEvent, float fadeTime) { float sumTime = 0; float volume = Music.volume; if (Music.clip != null && Music.isPlaying) { while (sumTime <= fadeTime) { Music.volume = Mathf.Lerp(volume, 0, sumTime / fadeTime); sumTime += Time.deltaTime; yield return(null); } } PlayMusic(soundEvent); sumTime = 0; while (sumTime <= fadeTime) { Music.volume = Mathf.Lerp(0, volume, sumTime / fadeTime); sumTime += Time.deltaTime; yield return(null); } Music.volume = volume; }
/// <summary> /// Play a new sound. /// There can be as many concurrent sound as needed /// </summary> /// <param name="soundEvent">the new sound</param> public void PlaySound(SoundController.SoundEvent soundEvent) { AudioSource currentAudioSource = m_soundPool.GetAudioSource(); currentAudioSource.clip = soundEvent.audio; if (soundEvent.mixer != null) { currentAudioSource.outputAudioMixerGroup = soundEvent.mixer; } else { currentAudioSource.outputAudioMixerGroup = null; } currentAudioSource.Play(); }
/// <summary> /// Play a music. /// If another music was playing, stop it /// </summary> /// <param name="soundEvent">The new music</param> public void PlayMusic(SoundController.SoundEvent soundEvent) { if (Music.isPlaying) { Music.Stop(); } Music.clip = soundEvent.audio; if (soundEvent.mixer != null) { Music.outputAudioMixerGroup = soundEvent.mixer; } else { Music.outputAudioMixerGroup = null; } Music.loop = true; Music.playOnAwake = true; Music.Play(); }
/// <summary> /// Fade the music to another music /// If no music exist, the new music is faded in /// </summary> /// <param name="soundEvent">The new music</param> /// <param name="fadeTime">Fading time</param> public void FadeMusicTo(SoundController.SoundEvent soundEvent, float fadeTime) { StartCoroutine(FadeMusicCoroutine(soundEvent, fadeTime)); }