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))]
public class MusicManager : MonoBehaviour
{
protected AudioSource audioSource;
protected virtual void Awake()
{
audioSource = GetComponent<AudioSource>();
}
protected virtual void Start()
{
GetComponent<AudioSource>().playOnAwake = false;
GetComponent<AudioSource>().loop = true;
audioSource.playOnAwake = false;
audioSource.loop = true;
}
#region Public members
@ -26,7 +33,6 @@ namespace Fungus
/// </summary>
public void PlayMusic(AudioClip musicClip, bool loop, float fadeDuration, float atTime)
{
AudioSource audioSource = GetComponent<AudioSource>();
if (audioSource == null || audioSource.clip == musicClip)
{
return;
@ -65,7 +71,7 @@ namespace Fungus
/// <param name="volume">The volume level of the sound effect.</param>
public virtual void PlaySound(AudioClip soundClip, float volume)
{
GetComponent<AudioSource>().PlayOneShot(soundClip, volume);
audioSource.PlayOneShot(soundClip, volume);
}
/// <summary>
@ -76,11 +82,9 @@ namespace Fungus
/// <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)
{
AudioSource audio = GetComponent<AudioSource>();
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
/// <param name="onComplete">Delegate function to call when fade completes.</param>
public virtual void SetAudioVolume(float volume, float duration, System.Action onComplete)
{
AudioSource audio = GetComponent<AudioSource>();
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
/// </summary>
public virtual void StopMusic()
{
GetComponent<AudioSource>().Stop();
audioSource.Stop();
audioSource.clip = null;
}
#endregion

Loading…
Cancel
Save