123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550 |
- using UnityEngine;
- using System.Collections;
- using UnityEngine.SceneManagement;
- using System.Runtime.CompilerServices;
- /// <summary>
- /// 音频管理类
- /// </summary>
- public class AudioManager : MonoSingletonOBE<AudioManager>
- {
- // 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<AudioManager>();
- // // }
- // // else
- // // {
- // // _instance = manager.GetComponent<AudioManager>();
- // // if (_instance == null)
- // // {
- // // _instance = manager.AddComponent<AudioManager>();
- // // }
- // // }
- // //}
- // 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;
- /// <summary>
- /// 背景音效
- /// </summary>
- /// <param name="str"></param>
- 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;
- }
- }
- /// <summary>
- /// 三国场景音效
- /// </summary>
- /// <param name="str"></param>
- 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;
- }
- }
- /// <summary>
- /// OBE
- /// </summary>
- /// <param name="str"></param>
- 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;
- }
- }
- /// <summary>
- /// 光刻机
- /// </summary>
- /// <param name="str"></param>
- 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;
- }
- }
- /// <summary>
- /// 地铁内视图
- /// </summary>
- /// <param name="str"></param>
- 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;
- }
- }
- /// <summary>
- ///医疗音效
- /// </summary>
- /// <param name="str"></param>
- public void AudioYiLiao_LanguageSounds(string str)
- {
- switch (str)
- {
- case "YL_Start":
- PlayLanguage(sceses_YiLiaoList[0]);
- break;
- }
- }
- /// <summary>
- ///未来城市
- /// </summary>
- /// <param name="str"></param>
- public void AudioWLCS_LanguageSounds(string str)
- {
- switch (str)
- {
- case "WLCS_Start":
- PlayLanguage(sceses_WeiLaiChengShiList[0]);
- break;
- }
- }
- /// <summary>
- /// 消防安全
- /// </summary>
- /// <param name="str"></param>
- public void AudioHZ_LanguageSounds(string str)
- {
- switch (str)
- {
- case "XFAQ_Start":
- PlayLanguage(sceses_XiaoFangAnQuanList[0]);
- break;
- }
- }
- /// <summary>
- /// 化学教育音效
- /// </summary>
- /// <param name="str"></param>
- 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;
- }
- }
- /// <summary>
- /// 博物馆音效
- /// </summary>
- /// <param name="str"></param>
- 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;
- }
- }
- /// <summary>
- /// 党建音乐
- /// </summary>
- /// <param name="str"></param>
- 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;
- }
- }
- /// <summary>
- /// 是否播放背景音乐
- /// </summary>
- public bool IsPlayMusic
- {
- get { return !audioMusic.mute; }
- set
- {
- audioMusic.mute = !value;
- PlayerPrefs.SetInt("isMusicOpen", value ? 1 : 0);
- }
- }
- /// <summary>
- /// 是否播放音效
- /// </summary>
- public bool IsPlaySound
- {
- get { return !audioSound.mute; }
- set
- {
- audioSound.mute = !value;
- PlayerPrefs.SetInt("isSoundOpen", value ? 1 : 0);
- }
- }
- /// <summary>
- /// 是否播放配音
- /// </summary>
- public bool IsPlayLanguage
- {
- get { return !audioLanguage.mute; }
- set
- {
- audioLanguage.mute = !value;
- PlayerPrefs.SetInt("isLanguageOpen", value ? 1 : 0);
- }
- }
- /// <summary>
- /// 背景音乐大小,调节大小
- /// </summary>
- public float MusicVolume
- {
- get { return audioMusic.volume; }
- set
- {
- audioMusic.volume = value;
- PlayerPrefs.SetFloat("MusicVolume", Mathf.Clamp(value, 0f, 1f));
- }
- }
- /// <summary>
- /// 。调节大小
- /// </summary>
- public float SoundVolume
- {
- get { return audioSound.volume; }
- set
- {
- audioSound.volume = value;
- PlayerPrefs.SetFloat("SoundVolume", Mathf.Clamp(value, 0f, 1f));
- }
- }
- /// <summary>
- /// 音效大小
- /// </summary>
- 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<AudioSource>();
- audioMusic = gameObject.AddComponent<AudioSource>();
- audioLanguage = gameObject.AddComponent<AudioSource>();
- 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
- }
- /// <summary>
- /// 播放背景音乐
- /// </summary>
- 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;
- //}
- }
- /// <summary>
- /// 停止播放音乐
- /// </summary>
- public void StopMusic()
- {
- audioMusic.clip = null;
- audioMusic.Stop();
- audioLanguage.clip = null;
- audioLanguage.Stop();
- audioSound.clip = null;
- audioSound.Stop();
- }
- /// <summary>
- /// 播放音效
- /// </summary>
- public void PlaySound(AudioClip clip)
- {
- if (clip == null)
- {
- return;
- }
- audioSound.PlayOneShot(clip);
- }
- /// <summary>
- /// 播放配音
- /// </summary>
- public void PlayLanguage(AudioClip clip)
- {
- audioLanguage.clip = null;
- if (clip == null)
- {
- return;
- }
- audioLanguage.clip = clip;
- audioLanguage.Play();
- // audioLanguage.PlayOneShot(clip);
- }
- }
|