From 81efec8f6d1fd61519fc85e839d962f62eedff73 Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Wed, 25 Jun 2014 11:39:44 +0100 Subject: [PATCH] Added MusicManager singleton to play music across scene loads --- .../Fungus/Scripts/Commands/AudioCommands.cs | 113 ------------------ Assets/Fungus/Scripts/GameController.cs | 40 ++++--- Assets/Fungus/Scripts/MusicManager.cs | 78 ++++++++++++ ...oCommands.cs.meta => MusicManager.cs.meta} | 2 +- 4 files changed, 103 insertions(+), 130 deletions(-) delete mode 100644 Assets/Fungus/Scripts/Commands/AudioCommands.cs create mode 100644 Assets/Fungus/Scripts/MusicManager.cs rename Assets/Fungus/Scripts/{Commands/AudioCommands.cs.meta => MusicManager.cs.meta} (78%) diff --git a/Assets/Fungus/Scripts/Commands/AudioCommands.cs b/Assets/Fungus/Scripts/Commands/AudioCommands.cs deleted file mode 100644 index 77021a68..00000000 --- a/Assets/Fungus/Scripts/Commands/AudioCommands.cs +++ /dev/null @@ -1,113 +0,0 @@ -using UnityEngine; -using System; -using System.Collections; - -namespace Fungus -{ - /** - * Command classes have their own namespace to prevent them popping up in code completion - */ - namespace Command - { - /** - * Plays a music clip - */ - public class PlayMusic : CommandQueue.Command - { - AudioClip audioClip; - - public PlayMusic(AudioClip _audioClip) - { - if (_audioClip == null) - { - Debug.LogError("Audio clip must not be null."); - return; - } - - audioClip = _audioClip; - } - - public override void Execute(CommandQueue commandQueue, Action onComplete) - { - Game game = Game.GetInstance(); - - game.audio.clip = audioClip; - game.audio.Play(); - - if (onComplete != null) - { - onComplete(); - } - } - } - - /** - * Stops a music clip - */ - public class StopMusic : CommandQueue.Command - { - public override void Execute(CommandQueue commandQueue, Action onComplete) - { - Game game = Game.GetInstance(); - game.audio.Stop(); - - if (onComplete != null) - { - onComplete(); - } - } - } - - /** - * Fades music volume to required level over a period of time - */ - public class SetMusicVolume : CommandQueue.Command - { - float musicVolume; - float duration; - - public SetMusicVolume(float _musicVolume, float _duration) - { - musicVolume = _musicVolume; - duration = _duration; - } - - public override void Execute(CommandQueue commandQueue, Action onComplete) - { - Game game = Game.GetInstance(); - iTween.AudioTo(game.gameObject, musicVolume, 1f, duration); - - if (onComplete != null) - { - onComplete(); - } - } - } - - /** - * Plays a sound effect once - */ - public class PlaySound : CommandQueue.Command - { - AudioClip audioClip; - float volume; - - public PlaySound(AudioClip _audioClip, float _volume) - { - audioClip = _audioClip; - volume = _volume; - } - - public override void Execute(CommandQueue commandQueue, Action onComplete) - { - Game game = Game.GetInstance(); - game.audio.PlayOneShot(audioClip, volume); - - if (onComplete != null) - { - onComplete(); - } - } - } - } -} \ No newline at end of file diff --git a/Assets/Fungus/Scripts/GameController.cs b/Assets/Fungus/Scripts/GameController.cs index 3cc44a8e..4ffbd2f7 100644 --- a/Assets/Fungus/Scripts/GameController.cs +++ b/Assets/Fungus/Scripts/GameController.cs @@ -500,12 +500,14 @@ namespace Fungus /** * Plays game music using an audio clip. * One music clip may be played at a time. - * @param audioClip The music clip to play + * @param musicClip The music clip to play */ - public static void PlayMusic(AudioClip audioClip) + public static void PlayMusic(AudioClip musicClip) { CommandQueue commandQueue = Game.GetInstance().commandQueue; - commandQueue.AddCommand(new Command.PlayMusic(audioClip)); + commandQueue.AddCommand(new Command.Call(delegate { + MusicManager.GetInstance().PlayMusic(musicClip); + })); } /** @@ -514,49 +516,55 @@ namespace Fungus public static void StopMusic() { CommandQueue commandQueue = Game.GetInstance().commandQueue; - commandQueue.AddCommand(new Command.StopMusic()); + commandQueue.AddCommand(new Command.Call(delegate { + MusicManager.GetInstance().StopMusic(); + })); } /** * Sets the game music volume immediately. - * @param musicVolume The new music volume value [0..1] + * @param volume The new music volume value [0..1] */ - public static void SetMusicVolume(float musicVolume) + public static void SetMusicVolume(float volume) { - SetMusicVolume(musicVolume, 0f); + SetMusicVolume(volume, 0f); } /** * Fades the game music volume to required level over a period of time. - * @param musicVolume 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. */ - public static void SetMusicVolume(float musicVolume, float duration) + public static void SetMusicVolume(float volume, float duration) { CommandQueue commandQueue = Game.GetInstance().commandQueue; - commandQueue.AddCommand(new Command.SetMusicVolume(musicVolume, duration)); + commandQueue.AddCommand(new Command.Call(delegate { + MusicManager.GetInstance().SetMusicVolume(volume, duration); + })); } /** * Plays a sound effect once. * Multiple sound effects can be played at the same time. - * @param audioClip The sound effect clip to play + * @param soundClip The sound effect clip to play */ - public static void PlaySound(AudioClip audioClip) + public static void PlaySound(AudioClip soundClip) { - PlaySound(audioClip, 1f); + PlaySound(soundClip, 1f); } /** * Plays a sound effect once, at the specified volume. * Multiple sound effects can be played at the same time. - * @param audioClip The sound effect clip to play + * @param soundClip The sound effect clip to play * @param volume The volume level of the sound effect */ - public static void PlaySound(AudioClip audioClip, float volume) + public static void PlaySound(AudioClip soundClip, float volume) { CommandQueue commandQueue = Game.GetInstance().commandQueue; - commandQueue.AddCommand(new Command.PlaySound(audioClip, volume)); + commandQueue.AddCommand(new Command.Call(delegate { + MusicManager.GetInstance().PlaySound(soundClip, volume); + })); } #endregion diff --git a/Assets/Fungus/Scripts/MusicManager.cs b/Assets/Fungus/Scripts/MusicManager.cs new file mode 100644 index 00000000..d06e895a --- /dev/null +++ b/Assets/Fungus/Scripts/MusicManager.cs @@ -0,0 +1,78 @@ +using UnityEngine; +using System.Collections; + +namespace Fungus +{ + /** + * Singleton music manager component. + * Provides basic music and sound effect functionality. + * Music playback persists across scene loads. + */ + [RequireComponent(typeof(AudioSource))] + public class MusicManager : MonoBehaviour + { + static MusicManager instance; + + /** + * Returns the MusicManager singleton instance. + * Will create a MusicManager game object if none currently exists. + */ + static public MusicManager GetInstance() + { + if (instance == null) + { + GameObject go = new GameObject("MusicManager"); + DontDestroyOnLoad(go); + instance = go.AddComponent(); + } + + return instance; + } + + void Start() + { + audio.playOnAwake = false; + audio.loop = true; + } + + /** + * Plays game music using an audio clip. + * One music clip may be played at a time. + * @param musicClip The music clip to play + */ + public void PlayMusic(AudioClip musicClip) + { + audio.clip = musicClip; + audio.Play(); + } + + /** + * Stops playing game music. + */ + public void StopMusic() + { + audio.Stop(); + } + + /** + * Fades the game music volume to required level over a period of time. + * @param volume The new music volume value [0..1] + * @param duration The length of time in seconds needed to complete the volume change. + */ + public void SetMusicVolume(float volume, float duration) + { + iTween.AudioTo(gameObject, volume, 1f, duration); + } + + /** + * Plays a sound effect once, at the specified volume. + * Multiple sound effects can be played at the same time. + * @param soundClip The sound effect clip to play + * @param volume The volume level of the sound effect + */ + public void PlaySound(AudioClip soundClip, float volume) + { + audio.PlayOneShot(soundClip, volume); + } + } +} \ No newline at end of file diff --git a/Assets/Fungus/Scripts/Commands/AudioCommands.cs.meta b/Assets/Fungus/Scripts/MusicManager.cs.meta similarity index 78% rename from Assets/Fungus/Scripts/Commands/AudioCommands.cs.meta rename to Assets/Fungus/Scripts/MusicManager.cs.meta index 3ee59fd1..7d112d1b 100644 --- a/Assets/Fungus/Scripts/Commands/AudioCommands.cs.meta +++ b/Assets/Fungus/Scripts/MusicManager.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: bc443341450b64de790b66416177cca7 +guid: a5ff9b322bf3e407fb29148a2e50d076 MonoImporter: serializedVersion: 2 defaultReferences: []