using UnityEngine; using System.Collections; using UnityEngine.SceneManagement; using System.Runtime.CompilerServices; /// /// 音频管理类 /// public class AudioManager : MonoSingleton { // private static AudioManager _instance; //public static AudioManager Instance; //{ // get // { // //if (_instance == null) // //{ // // string name = "SoundManager"; // // GameObject manager = GameObject.Find("SoundManager"); // // if (manager == null) // // { // // manager = new GameObject(name); // // _instance = manager.AddComponent(); // // } // // else // // { // // _instance = manager.GetComponent(); // // if (_instance == null) // // { // // _instance = manager.AddComponent(); // // } // // } // //} // return _instance; // } //} private AudioSource audioSound; //音效播放器 private AudioSource audioMusic; //背景音乐播放 private AudioSource audioLanguage; //配音播放器 public AudioClip[] sceses_BJList; //主背景音效 public AudioClip[] sceses_SanGuoList;//三国场景音效 public AudioClip[] scences_Aquarium;//海洋馆音效 public AudioClip[] sceses_GongYeList_OBE;//工业obe场景音效 public AudioClip[] sceses_GongYeList_GKJ;//工业光刻机场景音效 public AudioClip[] sceses_GongYeList_DT;//工业地铁内抛图场景音效 public AudioClip[] sceses_YiLiaoList;//医疗场景音效 public AudioClip[] sceses_WeiLaiChengShiList;//未来城市 public AudioClip[] sceses_XiaoFangAnQuanList;//消防安全音效 public AudioClip[] sceses_JiaoYuList;//化学教育音效 public AudioClip[] sceses_BoWuGunList;//博物馆音效 public AudioClip[] sceses_PartyBuildingList;//党建音效 public float bjSoundVolume; /// /// 背景音效 /// /// public void PlayBJSounds(string str) { Debug.Log(str); switch (str) { case "bjSound_SanGuo": PlayMusic((AudioClip)sceses_BJList[0], true); break; case "bjSound2_GongYe": PlayMusic((AudioClip)sceses_BJList[1], true); break; case "bjSound3_RenTi": PlayMusic((AudioClip)sceses_BJList[2], true); break; case "bjSound4_JiaoYu": PlayMusic((AudioClip)sceses_BJList[3], true); break; case "bjSound5_WeiLaiChengShi": PlayMusic((AudioClip)sceses_BJList[4], true); break; case "bjSound6_FireSafe": PlayMusic((AudioClip)sceses_BJList[5], true); break; case "bjSound5_BoWuGuan": PlayMusic((AudioClip)sceses_BJList[6], true); break; case "bjSound5_PartyBuilding": PlayMusic((AudioClip)sceses_BJList[7], true); break; case "None": PlayMusic((AudioClip)sceses_BJList[8], false); break; } } /// /// 三国场景音效 /// /// public void AudioSanGuo_LanguageSounds(string str) { switch (str) { case "SG_HLG"://虎牢关 三英战吕布 PlayLanguage(sceses_SanGuoList[0]); break; case "SG_TYSJY"://桃园三结义 PlayLanguage(sceses_SanGuoList[1]); break; case "SG_HSCB"://火烧赤壁 PlayLanguage(sceses_SanGuoList[2]); break; case "SG_YMSJ"://辕门射戟 PlayLanguage(sceses_SanGuoList[3]); break; } } /// /// OBE /// /// public void AudioGongYe_OBE_LanguageSounds(string str) { switch (str) { case "GY_OBE_SoundStart": PlayLanguage(sceses_GongYeList_OBE[0]); break; case "GY_OBE_Sound1": PlayLanguage(sceses_GongYeList_OBE[1]); break; case "GY_OBE_Sound2": PlayLanguage(sceses_GongYeList_OBE[2]); break; case "GY_OBE_Sound3": PlayLanguage(sceses_GongYeList_OBE[3]); break; case "GY_OBE_Sound4": PlayLanguage(sceses_GongYeList_OBE[4]); break; case "GY_OBE_Sound5": PlayLanguage(sceses_GongYeList_OBE[5]); break; case "GY_OBE_SoundEnd": PlayLanguage(sceses_GongYeList_OBE[6]); break; } } /// /// 光刻机 /// /// public void AudioGongYe_GKJ_LanguageSounds(string str) { switch (str) { case "GY_GKJ1": PlayLanguage(sceses_GongYeList_GKJ[0]); break; case "GY_GKJ2": PlayLanguage(sceses_GongYeList_GKJ[1]); break; case "GY_GKJ3": PlayLanguage(sceses_GongYeList_GKJ[2]); break; case "GY_GKJ4": PlayLanguage(sceses_GongYeList_GKJ[3]); break; case "GY_GKJ5": PlayLanguage(sceses_GongYeList_GKJ[4]); break; case "GY_GKJ6": PlayLanguage(sceses_GongYeList_GKJ[5]); break; case "GY_GKJ_Start": PlayLanguage(sceses_GongYeList_GKJ[6]); break; } } /// /// 地铁内视图 /// /// public void AudioGongYe_DT_LanguageSounds(string str) { Debug.Log("str"); switch (str) { case "GY_dianliqianyin"://电力牵引机 PlayLanguage(sceses_GongYeList_DT[0]); break; case "GY_guagqian"://光纤交换机 PlayLanguage(sceses_GongYeList_DT[1]); break; case "GY_luyouqi"://路由器 PlayLanguage(sceses_GongYeList_DT[2]); break; case "GY_yitaiwang"://以太网交换机 PlayLanguage(sceses_GongYeList_DT[3]); break; case "GY_jiankong"://.监控系统 PlayLanguage(sceses_GongYeList_DT[4]); break; case "GY_zhuanhuanqi"://转换器 PlayLanguage(sceses_GongYeList_DT[5]); break; case "GY_dianji"://电机 PlayLanguage(sceses_GongYeList_DT[6]); break; case "GY_kongtiao"://空调通风系统 PlayLanguage(sceses_GongYeList_DT[7]); break; case "GY_DT"://空调通风系统 PlayLanguage(sceses_GongYeList_DT[8]); break; } } /// ///医疗音效 /// /// public void AudioYiLiao_LanguageSounds(string str) { switch (str) { case "YL_Start": PlayLanguage(sceses_YiLiaoList[0]); break; } } /// ///未来城市 /// /// public void AudioWLCS_LanguageSounds(string str) { switch (str) { case "WLCS_Start": PlayLanguage(sceses_WeiLaiChengShiList[0]); break; } } /// /// 消防安全 /// /// public void AudioHZ_LanguageSounds(string str) { switch (str) { case "XFAQ_Start": PlayLanguage(sceses_XiaoFangAnQuanList[0]); break; } } /// /// 化学教育音效 /// /// public void AudioJiaoYu_LanguageSounds(string str) { switch (str) { case "HX_SoundStart": PlayLanguage(sceses_JiaoYuList[0]); break; case "HX_1": PlayLanguage(sceses_JiaoYuList[1]); break; case "HX_2": PlayLanguage(sceses_JiaoYuList[2]); break; case "HX_3": PlayLanguage(sceses_JiaoYuList[3]); break; case "HX_4": PlayLanguage(sceses_JiaoYuList[4]); break; case "HX_5": PlayLanguage(sceses_JiaoYuList[5]); break; case "HX_6": PlayLanguage(sceses_JiaoYuList[6]); break; } } /// /// 博物馆音效 /// /// public void AudioBoWuGun_LanguageSounds(string str) { Debug.Log(str); switch (str) { case "matafeiyan": PlayLanguage(sceses_BoWuGunList[0]); break; case "yuwanggoujianjian": PlayLanguage(sceses_BoWuGunList[1]); break; case "tangsancai": PlayLanguage(sceses_BoWuGunList[2]); break; case "qinghuaci": PlayLanguage(sceses_BoWuGunList[3]); break; case "BWG_end": PlayLanguage(sceses_BoWuGunList[4]); break; } } /// /// 党建音乐 /// /// public void AudioPartyBuilding_LanguageSounds(string str) { switch (str) { case "PartyBuilding_zgcl"://中共成立 PlayLanguage((AudioClip)sceses_PartyBuildingList[0]); break; case "PartyBuilding_gxscd"://过雪山草地 PlayLanguage((AudioClip)sceses_PartyBuildingList[1]); break; case "PartyBuilding_zxzd"://政协制度 PlayLanguage((AudioClip)sceses_PartyBuildingList[2]); break; case "PartyBuilding_ggkf"://改革开放 PlayLanguage((AudioClip)sceses_PartyBuildingList[3]); break; case "PartyBuilding_kgdd"://开国大典 PlayLanguage((AudioClip)sceses_PartyBuildingList[4]); break; } } /// /// 是否播放背景音乐 /// public bool IsPlayMusic { get { return !audioMusic.mute; } set { audioMusic.mute = !value; PlayerPrefs.SetInt("isMusicOpen", value ? 1 : 0); } } /// /// 是否播放音效 /// public bool IsPlaySound { get { return !audioSound.mute; } set { audioSound.mute = !value; PlayerPrefs.SetInt("isSoundOpen", value ? 1 : 0); } } /// /// 是否播放配音 /// public bool IsPlayLanguage { get { return !audioLanguage.mute; } set { audioLanguage.mute = !value; PlayerPrefs.SetInt("isLanguageOpen", value ? 1 : 0); } } /// /// 背景音乐大小,调节大小 /// public float MusicVolume { get { return audioMusic.volume; } set { audioMusic.volume = value; PlayerPrefs.SetFloat("MusicVolume", Mathf.Clamp(value, 0f, 1f)); } } /// /// 。调节大小 /// public float SoundVolume { get { return audioSound.volume; } set { audioSound.volume = value; PlayerPrefs.SetFloat("SoundVolume", Mathf.Clamp(value, 0f, 1f)); } } /// /// 音效大小 /// public float LanguageVolume { get { return audioLanguage.volume; } set { audioLanguage.volume = value; PlayerPrefs.SetFloat("LanguageVolume", Mathf.Clamp(value, 0f, 1f)); } } void Awake() { // DontDestroyOnLoad(this.gameObject); //Instance = this; //sceses_BJList = Resources.LoadAll("Sounds/BJSounds");//自动加载文件夹下的声音 audioSound = gameObject.AddComponent(); audioMusic = gameObject.AddComponent(); audioLanguage = gameObject.AddComponent(); audioMusic.loop = true; audioMusic.playOnAwake = false; MusicVolume = bjSoundVolume; this.IsPlayMusic = (PlayerPrefs.GetInt("isMusicOpen", 1) > 0); this.IsPlaySound = (PlayerPrefs.GetInt("isSoundOpen", 1) > 0); this.IsPlayLanguage = (PlayerPrefs.GetInt("isLanguageOpen", 1) > 0); this.MusicVolume = PlayerPrefs.GetFloat("MusicVolume", bjSoundVolume); this.SoundVolume = PlayerPrefs.GetFloat("SoundVolume", 1f); this.LanguageVolume = PlayerPrefs.GetFloat("LanguageVolume", 1f); } private void Start() { // StartCoroutine(MusicPlay()); } IEnumerator MusicPlay() { yield return new WaitForSeconds(0.1f); audioMusic.volume = 0.2f;//背景初始音效为0.2f } /// /// 播放背景音乐 /// public void PlayMusic(AudioClip clip, bool isLoop) { audioMusic.clip = null; audioLanguage.clip = null; if (clip == null) { return; } if (isLoop == false) { audioMusic.loop = false; audioMusic.clip = clip; audioMusic.Play(); } else { audioMusic.loop = true; audioMusic.clip = clip; audioMusic.Play(); } //if (audioMusic.isPlaying && clip.name == audioMusic.clip.name) //{ // return; //} } /// /// 停止播放音乐 /// public void StopMusic() { audioMusic.clip = null; audioMusic.Stop(); audioLanguage.clip = null; audioLanguage.Stop(); audioSound.clip = null; audioSound.Stop(); } /// /// 播放音效 /// public void PlaySound(AudioClip clip) { if (clip == null) { return; } audioSound.PlayOneShot(clip); } /// /// 播放配音 /// public void PlayLanguage(AudioClip clip) { audioLanguage.clip = null; if (clip == null) { return; } audioLanguage.clip = clip; audioLanguage.Play(); // audioLanguage.PlayOneShot(clip); } }