From 4f603f91acb596ac2f25ac6f3075fcd5cb6f24eb Mon Sep 17 00:00:00 2001 From: Hector Castelli Zacharias Date: Thu, 28 Jun 2018 18:48:39 -0400 Subject: [PATCH 1/7] Added new command: Play Random sound. --- .../Scripts/Commands/PlayRandomSound.cs | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 Assets/Fungus/Scripts/Commands/PlayRandomSound.cs diff --git a/Assets/Fungus/Scripts/Commands/PlayRandomSound.cs b/Assets/Fungus/Scripts/Commands/PlayRandomSound.cs new file mode 100644 index 00000000..e3b0f953 --- /dev/null +++ b/Assets/Fungus/Scripts/Commands/PlayRandomSound.cs @@ -0,0 +1,83 @@ +// 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) + +using UnityEngine; + +namespace Fungus +{ + /// + /// Plays a once-off sound effect. Multiple sound effects can be played at the same time. + /// + [CommandInfo("Audio", + "Play Random Sound", + "Plays a once-off sound effect. Multiple sound effects can be played at the same time.")] + [AddComponentMenu("")] + public class PlayRandomSound : Command + { + [Tooltip("Sound effect clip to play")] + [SerializeField] + protected AudioClip[] soundClip; + + [Range(0, 1)] + [Tooltip("Volume level of the sound effect")] + [SerializeField] + protected float volume = 1; + + [Tooltip("Wait until the sound has finished playing before continuing execution.")] + [SerializeField] + protected bool waitUntilFinished; + + protected virtual void DoWait() + { + Continue(); + } + + #region Public members + + public override void OnEnter() + { + int rand = Random.Range(0, soundClip.Length); + if (soundClip == null) + { + Continue(); + return; + } + + var musicManager = FungusManager.Instance.MusicManager; + + musicManager.PlaySound(soundClip[rand], volume); + + if (waitUntilFinished) + { + Invoke("DoWait", soundClip[rand].length); + } + else + { + Continue(); + } + } + + public override string GetSummary() + { + if (soundClip == null) + { + return "Error: No sound clip selected"; + } + + string sounds = "["; + foreach (AudioClip ac in soundClip) { + sounds+=ac.name+" ,"; + } + sounds = sounds.TrimEnd(' ', ','); + sounds += "]"; + return "Random sounds "+sounds; + } + + public override Color GetButtonColor() + { + return new Color32(242, 209, 176, 255); + } + + #endregion + } +} From 1cfce972b6c966045f5b243512736be23f486846 Mon Sep 17 00:00:00 2001 From: Hector Castelli Zacharias Date: Thu, 28 Jun 2018 18:48:57 -0400 Subject: [PATCH 2/7] Added new command: Play ambience sound. (overlayed over music, can be looped) --- .../Scripts/Commands/PlayAmbienceSound.cs | 69 +++++++++++++++++++ .../Fungus/Scripts/Components/MusicManager.cs | 14 ++++ 2 files changed, 83 insertions(+) create mode 100644 Assets/Fungus/Scripts/Commands/PlayAmbienceSound.cs diff --git a/Assets/Fungus/Scripts/Commands/PlayAmbienceSound.cs b/Assets/Fungus/Scripts/Commands/PlayAmbienceSound.cs new file mode 100644 index 00000000..c893dd2f --- /dev/null +++ b/Assets/Fungus/Scripts/Commands/PlayAmbienceSound.cs @@ -0,0 +1,69 @@ +// 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) + +using UnityEngine; + +namespace Fungus +{ + /// + /// Plays a once-off sound effect. Multiple sound effects can be played at the same time. + /// + [CommandInfo("Audio", + "Play Ambience Sound", + "Plays a once-off sound effect. Multiple sound effects can be played at the same time.")] + [AddComponentMenu("")] + public class PlayAmbienceSound : Command + { + [Tooltip("Sound effect clip to play")] + [SerializeField] + protected AudioClip soundClip; + + [Range(0, 1)] + [Tooltip("Volume level of the sound effect")] + [SerializeField] + protected float volume = 1; + + [Tooltip("Sound effect clip to play")] + [SerializeField] + protected bool loop; + + protected virtual void DoWait() + { + Continue(); + } + + #region Public members + + public override void OnEnter() + { + if (soundClip == null) + { + Continue(); + return; + } + + var musicManager = FungusManager.Instance.MusicManager; + + musicManager.PlayAmbienceSound(soundClip, loop, volume); + + Continue(); + } + + public override string GetSummary() + { + if (soundClip == null) + { + return "Error: No sound clip selected"; + } + + return soundClip.name; + } + + public override Color GetButtonColor() + { + return new Color32(242, 209, 176, 255); + } + + #endregion + } +} diff --git a/Assets/Fungus/Scripts/Components/MusicManager.cs b/Assets/Fungus/Scripts/Components/MusicManager.cs index c89744e8..9b969490 100644 --- a/Assets/Fungus/Scripts/Components/MusicManager.cs +++ b/Assets/Fungus/Scripts/Components/MusicManager.cs @@ -74,6 +74,20 @@ namespace Fungus audioSource.PlayOneShot(soundClip, volume); } + /// + /// Plays a sound effect with optional looping values, at the specified volume. + /// + /// The sound effect clip to play. + /// If the audioclip should loop or not. + /// The volume level of the sound effect. + public virtual void PlayAmbienceSound(AudioClip soundClip, bool loop, float volume) + { + audioSource.loop = loop; + audioSource.clip = soundClip; + audioSource.volume = volume; + audioSource.Play(); + } + /// /// Shifts the game music pitch to required value over a period of time. /// From f0813d4aae31e955d46b8eef3fafde9e9bc244f7 Mon Sep 17 00:00:00 2001 From: Hector Castelli Zacharias Date: Thu, 28 Jun 2018 18:52:35 -0400 Subject: [PATCH 3/7] Fixed naming issues in ambience audio. --- Assets/Fungus/Scripts/Commands/PlayAmbienceSound.cs | 4 ++-- Assets/Fungus/Scripts/Components/MusicManager.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Assets/Fungus/Scripts/Commands/PlayAmbienceSound.cs b/Assets/Fungus/Scripts/Commands/PlayAmbienceSound.cs index c893dd2f..fcffb9b1 100644 --- a/Assets/Fungus/Scripts/Commands/PlayAmbienceSound.cs +++ b/Assets/Fungus/Scripts/Commands/PlayAmbienceSound.cs @@ -9,10 +9,10 @@ namespace Fungus /// Plays a once-off sound effect. Multiple sound effects can be played at the same time. /// [CommandInfo("Audio", - "Play Ambience Sound", + "Play Ambiance Sound", "Plays a once-off sound effect. Multiple sound effects can be played at the same time.")] [AddComponentMenu("")] - public class PlayAmbienceSound : Command + public class PlayAmbianceSound : Command { [Tooltip("Sound effect clip to play")] [SerializeField] diff --git a/Assets/Fungus/Scripts/Components/MusicManager.cs b/Assets/Fungus/Scripts/Components/MusicManager.cs index 9b969490..1dcaa7f0 100644 --- a/Assets/Fungus/Scripts/Components/MusicManager.cs +++ b/Assets/Fungus/Scripts/Components/MusicManager.cs @@ -80,7 +80,7 @@ namespace Fungus /// The sound effect clip to play. /// If the audioclip should loop or not. /// The volume level of the sound effect. - public virtual void PlayAmbienceSound(AudioClip soundClip, bool loop, float volume) + public virtual void PlayAmbianceSound(AudioClip soundClip, bool loop, float volume) { audioSource.loop = loop; audioSource.clip = soundClip; From c7e167124dbb42f842dbc16ceecca7b3bd4e2876 Mon Sep 17 00:00:00 2001 From: Hector Castelli Zacharias Date: Thu, 28 Jun 2018 19:05:52 -0400 Subject: [PATCH 4/7] Updated Ambiance Sound with new settings and generated Meta files. --- Assets/Fungus/Scripts/Commands/PlayAmbienceSound.cs | 2 +- .../Fungus/Scripts/Commands/PlayAmbienceSound.cs.meta | 11 +++++++++++ .../Fungus/Scripts/Commands/PlayRandomSound.cs.meta | 11 +++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 Assets/Fungus/Scripts/Commands/PlayAmbienceSound.cs.meta create mode 100644 Assets/Fungus/Scripts/Commands/PlayRandomSound.cs.meta diff --git a/Assets/Fungus/Scripts/Commands/PlayAmbienceSound.cs b/Assets/Fungus/Scripts/Commands/PlayAmbienceSound.cs index fcffb9b1..f6e28cdf 100644 --- a/Assets/Fungus/Scripts/Commands/PlayAmbienceSound.cs +++ b/Assets/Fungus/Scripts/Commands/PlayAmbienceSound.cs @@ -44,7 +44,7 @@ namespace Fungus var musicManager = FungusManager.Instance.MusicManager; - musicManager.PlayAmbienceSound(soundClip, loop, volume); + musicManager.PlayAmbianceSound(soundClip, loop, volume); Continue(); } diff --git a/Assets/Fungus/Scripts/Commands/PlayAmbienceSound.cs.meta b/Assets/Fungus/Scripts/Commands/PlayAmbienceSound.cs.meta new file mode 100644 index 00000000..17d2cdd6 --- /dev/null +++ b/Assets/Fungus/Scripts/Commands/PlayAmbienceSound.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1e466cf468c490144a240a8f7a566455 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Fungus/Scripts/Commands/PlayRandomSound.cs.meta b/Assets/Fungus/Scripts/Commands/PlayRandomSound.cs.meta new file mode 100644 index 00000000..a5c778b3 --- /dev/null +++ b/Assets/Fungus/Scripts/Commands/PlayRandomSound.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 04659f7200983fe40b49cc144086844b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: From 91781f8c582e9619ea282194f3d51ab0b5128a02 Mon Sep 17 00:00:00 2001 From: Hector Castelli Zacharias Date: Thu, 28 Jun 2018 20:09:44 -0400 Subject: [PATCH 5/7] Fixed issue when first creating a random sound component. --- Assets/Fungus/Scripts/Commands/PlayRandomSound.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Assets/Fungus/Scripts/Commands/PlayRandomSound.cs b/Assets/Fungus/Scripts/Commands/PlayRandomSound.cs index e3b0f953..7b0b7ffb 100644 --- a/Assets/Fungus/Scripts/Commands/PlayRandomSound.cs +++ b/Assets/Fungus/Scripts/Commands/PlayRandomSound.cs @@ -66,7 +66,8 @@ namespace Fungus string sounds = "["; foreach (AudioClip ac in soundClip) { - sounds+=ac.name+" ,"; + if(ac!=null) + sounds+=ac.name+" ,"; } sounds = sounds.TrimEnd(' ', ','); sounds += "]"; From 522b2d44d61cdbd940fb2960d6d38dee97e1c16c Mon Sep 17 00:00:00 2001 From: Hector Castelli Zacharias Date: Sat, 30 Jun 2018 11:16:02 -0400 Subject: [PATCH 6/7] Fixed issues where there woudln't be enough audio sources for the ambiance. Added Stop Ambiance command. --- ...yAmbienceSound.cs => PlayAmbianceSound.cs} | 0 .../Commands/PlayAmbienceSound.cs.meta | 11 --- .../Fungus/Scripts/Commands/StopAmbiance.cs | 35 +++++++++ .../Fungus/Scripts/Components/MusicManager.cs | 72 +++++++++++++------ 4 files changed, 84 insertions(+), 34 deletions(-) rename Assets/Fungus/Scripts/Commands/{PlayAmbienceSound.cs => PlayAmbianceSound.cs} (100%) delete mode 100644 Assets/Fungus/Scripts/Commands/PlayAmbienceSound.cs.meta create mode 100644 Assets/Fungus/Scripts/Commands/StopAmbiance.cs diff --git a/Assets/Fungus/Scripts/Commands/PlayAmbienceSound.cs b/Assets/Fungus/Scripts/Commands/PlayAmbianceSound.cs similarity index 100% rename from Assets/Fungus/Scripts/Commands/PlayAmbienceSound.cs rename to Assets/Fungus/Scripts/Commands/PlayAmbianceSound.cs diff --git a/Assets/Fungus/Scripts/Commands/PlayAmbienceSound.cs.meta b/Assets/Fungus/Scripts/Commands/PlayAmbienceSound.cs.meta deleted file mode 100644 index 17d2cdd6..00000000 --- a/Assets/Fungus/Scripts/Commands/PlayAmbienceSound.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 1e466cf468c490144a240a8f7a566455 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Fungus/Scripts/Commands/StopAmbiance.cs b/Assets/Fungus/Scripts/Commands/StopAmbiance.cs new file mode 100644 index 00000000..b3130c06 --- /dev/null +++ b/Assets/Fungus/Scripts/Commands/StopAmbiance.cs @@ -0,0 +1,35 @@ +// 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) + +using UnityEngine; + +namespace Fungus +{ + /// + /// Stops the currently playing game music. + /// + [CommandInfo("Audio", + "Stop Ambiance", + "Stops the currently playing game ambiance.")] + [AddComponentMenu("")] + public class StopAmbiance : Command + { + #region Public members + + public override void OnEnter() + { + var musicManager = FungusManager.Instance.MusicManager; + + musicManager.StopAmbiance(); + + Continue(); + } + + public override Color GetButtonColor() + { + return new Color32(242, 209, 176, 255); + } + + #endregion + } +} \ No newline at end of file diff --git a/Assets/Fungus/Scripts/Components/MusicManager.cs b/Assets/Fungus/Scripts/Components/MusicManager.cs index 1dcaa7f0..8ea094d9 100644 --- a/Assets/Fungus/Scripts/Components/MusicManager.cs +++ b/Assets/Fungus/Scripts/Components/MusicManager.cs @@ -1,7 +1,7 @@ // 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) -using UnityEngine; +using UnityEngine; namespace Fungus { @@ -9,14 +9,25 @@ namespace Fungus /// Music manager which provides basic music and sound effect functionality. /// Music playback persists across scene loads. /// - [RequireComponent(typeof(AudioSource))] + //[RequireComponent(typeof(AudioSource))] public class MusicManager : MonoBehaviour { protected AudioSource audioSource; + protected AudioSource audioSourceAmbiance; + + void Reset() + { + int audioSourceCount = this.GetComponents().Length; + for (int i = 0; i < 2 - audioSourceCount; i++) + gameObject.AddComponent(); + + } protected virtual void Awake() { - audioSource = GetComponent(); + Reset(); + audioSource = GetComponents()[0]; + audioSourceAmbiance = GetComponents()[1]; } protected virtual void Start() @@ -50,10 +61,10 @@ namespace Fungus float startVolume = audioSource.volume; LeanTween.value(gameObject, startVolume, 0f, fadeDuration) - .setOnUpdate( (v) => { + .setOnUpdate((v) => { // Fade out current music audioSource.volume = v; - }).setOnComplete( () => { + }).setOnComplete(() => { // Play new music audioSource.volume = startVolume; audioSource.clip = musicClip; @@ -82,10 +93,10 @@ namespace Fungus /// The volume level of the sound effect. public virtual void PlayAmbianceSound(AudioClip soundClip, bool loop, float volume) { - audioSource.loop = loop; - audioSource.clip = soundClip; - audioSource.volume = volume; - audioSource.Play(); + audioSourceAmbiance.loop = loop; + audioSourceAmbiance.clip = soundClip; + audioSourceAmbiance.volume = volume; + audioSourceAmbiance.Play(); } /// @@ -99,6 +110,7 @@ namespace Fungus if (Mathf.Approximately(duration, 0f)) { audioSource.pitch = pitch; + audioSourceAmbiance.pitch = pitch; if (onComplete != null) { onComplete(); @@ -106,12 +118,15 @@ namespace Fungus return; } - LeanTween.value(gameObject, - audioSource.pitch, - pitch, - duration).setOnUpdate( (p) => { + LeanTween.value(gameObject, + audioSource.pitch, + pitch, + duration).setOnUpdate((p) => + { audioSource.pitch = p; - }).setOnComplete( () => { + audioSourceAmbiance.pitch = p; + }).setOnComplete(() => + { if (onComplete != null) { onComplete(); @@ -129,20 +144,22 @@ namespace Fungus { if (Mathf.Approximately(duration, 0f)) { - if (onComplete != null) - { - onComplete(); - } + if (onComplete != null) + { + onComplete(); + } audioSource.volume = volume; + audioSourceAmbiance.volume = volume; return; } - LeanTween.value(gameObject, - audioSource.volume, - volume, - duration).setOnUpdate( (v) => { + LeanTween.value(gameObject, + audioSource.volume, + volume, + duration).setOnUpdate((v) => { audioSource.volume = v; - }).setOnComplete( () => { + audioSourceAmbiance.volume = v; + }).setOnComplete(() => { if (onComplete != null) { onComplete(); @@ -159,6 +176,15 @@ namespace Fungus audioSource.clip = null; } + /// + /// Stops playing game ambiance. + /// + public virtual void StopAmbiance() + { + audioSourceAmbiance.Stop(); + audioSourceAmbiance.clip = null; + } + #endregion } } \ No newline at end of file From 7fab22dc9deae7f442b04185121a2a1e4da367d5 Mon Sep 17 00:00:00 2001 From: Hector Castelli Zacharias Date: Fri, 27 Jul 2018 20:54:54 -0400 Subject: [PATCH 7/7] Updated the comments and documentation in the commands. --- Assets/Fungus/Scripts/Commands/PlayAmbianceSound.cs | 2 +- .../Fungus/Scripts/Commands/PlayAmbianceSound.cs.meta | 11 +++++++++++ Assets/Fungus/Scripts/Commands/PlayRandomSound.cs | 2 +- Assets/Fungus/Scripts/Commands/StopAmbiance.cs.meta | 11 +++++++++++ 4 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 Assets/Fungus/Scripts/Commands/PlayAmbianceSound.cs.meta create mode 100644 Assets/Fungus/Scripts/Commands/StopAmbiance.cs.meta diff --git a/Assets/Fungus/Scripts/Commands/PlayAmbianceSound.cs b/Assets/Fungus/Scripts/Commands/PlayAmbianceSound.cs index f6e28cdf..de01f162 100644 --- a/Assets/Fungus/Scripts/Commands/PlayAmbianceSound.cs +++ b/Assets/Fungus/Scripts/Commands/PlayAmbianceSound.cs @@ -10,7 +10,7 @@ namespace Fungus /// [CommandInfo("Audio", "Play Ambiance Sound", - "Plays a once-off sound effect. Multiple sound effects can be played at the same time.")] + "Plays a background sound to be overlayed on top of the music. Only one Ambiance can be played at a time.")] [AddComponentMenu("")] public class PlayAmbianceSound : Command { diff --git a/Assets/Fungus/Scripts/Commands/PlayAmbianceSound.cs.meta b/Assets/Fungus/Scripts/Commands/PlayAmbianceSound.cs.meta new file mode 100644 index 00000000..6465ec8e --- /dev/null +++ b/Assets/Fungus/Scripts/Commands/PlayAmbianceSound.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 831c208af3cccf24b80cc6f3768919bc +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Fungus/Scripts/Commands/PlayRandomSound.cs b/Assets/Fungus/Scripts/Commands/PlayRandomSound.cs index 7b0b7ffb..3b1e2daf 100644 --- a/Assets/Fungus/Scripts/Commands/PlayRandomSound.cs +++ b/Assets/Fungus/Scripts/Commands/PlayRandomSound.cs @@ -10,7 +10,7 @@ namespace Fungus /// [CommandInfo("Audio", "Play Random Sound", - "Plays a once-off sound effect. Multiple sound effects can be played at the same time.")] + "Plays a once-off sound effect from a list of available sound effects. Multiple sound effects can be played at the same time.")] [AddComponentMenu("")] public class PlayRandomSound : Command { diff --git a/Assets/Fungus/Scripts/Commands/StopAmbiance.cs.meta b/Assets/Fungus/Scripts/Commands/StopAmbiance.cs.meta new file mode 100644 index 00000000..b4e0d210 --- /dev/null +++ b/Assets/Fungus/Scripts/Commands/StopAmbiance.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4bce3bd679be2c94196f3f6aaeb33f0b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: