using UnityEngine;
using System.Collections;
public class SoundManager : MonoBehaviour {
private static SoundManager instance;
public static SoundManager Instance{
get { return instance;}
}
public string ResourceDir = "Sounds";
private AudioSource audioSource;
void Awake(){
instance = this;
audioSource = GetComponent<AudioSource> ();
audioSource.loop = true;
audioSource.playOnAwake = false;
}
public bool Mute{
get { return audioSource.mute;}
set{
audioSource.mute = value;
//PlayerPrefs.SetInt ("Mute", value ? 1 : 0);
}
}
public float BGVolume{
get{return audioSource.volume;}
set{audioSource.volume = value;}
}
public void PlayBGM(string name){
string path = ResourceDir + "/" + name;
AudioClip ac = Resources.Load <AudioClip>(path);
audioSource.clip = ac;
audioSource.Play ();
Debug.Log ("should play bgm");
}
public void StopBGm(){
audioSource.clip = null;
audioSource.Stop ();
}
public void PlayAudio(string name){
string path = ResourceDir + "/" + name;
AudioClip ac = Resources.Load <AudioClip>(path);
AudioSource.PlayClipAtPoint (ac,Vector2.zero);
}
}
全局脚本,调用起来很方便。
例如: SoundManager.Instance.PlayAudio (“兄贵 - 开头”);