Browse Source

Update MusicManager.cs

Fixed Play Sound volume being scaled by Music Manager music volume.
master
Chris Gregan 6 years ago
parent
commit
01e89455a5
  1. 57
      Assets/Fungus/Scripts/Components/MusicManager.cs

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

@ -12,13 +12,14 @@ namespace Fungus
//[RequireComponent(typeof(AudioSource))] //[RequireComponent(typeof(AudioSource))]
public class MusicManager : MonoBehaviour public class MusicManager : MonoBehaviour
{ {
protected AudioSource audioSource; protected AudioSource audioSourceMusic;
protected AudioSource audioSourceAmbiance; protected AudioSource audioSourceAmbiance;
protected AudioSource audioSourceSoundEffect;
void Reset() void Reset()
{ {
int audioSourceCount = this.GetComponents<AudioSource>().Length; int audioSourceCount = this.GetComponents<AudioSource>().Length;
for (int i = 0; i < 2 - audioSourceCount; i++) for (int i = 0; i < 3 - audioSourceCount; i++)
gameObject.AddComponent<AudioSource>(); gameObject.AddComponent<AudioSource>();
} }
@ -26,14 +27,16 @@ namespace Fungus
protected virtual void Awake() protected virtual void Awake()
{ {
Reset(); Reset();
audioSource = GetComponents<AudioSource>()[0]; AudioSource[] audioSources = GetComponents<AudioSource>();
audioSourceAmbiance = GetComponents<AudioSource>()[1]; audioSourceMusic = audioSources[0];
audioSourceAmbiance = audioSources[1];
audioSourceSoundEffect = audioSources[2];
} }
protected virtual void Start() protected virtual void Start()
{ {
audioSource.playOnAwake = false; audioSourceMusic.playOnAwake = false;
audioSource.loop = true; audioSourceMusic.loop = true;
} }
#region Public members #region Public members
@ -44,33 +47,33 @@ 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)
{ {
if (audioSource == null || audioSource.clip == musicClip) if (audioSourceMusic == null || audioSourceMusic.clip == musicClip)
{ {
return; return;
} }
if (Mathf.Approximately(fadeDuration, 0f)) if (Mathf.Approximately(fadeDuration, 0f))
{ {
audioSource.clip = musicClip; audioSourceMusic.clip = musicClip;
audioSource.loop = loop; audioSourceMusic.loop = loop;
audioSource.time = atTime; // May be inaccurate if the audio source is compressed http://docs.unity3d.com/ScriptReference/AudioSource-time.html BK audioSourceMusic.time = atTime; // May be inaccurate if the audio source is compressed http://docs.unity3d.com/ScriptReference/AudioSource-time.html BK
audioSource.Play(); audioSourceMusic.Play();
} }
else else
{ {
float startVolume = audioSource.volume; float startVolume = audioSourceMusic.volume;
LeanTween.value(gameObject, startVolume, 0f, fadeDuration) LeanTween.value(gameObject, startVolume, 0f, fadeDuration)
.setOnUpdate((v) => { .setOnUpdate((v) => {
// Fade out current music // Fade out current music
audioSource.volume = v; audioSourceMusic.volume = v;
}).setOnComplete(() => { }).setOnComplete(() => {
// Play new music // Play new music
audioSource.volume = startVolume; audioSourceMusic.volume = startVolume;
audioSource.clip = musicClip; audioSourceMusic.clip = musicClip;
audioSource.loop = loop; audioSourceMusic.loop = loop;
audioSource.time = atTime; // May be inaccurate if the audio source is compressed http://docs.unity3d.com/ScriptReference/AudioSource-time.html BK audioSourceMusic.time = atTime; // May be inaccurate if the audio source is compressed http://docs.unity3d.com/ScriptReference/AudioSource-time.html BK
audioSource.Play(); audioSourceMusic.Play();
}); });
} }
} }
@ -82,7 +85,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)
{ {
audioSource.PlayOneShot(soundClip, volume); audioSourceSoundEffect.PlayOneShot(soundClip, volume);
} }
/// <summary> /// <summary>
@ -109,7 +112,7 @@ namespace Fungus
{ {
if (Mathf.Approximately(duration, 0f)) if (Mathf.Approximately(duration, 0f))
{ {
audioSource.pitch = pitch; audioSourceMusic.pitch = pitch;
audioSourceAmbiance.pitch = pitch; audioSourceAmbiance.pitch = pitch;
if (onComplete != null) if (onComplete != null)
{ {
@ -119,11 +122,11 @@ namespace Fungus
} }
LeanTween.value(gameObject, LeanTween.value(gameObject,
audioSource.pitch, audioSourceMusic.pitch,
pitch, pitch,
duration).setOnUpdate((p) => duration).setOnUpdate((p) =>
{ {
audioSource.pitch = p; audioSourceMusic.pitch = p;
audioSourceAmbiance.pitch = p; audioSourceAmbiance.pitch = p;
}).setOnComplete(() => }).setOnComplete(() =>
{ {
@ -148,16 +151,16 @@ namespace Fungus
{ {
onComplete(); onComplete();
} }
audioSource.volume = volume; audioSourceMusic.volume = volume;
audioSourceAmbiance.volume = volume; audioSourceAmbiance.volume = volume;
return; return;
} }
LeanTween.value(gameObject, LeanTween.value(gameObject,
audioSource.volume, audioSourceMusic.volume,
volume, volume,
duration).setOnUpdate((v) => { duration).setOnUpdate((v) => {
audioSource.volume = v; audioSourceMusic.volume = v;
audioSourceAmbiance.volume = v; audioSourceAmbiance.volume = v;
}).setOnComplete(() => { }).setOnComplete(() => {
if (onComplete != null) if (onComplete != null)
@ -172,8 +175,8 @@ namespace Fungus
/// </summary> /// </summary>
public virtual void StopMusic() public virtual void StopMusic()
{ {
audioSource.Stop(); audioSourceMusic.Stop();
audioSource.clip = null; audioSourceMusic.clip = null;
} }
/// <summary> /// <summary>

Loading…
Cancel
Save