Browse Source

Fixed repeat play of music

master
Chris Gregan 7 years ago
parent
commit
0b0776d775
  1. 33
      Assets/Fungus/Scripts/Components/MusicManager.cs

33
Assets/Fungus/Scripts/Components/MusicManager.cs

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

Loading…
Cancel
Save