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.
133 lines
4.3 KiB
133 lines
4.3 KiB
8 years ago
|
// 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)
|
||
9 years ago
|
|
||
11 years ago
|
using UnityEngine;
|
||
|
|
||
|
namespace Fungus
|
||
|
{
|
||
8 years ago
|
[RequireComponent(typeof(AudioSource))]
|
||
8 years ago
|
public class MusicController : MonoBehaviour, IMusicController
|
||
8 years ago
|
{
|
||
8 years ago
|
static IMusicController instance;
|
||
8 years ago
|
|
||
8 years ago
|
/// <summary>
|
||
|
/// Returns the MusicController singleton instance.
|
||
|
/// Will create a MusicController game object if none currently exists.
|
||
|
/// </summary>
|
||
|
static public IMusicController GetInstance()
|
||
8 years ago
|
{
|
||
|
if (instance == null)
|
||
|
{
|
||
|
GameObject go = new GameObject("MusicController");
|
||
|
DontDestroyOnLoad(go);
|
||
|
instance = go.AddComponent<MusicController>();
|
||
|
}
|
||
|
|
||
|
return instance;
|
||
|
}
|
||
|
|
||
|
protected virtual void Start()
|
||
|
{
|
||
|
GetComponent<AudioSource>().playOnAwake = false;
|
||
|
GetComponent<AudioSource>().loop = true;
|
||
|
}
|
||
8 years ago
|
|
||
|
#region IMusicController implementation
|
||
8 years ago
|
|
||
|
public void PlayMusic(AudioClip musicClip, bool loop, float fadeDuration, float atTime)
|
||
|
{
|
||
|
AudioSource audioSource = GetComponent<AudioSource>();
|
||
|
if (audioSource == null || audioSource.clip == musicClip)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (fadeDuration == 0f)
|
||
|
{
|
||
|
audioSource.clip = musicClip;
|
||
|
audioSource.loop = loop;
|
||
|
audioSource.time = atTime; // May be inaccurate if the audio source is compressed http://docs.unity3d.com/ScriptReference/AudioSource-time.html BK
|
||
|
audioSource.Play();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
float startVolume = audioSource.volume;
|
||
|
|
||
|
LeanTween.value(gameObject, startVolume, 0f, fadeDuration)
|
||
|
.setOnUpdate( (v) => {
|
||
|
// Fade out current music
|
||
|
audioSource.volume = v;
|
||
|
}).setOnComplete( () => {
|
||
|
// Play new music
|
||
|
audioSource.volume = startVolume;
|
||
|
audioSource.clip = musicClip;
|
||
|
audioSource.loop = loop;
|
||
|
audioSource.time = atTime; // May be inaccurate if the audio source is compressed http://docs.unity3d.com/ScriptReference/AudioSource-time.html BK
|
||
|
audioSource.Play();
|
||
|
});
|
||
|
}
|
||
|
}
|
||
8 years ago
|
|
||
|
public virtual void PlaySound(AudioClip soundClip, float volume)
|
||
8 years ago
|
{
|
||
8 years ago
|
GetComponent<AudioSource>().PlayOneShot(soundClip, volume);
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
public virtual void SetAudioPitch(float pitch, float duration, System.Action onComplete)
|
||
8 years ago
|
{
|
||
|
AudioSource audio = GetComponent<AudioSource>();
|
||
9 years ago
|
|
||
8 years ago
|
if (duration == 0f)
|
||
8 years ago
|
{
|
||
8 years ago
|
audio.pitch = pitch;
|
||
|
if (onComplete != null)
|
||
|
{
|
||
|
onComplete();
|
||
|
}
|
||
8 years ago
|
return;
|
||
|
}
|
||
|
|
||
|
LeanTween.value(gameObject,
|
||
8 years ago
|
audio.pitch,
|
||
|
pitch,
|
||
|
duration).setOnUpdate( (p) => {
|
||
|
audio.pitch = p;
|
||
9 years ago
|
}).setOnComplete( () => {
|
||
|
if (onComplete != null)
|
||
|
{
|
||
|
onComplete();
|
||
|
}
|
||
|
});
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
public virtual void SetAudioVolume(float volume, float duration, System.Action onComplete)
|
||
8 years ago
|
{
|
||
|
AudioSource audio = GetComponent<AudioSource>();
|
||
|
|
||
8 years ago
|
if (Mathf.Approximately(duration, 0f))
|
||
8 years ago
|
{
|
||
8 years ago
|
audio.volume = volume;
|
||
8 years ago
|
return;
|
||
|
}
|
||
|
|
||
|
LeanTween.value(gameObject,
|
||
8 years ago
|
audio.volume,
|
||
|
volume,
|
||
|
duration).setOnUpdate( (v) => {
|
||
|
audio.volume = v;
|
||
8 years ago
|
}).setOnComplete( () => {
|
||
|
if (onComplete != null)
|
||
|
{
|
||
|
onComplete();
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
8 years ago
|
public virtual void StopMusic()
|
||
8 years ago
|
{
|
||
8 years ago
|
GetComponent<AudioSource>().Stop();
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
#endregion
|
||
8 years ago
|
}
|
||
11 years ago
|
}
|