// This code is part of the Fungus library (https://github.com/snozbot/fungus) // It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE) using UnityEngine; namespace Fungus { /// /// Music manager which provides basic music and sound effect functionality. /// Music playback persists across scene loads. /// //[RequireComponent(typeof(AudioSource))] public class MusicManager : MonoBehaviour { protected AudioSource audioSourceMusic; protected AudioSource audioSourceAmbiance; protected AudioSource audioSourceSoundEffect; void Reset() { int audioSourceCount = this.GetComponents().Length; for (int i = 0; i < 3 - audioSourceCount; i++) gameObject.AddComponent(); } protected virtual void Awake() { Reset(); AudioSource[] audioSources = GetComponents(); audioSourceMusic = audioSources[0]; audioSourceAmbiance = audioSources[1]; audioSourceSoundEffect = audioSources[2]; } protected virtual void Start() { audioSourceMusic.playOnAwake = false; audioSourceMusic.loop = true; } #region Public members /// /// Plays game music using an audio clip. /// One music clip may be played at a time. /// public void PlayMusic(AudioClip musicClip, bool loop, float fadeDuration, float atTime) { if (audioSourceMusic == null || audioSourceMusic.clip == musicClip) { return; } if (Mathf.Approximately(fadeDuration, 0f)) { audioSourceMusic.clip = musicClip; audioSourceMusic.loop = loop; audioSourceMusic.time = atTime; // May be inaccurate if the audio source is compressed http://docs.unity3d.com/ScriptReference/AudioSource-time.html BK audioSourceMusic.Play(); } else { float startVolume = audioSourceMusic.volume; LeanTween.value(gameObject, startVolume, 0f, fadeDuration) .setOnUpdate((v) => { // Fade out current music audioSourceMusic.volume = v; }).setOnComplete(() => { // Play new music audioSourceMusic.volume = startVolume; audioSourceMusic.clip = musicClip; audioSourceMusic.loop = loop; audioSourceMusic.time = atTime; // May be inaccurate if the audio source is compressed http://docs.unity3d.com/ScriptReference/AudioSource-time.html BK audioSourceMusic.Play(); }); } } /// /// Plays a sound effect once, at the specified volume. /// /// The sound effect clip to play. /// The volume level of the sound effect. public virtual void PlaySound(AudioClip soundClip, float volume) { audioSourceSoundEffect.PlayOneShot(soundClip, volume); } /// /// Plays a sound effect with optional looping values, at the specified volume. /// /// The sound effect clip to play. /// If the audioclip should loop or not. /// The volume level of the sound effect. public virtual void PlayAmbianceSound(AudioClip soundClip, bool loop, float volume) { audioSourceAmbiance.loop = loop; audioSourceAmbiance.clip = soundClip; audioSourceAmbiance.volume = volume; audioSourceAmbiance.Play(); } /// /// Shifts the game music pitch to required value over a period of time. /// /// The new music pitch value. /// The length of time in seconds needed to complete the pitch change. /// A delegate method to call when the pitch shift has completed. public virtual void SetAudioPitch(float pitch, float duration, System.Action onComplete) { if (Mathf.Approximately(duration, 0f)) { audioSourceMusic.pitch = pitch; audioSourceAmbiance.pitch = pitch; if (onComplete != null) { onComplete(); } return; } LeanTween.value(gameObject, audioSourceMusic.pitch, pitch, duration).setOnUpdate((p) => { audioSourceMusic.pitch = p; audioSourceAmbiance.pitch = p; }).setOnComplete(() => { if (onComplete != null) { onComplete(); } }); } /// /// Fades the game music volume to required level over a period of time. /// /// The new music volume value [0..1] /// The length of time in seconds needed to complete the volume change. /// Delegate function to call when fade completes. public virtual void SetAudioVolume(float volume, float duration, System.Action onComplete) { if (Mathf.Approximately(duration, 0f)) { if (onComplete != null) { onComplete(); } audioSourceMusic.volume = volume; audioSourceAmbiance.volume = volume; return; } LeanTween.value(gameObject, audioSourceMusic.volume, volume, duration).setOnUpdate((v) => { audioSourceMusic.volume = v; audioSourceAmbiance.volume = v; }).setOnComplete(() => { if (onComplete != null) { onComplete(); } }); } /// /// Stops playing game music. /// public virtual void StopMusic() { audioSourceMusic.Stop(); audioSourceMusic.clip = null; } /// /// Stops playing game ambiance. /// public virtual void StopAmbiance() { audioSourceAmbiance.Stop(); audioSourceAmbiance.clip = null; } #endregion } }