diff --git a/Assets/Fungus/Scripts/Components/MusicManager.cs b/Assets/Fungus/Scripts/Components/MusicManager.cs index bdae88c5..c89744e8 100644 --- a/Assets/Fungus/Scripts/Components/MusicManager.cs +++ b/Assets/Fungus/Scripts/Components/MusicManager.cs @@ -12,10 +12,17 @@ namespace Fungus [RequireComponent(typeof(AudioSource))] public class MusicManager : MonoBehaviour { + protected AudioSource audioSource; + + protected virtual void Awake() + { + audioSource = GetComponent(); + } + protected virtual void Start() { - GetComponent().playOnAwake = false; - GetComponent().loop = true; + audioSource.playOnAwake = false; + audioSource.loop = true; } #region Public members @@ -26,7 +33,6 @@ namespace Fungus /// public void PlayMusic(AudioClip musicClip, bool loop, float fadeDuration, float atTime) { - AudioSource audioSource = GetComponent(); if (audioSource == null || audioSource.clip == musicClip) { return; @@ -65,7 +71,7 @@ namespace Fungus /// The volume level of the sound effect. public virtual void PlaySound(AudioClip soundClip, float volume) { - GetComponent().PlayOneShot(soundClip, volume); + audioSource.PlayOneShot(soundClip, volume); } /// @@ -76,11 +82,9 @@ namespace Fungus /// A delegate method to call when the pitch shift has completed. public virtual void SetAudioPitch(float pitch, float duration, System.Action onComplete) { - AudioSource audio = GetComponent(); - if (Mathf.Approximately(duration, 0f)) { - audio.pitch = pitch; + audioSource.pitch = pitch; if (onComplete != null) { onComplete(); @@ -89,10 +93,10 @@ namespace Fungus } LeanTween.value(gameObject, - audio.pitch, + audioSource.pitch, pitch, duration).setOnUpdate( (p) => { - audio.pitch = p; + audioSource.pitch = p; }).setOnComplete( () => { if (onComplete != null) { @@ -109,23 +113,21 @@ namespace Fungus /// Delegate function to call when fade completes. public virtual void SetAudioVolume(float volume, float duration, System.Action onComplete) { - AudioSource audio = GetComponent(); - if (Mathf.Approximately(duration, 0f)) { if (onComplete != null) { onComplete(); } - audio.volume = volume; + audioSource.volume = volume; return; } LeanTween.value(gameObject, - audio.volume, + audioSource.volume, volume, duration).setOnUpdate( (v) => { - audio.volume = v; + audioSource.volume = v; }).setOnComplete( () => { if (onComplete != null) { @@ -139,7 +141,8 @@ namespace Fungus /// public virtual void StopMusic() { - GetComponent().Stop(); + audioSource.Stop(); + audioSource.clip = null; } #endregion