背景音乐管理器
using UnityEngine; using System.Collections; /// <summary> /// 背景音乐管理器 /// </summary> public class BgMusic : MonoBehaviour { public static AudioSource bgMusic;//播放器 void Start () { if(bgMusic == null){ bgMusic = transform.Find("BgMusic").gameObject.AddComponent<AudioSource>(); } } /// <summary> /// 播放背景音乐 /// </summary> public static void PlayMusic(string levelPath){ //根据关卡,获取不同的音频路径 string path = null; switch(levelPath){ case "MainMenu": path = "Sound/BgMusic/menu";break; case "Village": path = "Sound/BgMusic/village";break; case "Cemetery": path = "Sound/BgMusic/danger";break; case "Grave": path = "Sound/BgMusic/outdoor";break; } AudioClip clip = Resources.Load(path) as AudioClip; bgMusic.clip = clip; bgMusic.volume = UISetting.BgMusicVolume; bgMusic.loop = true; bgMusic.Play(); } /// <summary> /// 设置音量大小 /// </summary> public static void SetVolume(float volume){ bgMusic.volume = volume; } }