// Felix-Bang:FBSound //   へ     /| //  /\7    ∠_/ //  / │   / / // │ Z _,< /   /`ヽ // │     ヽ   /  〉 //  Y     `  /  / // イ● 、 ●  ⊂⊃〈  / // ()  へ    | \〈 //  >ー 、_  ィ  │ // //  / へ   / ノ<| \\ //  ヽ_ノ  (_/  │// //  7       |/ //  >―r ̄ ̄`ー―_ // Describe: // Createtime: using System.Collections; using System.Collections.Generic; using UnityEngine; namespace FBFramework { public class FBSound : FBSingleton { public string ResourceDir = ""; AudioSource f_bgm; AudioSource f_effectSound; public float BgmVolum { get { return f_bgm.volume; } set { f_bgm.volume = value; } } public float EffectVolum { get { return f_effectSound.volume; } set { f_effectSound.volume = value; } } protected override void Awake() { base.Awake(); f_bgm = this.gameObject.AddComponent(); f_bgm.playOnAwake = true; f_bgm.loop = true; f_effectSound = this.gameObject.AddComponent(); } /// 播放背景音乐 public void PlayBgm(string audioName) { string oldName = ""; if (f_bgm.clip == null) oldName = ""; else oldName = f_bgm.clip.name; if (string.IsNullOrEmpty(oldName)) { AudioClip clip = GetClip(audioName); if (clip != null) { f_bgm.clip = clip; f_bgm.Play(); } } } /// 关闭背景音乐 public void StopBgm() { f_bgm.Stop(); f_bgm.clip = null; } /// 播放音效 public void PlayEffect(string audioName) { AudioClip clip = GetClip(audioName); f_effectSound.PlayOneShot(clip); } private AudioClip GetClip(string audioName) { string path = string.Empty; if (string.IsNullOrEmpty(ResourceDir)) path = string.Empty; else path = ResourceDir + "/" + audioName; return Resources.Load(path); } } }