Browse Source

Added SetAudioVolume.waitUntilFinished property #495

master
Christopher 9 years ago
parent
commit
bd66d6df6c
  1. 19
      Assets/Fungus/Audio/Scripts/Commands/SetAudioVolume.cs
  2. 14
      Assets/Fungus/Audio/Scripts/MusicController.cs

19
Assets/Fungus/Audio/Scripts/Commands/SetAudioVolume.cs

@ -11,21 +11,32 @@ namespace Fungus
{ {
[Range(0,1)] [Range(0,1)]
[Tooltip("Global volume level for audio played using Play Music and Play Sound")] [Tooltip("Global volume level for audio played using Play Music and Play Sound")]
public float volume = 1; public float volume = 1f;
[Range(0,30)] [Range(0,30)]
[Tooltip("Time to fade between current volume level and target volume level.")] [Tooltip("Time to fade between current volume level and target volume level.")]
public float fadeDuration; public float fadeDuration = 1f;
[Tooltip("Wait until the volume fade has completed before continuing.")]
public bool waitUntilFinished = true;
public override void OnEnter() public override void OnEnter()
{ {
MusicController musicController = MusicController.GetInstance(); MusicController musicController = MusicController.GetInstance();
if (musicController != null) if (musicController != null)
{ {
musicController.SetAudioVolume(volume, fadeDuration); musicController.SetAudioVolume(volume, fadeDuration, () => {
if (waitUntilFinished)
{
Continue();
}
});
} }
Continue(); if (!waitUntilFinished)
{
Continue();
}
} }
public override string GetSummary() public override string GetSummary()

14
Assets/Fungus/Audio/Scripts/MusicController.cs

@ -86,13 +86,14 @@ namespace Fungus
/** /**
* Fades the game music volume to required level over a period of time. * Fades the game music volume to required level over a period of time.
* @param volume The new music volume value [0..1] * @param volume The new music volume value [0..1]
* @param duration The length of time in seconds needed to complete the volume change. * @param duration The length of time in seconds needed to complete the volume change.
* @param onComplete Delegate function to call when fade completes.
*/ */
public virtual void SetAudioVolume(float volume, float duration) public virtual void SetAudioVolume(float volume, float duration, System.Action onComplete)
{ {
AudioSource audio = GetComponent<AudioSource>(); AudioSource audio = GetComponent<AudioSource>();
if (duration == 0f) if (Mathf.Approximately(duration, 0f))
{ {
audio.volume = volume; audio.volume = volume;
return; return;
@ -103,7 +104,12 @@ namespace Fungus
volume, volume,
duration).setOnUpdate( (v) => { duration).setOnUpdate( (v) => {
audio.volume = v; audio.volume = v;
}); }).setOnComplete( () => {
if (onComplete != null)
{
onComplete();
}
});
} }
/** /**

Loading…
Cancel
Save