|
|
@ -1,7 +1,7 @@ |
|
|
|
// This code is part of the Fungus library (http://fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus). |
|
|
|
// This code is part of the Fungus library (http://fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus). |
|
|
|
// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE) |
|
|
|
// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE) |
|
|
|
|
|
|
|
|
|
|
|
using UnityEngine; |
|
|
|
using UnityEngine; |
|
|
|
|
|
|
|
|
|
|
|
namespace Fungus |
|
|
|
namespace Fungus |
|
|
|
{ |
|
|
|
{ |
|
|
@ -9,14 +9,25 @@ namespace Fungus |
|
|
|
/// Music manager which provides basic music and sound effect functionality. |
|
|
|
/// Music manager which provides basic music and sound effect functionality. |
|
|
|
/// Music playback persists across scene loads. |
|
|
|
/// Music playback persists across scene loads. |
|
|
|
/// </summary> |
|
|
|
/// </summary> |
|
|
|
[RequireComponent(typeof(AudioSource))] |
|
|
|
//[RequireComponent(typeof(AudioSource))] |
|
|
|
public class MusicManager : MonoBehaviour |
|
|
|
public class MusicManager : MonoBehaviour |
|
|
|
{ |
|
|
|
{ |
|
|
|
protected AudioSource audioSource; |
|
|
|
protected AudioSource audioSource; |
|
|
|
|
|
|
|
protected AudioSource audioSourceAmbiance; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Reset() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
int audioSourceCount = this.GetComponents<AudioSource>().Length; |
|
|
|
|
|
|
|
for (int i = 0; i < 2 - audioSourceCount; i++) |
|
|
|
|
|
|
|
gameObject.AddComponent<AudioSource>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected virtual void Awake() |
|
|
|
protected virtual void Awake() |
|
|
|
{ |
|
|
|
{ |
|
|
|
audioSource = GetComponent<AudioSource>(); |
|
|
|
Reset(); |
|
|
|
|
|
|
|
audioSource = GetComponents<AudioSource>()[0]; |
|
|
|
|
|
|
|
audioSourceAmbiance = GetComponents<AudioSource>()[1]; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected virtual void Start() |
|
|
|
protected virtual void Start() |
|
|
@ -50,10 +61,10 @@ namespace Fungus |
|
|
|
float startVolume = audioSource.volume; |
|
|
|
float startVolume = audioSource.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; |
|
|
|
audioSource.volume = v; |
|
|
|
}).setOnComplete( () => { |
|
|
|
}).setOnComplete(() => { |
|
|
|
// Play new music |
|
|
|
// Play new music |
|
|
|
audioSource.volume = startVolume; |
|
|
|
audioSource.volume = startVolume; |
|
|
|
audioSource.clip = musicClip; |
|
|
|
audioSource.clip = musicClip; |
|
|
@ -82,10 +93,10 @@ 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 PlayAmbianceSound(AudioClip soundClip, bool loop, float volume) |
|
|
|
public virtual void PlayAmbianceSound(AudioClip soundClip, bool loop, float volume) |
|
|
|
{ |
|
|
|
{ |
|
|
|
audioSource.loop = loop; |
|
|
|
audioSourceAmbiance.loop = loop; |
|
|
|
audioSource.clip = soundClip; |
|
|
|
audioSourceAmbiance.clip = soundClip; |
|
|
|
audioSource.volume = volume; |
|
|
|
audioSourceAmbiance.volume = volume; |
|
|
|
audioSource.Play(); |
|
|
|
audioSourceAmbiance.Play(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// <summary> |
|
|
@ -99,6 +110,7 @@ namespace Fungus |
|
|
|
if (Mathf.Approximately(duration, 0f)) |
|
|
|
if (Mathf.Approximately(duration, 0f)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
audioSource.pitch = pitch; |
|
|
|
audioSource.pitch = pitch; |
|
|
|
|
|
|
|
audioSourceAmbiance.pitch = pitch; |
|
|
|
if (onComplete != null) |
|
|
|
if (onComplete != null) |
|
|
|
{ |
|
|
|
{ |
|
|
|
onComplete(); |
|
|
|
onComplete(); |
|
|
@ -106,12 +118,15 @@ namespace Fungus |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
LeanTween.value(gameObject, |
|
|
|
LeanTween.value(gameObject, |
|
|
|
audioSource.pitch, |
|
|
|
audioSource.pitch, |
|
|
|
pitch, |
|
|
|
pitch, |
|
|
|
duration).setOnUpdate( (p) => { |
|
|
|
duration).setOnUpdate((p) => |
|
|
|
|
|
|
|
{ |
|
|
|
audioSource.pitch = p; |
|
|
|
audioSource.pitch = p; |
|
|
|
}).setOnComplete( () => { |
|
|
|
audioSourceAmbiance.pitch = p; |
|
|
|
|
|
|
|
}).setOnComplete(() => |
|
|
|
|
|
|
|
{ |
|
|
|
if (onComplete != null) |
|
|
|
if (onComplete != null) |
|
|
|
{ |
|
|
|
{ |
|
|
|
onComplete(); |
|
|
|
onComplete(); |
|
|
@ -129,20 +144,22 @@ namespace Fungus |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (Mathf.Approximately(duration, 0f)) |
|
|
|
if (Mathf.Approximately(duration, 0f)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (onComplete != null) |
|
|
|
if (onComplete != null) |
|
|
|
{ |
|
|
|
{ |
|
|
|
onComplete(); |
|
|
|
onComplete(); |
|
|
|
} |
|
|
|
} |
|
|
|
audioSource.volume = volume; |
|
|
|
audioSource.volume = volume; |
|
|
|
|
|
|
|
audioSourceAmbiance.volume = volume; |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
LeanTween.value(gameObject, |
|
|
|
LeanTween.value(gameObject, |
|
|
|
audioSource.volume, |
|
|
|
audioSource.volume, |
|
|
|
volume, |
|
|
|
volume, |
|
|
|
duration).setOnUpdate( (v) => { |
|
|
|
duration).setOnUpdate((v) => { |
|
|
|
audioSource.volume = v; |
|
|
|
audioSource.volume = v; |
|
|
|
}).setOnComplete( () => { |
|
|
|
audioSourceAmbiance.volume = v; |
|
|
|
|
|
|
|
}).setOnComplete(() => { |
|
|
|
if (onComplete != null) |
|
|
|
if (onComplete != null) |
|
|
|
{ |
|
|
|
{ |
|
|
|
onComplete(); |
|
|
|
onComplete(); |
|
|
@ -159,6 +176,15 @@ namespace Fungus |
|
|
|
audioSource.clip = null; |
|
|
|
audioSource.clip = null; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
|
|
|
/// Stops playing game ambiance. |
|
|
|
|
|
|
|
/// </summary> |
|
|
|
|
|
|
|
public virtual void StopAmbiance() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
audioSourceAmbiance.Stop(); |
|
|
|
|
|
|
|
audioSourceAmbiance.clip = null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#endregion |
|
|
|
#endregion |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |