You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
1.8 KiB
78 lines
1.8 KiB
11 years ago
|
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))]
|
||
10 years ago
|
public class MusicController : MonoBehaviour
|
||
11 years ago
|
{
|
||
10 years ago
|
static MusicController instance;
|
||
11 years ago
|
|
||
|
/**
|
||
10 years ago
|
* Returns the MusicController singleton instance.
|
||
|
* Will create a MusicController game object if none currently exists.
|
||
11 years ago
|
*/
|
||
10 years ago
|
static public MusicController GetInstance()
|
||
11 years ago
|
{
|
||
|
if (instance == null)
|
||
|
{
|
||
10 years ago
|
GameObject go = new GameObject("MusicController");
|
||
11 years ago
|
DontDestroyOnLoad(go);
|
||
10 years ago
|
instance = go.AddComponent<MusicController>();
|
||
11 years ago
|
}
|
||
|
|
||
|
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);
|
||
|
}
|
||
|
}
|
||
|
}
|