Browse Source

Added MusicManager singleton to play music across scene loads

master
chrisgregan 11 years ago
parent
commit
81efec8f6d
  1. 113
      Assets/Fungus/Scripts/Commands/AudioCommands.cs
  2. 40
      Assets/Fungus/Scripts/GameController.cs
  3. 78
      Assets/Fungus/Scripts/MusicManager.cs
  4. 2
      Assets/Fungus/Scripts/MusicManager.cs.meta

113
Assets/Fungus/Scripts/Commands/AudioCommands.cs

@ -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();
}
}
}
}
}

40
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

78
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<MusicManager>();
}
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);
}
}
}

2
Assets/Fungus/Scripts/Commands/AudioCommands.cs.meta → Assets/Fungus/Scripts/MusicManager.cs.meta

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: bc443341450b64de790b66416177cca7
guid: a5ff9b322bf3e407fb29148a2e50d076
MonoImporter:
serializedVersion: 2
defaultReferences: []
Loading…
Cancel
Save