chrisgregan
11 years ago
4 changed files with 103 additions and 130 deletions
@ -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(); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
@ -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); |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue