Browse Source

Merge pull request #694 from HectorCastelli/master

Adding Random Sound Command and Ambiance Sounds
master
Chris Gregan 6 years ago committed by GitHub
parent
commit
6c64173f51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 69
      Assets/Fungus/Scripts/Commands/PlayAmbianceSound.cs
  2. 11
      Assets/Fungus/Scripts/Commands/PlayAmbianceSound.cs.meta
  3. 84
      Assets/Fungus/Scripts/Commands/PlayRandomSound.cs
  4. 11
      Assets/Fungus/Scripts/Commands/PlayRandomSound.cs.meta
  5. 35
      Assets/Fungus/Scripts/Commands/StopAmbiance.cs
  6. 11
      Assets/Fungus/Scripts/Commands/StopAmbiance.cs.meta
  7. 78
      Assets/Fungus/Scripts/Components/MusicManager.cs

69
Assets/Fungus/Scripts/Commands/PlayAmbianceSound.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
{
/// <summary>
/// Plays a once-off sound effect. Multiple sound effects can be played at the same time.
/// </summary>
[CommandInfo("Audio",
"Play Ambiance Sound",
"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
{
[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.PlayAmbianceSound(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
}
}

11
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:

84
Assets/Fungus/Scripts/Commands/PlayRandomSound.cs

@ -0,0 +1,84 @@
// 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
{
/// <summary>
/// Plays a once-off sound effect. Multiple sound effects can be played at the same time.
/// </summary>
[CommandInfo("Audio",
"Play Random Sound",
"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
{
[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) {
if(ac!=null)
sounds+=ac.name+" ,";
}
sounds = sounds.TrimEnd(' ', ',');
sounds += "]";
return "Random sounds "+sounds;
}
public override Color GetButtonColor()
{
return new Color32(242, 209, 176, 255);
}
#endregion
}
}

11
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:

35
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
{
/// <summary>
/// Stops the currently playing game music.
/// </summary>
[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
}
}

11
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:

78
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.
/// </summary>
[RequireComponent(typeof(AudioSource))]
//[RequireComponent(typeof(AudioSource))]
public class MusicManager : MonoBehaviour
{
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()
{
audioSource = GetComponent<AudioSource>();
Reset();
audioSource = GetComponents<AudioSource>()[0];
audioSourceAmbiance = GetComponents<AudioSource>()[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;
@ -74,6 +85,20 @@ namespace Fungus
audioSource.PlayOneShot(soundClip, volume);
}
/// <summary>
/// Plays a sound effect with optional looping values, at the specified volume.
/// </summary>
/// <param name="soundClip">The sound effect clip to play.</param>
/// <param name="loop">If the audioclip should loop or not.</param>
/// <param name="volume">The volume level of the sound effect.</param>
public virtual void PlayAmbianceSound(AudioClip soundClip, bool loop, float volume)
{
audioSourceAmbiance.loop = loop;
audioSourceAmbiance.clip = soundClip;
audioSourceAmbiance.volume = volume;
audioSourceAmbiance.Play();
}
/// <summary>
/// Shifts the game music pitch to required value over a period of time.
/// </summary>
@ -85,6 +110,7 @@ namespace Fungus
if (Mathf.Approximately(duration, 0f))
{
audioSource.pitch = pitch;
audioSourceAmbiance.pitch = pitch;
if (onComplete != null)
{
onComplete();
@ -92,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();
@ -115,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();
@ -145,6 +176,15 @@ namespace Fungus
audioSource.clip = null;
}
/// <summary>
/// Stops playing game ambiance.
/// </summary>
public virtual void StopAmbiance()
{
audioSourceAmbiance.Stop();
audioSourceAmbiance.clip = null;
}
#endregion
}
}
Loading…
Cancel
Save