AudioManager.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.SceneManagement;
  4. using System.Runtime.CompilerServices;
  5. /// <summary>
  6. /// 音频管理类
  7. /// </summary>
  8. public class AudioManager : MonoSingletonOBE<AudioManager>
  9. {
  10. // private static AudioManager _instance;
  11. //public static AudioManager Instance;
  12. //{
  13. // get
  14. // {
  15. // //if (_instance == null)
  16. // //{
  17. // // string name = "SoundManager";
  18. // // GameObject manager = GameObject.Find("SoundManager");
  19. // // if (manager == null)
  20. // // {
  21. // // manager = new GameObject(name);
  22. // // _instance = manager.AddComponent<AudioManager>();
  23. // // }
  24. // // else
  25. // // {
  26. // // _instance = manager.GetComponent<AudioManager>();
  27. // // if (_instance == null)
  28. // // {
  29. // // _instance = manager.AddComponent<AudioManager>();
  30. // // }
  31. // // }
  32. // //}
  33. // return _instance;
  34. // }
  35. //}
  36. private AudioSource audioSound; //音效播放器
  37. private AudioSource audioMusic; //背景音乐播放
  38. private AudioSource audioLanguage; //配音播放器
  39. public AudioClip[] sceses_BJList; //主背景音效
  40. public AudioClip[] sceses_SanGuoList;//三国场景音效
  41. public AudioClip[] scences_Aquarium;//海洋馆音效
  42. public AudioClip[] sceses_GongYeList_OBE;//工业obe场景音效
  43. public AudioClip[] sceses_GongYeList_GKJ;//工业光刻机场景音效
  44. public AudioClip[] sceses_GongYeList_DT;//工业地铁内抛图场景音效
  45. public AudioClip[] sceses_YiLiaoList;//医疗场景音效
  46. public AudioClip[] sceses_WeiLaiChengShiList;//未来城市
  47. public AudioClip[] sceses_XiaoFangAnQuanList;//消防安全音效
  48. public AudioClip[] sceses_JiaoYuList;//化学教育音效
  49. public AudioClip[] sceses_BoWuGunList;//博物馆音效
  50. public AudioClip[] sceses_PartyBuildingList;//党建音效
  51. public float bjSoundVolume;
  52. /// <summary>
  53. /// 背景音效
  54. /// </summary>
  55. /// <param name="str"></param>
  56. public void PlayBJSounds(string str)
  57. {
  58. Debug.Log(str);
  59. switch (str)
  60. {
  61. case "bjSound_SanGuo":
  62. PlayMusic((AudioClip)sceses_BJList[0], true);
  63. break;
  64. case "bjSound2_GongYe":
  65. PlayMusic((AudioClip)sceses_BJList[1], true);
  66. break;
  67. case "bjSound3_RenTi":
  68. PlayMusic((AudioClip)sceses_BJList[2], true);
  69. break;
  70. case "bjSound4_JiaoYu":
  71. PlayMusic((AudioClip)sceses_BJList[3], true);
  72. break;
  73. case "bjSound5_WeiLaiChengShi":
  74. PlayMusic((AudioClip)sceses_BJList[4], true);
  75. break;
  76. case "bjSound6_FireSafe":
  77. PlayMusic((AudioClip)sceses_BJList[5], true);
  78. break;
  79. case "bjSound5_BoWuGuan":
  80. PlayMusic((AudioClip)sceses_BJList[6], true);
  81. break;
  82. case "bjSound5_PartyBuilding":
  83. PlayMusic((AudioClip)sceses_BJList[7], true);
  84. break;
  85. case "None":
  86. PlayMusic((AudioClip)sceses_BJList[8], false);
  87. break;
  88. }
  89. }
  90. /// <summary>
  91. /// 三国场景音效
  92. /// </summary>
  93. /// <param name="str"></param>
  94. public void AudioSanGuo_LanguageSounds(string str)
  95. {
  96. switch (str)
  97. {
  98. case "SG_HLG"://虎牢关 三英战吕布
  99. PlayLanguage(sceses_SanGuoList[0]);
  100. break;
  101. case "SG_TYSJY"://桃园三结义
  102. PlayLanguage(sceses_SanGuoList[1]);
  103. break;
  104. case "SG_HSCB"://火烧赤壁
  105. PlayLanguage(sceses_SanGuoList[2]);
  106. break;
  107. case "SG_YMSJ"://辕门射戟
  108. PlayLanguage(sceses_SanGuoList[3]);
  109. break;
  110. }
  111. }
  112. /// <summary>
  113. /// OBE
  114. /// </summary>
  115. /// <param name="str"></param>
  116. public void AudioGongYe_OBE_LanguageSounds(string str)
  117. {
  118. switch (str)
  119. {
  120. case "GY_OBE_SoundStart":
  121. PlayLanguage(sceses_GongYeList_OBE[0]);
  122. break;
  123. case "GY_OBE_Sound1":
  124. PlayLanguage(sceses_GongYeList_OBE[1]);
  125. break;
  126. case "GY_OBE_Sound2":
  127. PlayLanguage(sceses_GongYeList_OBE[2]);
  128. break;
  129. case "GY_OBE_Sound3":
  130. PlayLanguage(sceses_GongYeList_OBE[3]);
  131. break;
  132. case "GY_OBE_Sound4":
  133. PlayLanguage(sceses_GongYeList_OBE[4]);
  134. break;
  135. case "GY_OBE_Sound5":
  136. PlayLanguage(sceses_GongYeList_OBE[5]);
  137. break;
  138. case "GY_OBE_SoundEnd":
  139. PlayLanguage(sceses_GongYeList_OBE[6]);
  140. break;
  141. }
  142. }
  143. /// <summary>
  144. /// 光刻机
  145. /// </summary>
  146. /// <param name="str"></param>
  147. public void AudioGongYe_GKJ_LanguageSounds(string str)
  148. {
  149. switch (str)
  150. {
  151. case "GY_GKJ1":
  152. PlayLanguage(sceses_GongYeList_GKJ[0]);
  153. break;
  154. case "GY_GKJ2":
  155. PlayLanguage(sceses_GongYeList_GKJ[1]);
  156. break;
  157. case "GY_GKJ3":
  158. PlayLanguage(sceses_GongYeList_GKJ[2]);
  159. break;
  160. case "GY_GKJ4":
  161. PlayLanguage(sceses_GongYeList_GKJ[3]);
  162. break;
  163. case "GY_GKJ5":
  164. PlayLanguage(sceses_GongYeList_GKJ[4]);
  165. break;
  166. case "GY_GKJ6":
  167. PlayLanguage(sceses_GongYeList_GKJ[5]);
  168. break;
  169. case "GY_GKJ_Start":
  170. PlayLanguage(sceses_GongYeList_GKJ[6]);
  171. break;
  172. }
  173. }
  174. /// <summary>
  175. /// 地铁内视图
  176. /// </summary>
  177. /// <param name="str"></param>
  178. public void AudioGongYe_DT_LanguageSounds(string str)
  179. {
  180. Debug.Log("str");
  181. switch (str)
  182. {
  183. case "GY_dianliqianyin"://电力牵引机
  184. PlayLanguage(sceses_GongYeList_DT[0]);
  185. break;
  186. case "GY_guagqian"://光纤交换机
  187. PlayLanguage(sceses_GongYeList_DT[1]);
  188. break;
  189. case "GY_luyouqi"://路由器
  190. PlayLanguage(sceses_GongYeList_DT[2]);
  191. break;
  192. case "GY_yitaiwang"://以太网交换机
  193. PlayLanguage(sceses_GongYeList_DT[3]);
  194. break;
  195. case "GY_jiankong"://.监控系统
  196. PlayLanguage(sceses_GongYeList_DT[4]);
  197. break;
  198. case "GY_zhuanhuanqi"://转换器
  199. PlayLanguage(sceses_GongYeList_DT[5]);
  200. break;
  201. case "GY_dianji"://电机
  202. PlayLanguage(sceses_GongYeList_DT[6]);
  203. break;
  204. case "GY_kongtiao"://空调通风系统
  205. PlayLanguage(sceses_GongYeList_DT[7]);
  206. break;
  207. case "GY_DT"://空调通风系统
  208. PlayLanguage(sceses_GongYeList_DT[8]);
  209. break;
  210. }
  211. }
  212. /// <summary>
  213. ///医疗音效
  214. /// </summary>
  215. /// <param name="str"></param>
  216. public void AudioYiLiao_LanguageSounds(string str)
  217. {
  218. switch (str)
  219. {
  220. case "YL_Start":
  221. PlayLanguage(sceses_YiLiaoList[0]);
  222. break;
  223. }
  224. }
  225. /// <summary>
  226. ///未来城市
  227. /// </summary>
  228. /// <param name="str"></param>
  229. public void AudioWLCS_LanguageSounds(string str)
  230. {
  231. switch (str)
  232. {
  233. case "WLCS_Start":
  234. PlayLanguage(sceses_WeiLaiChengShiList[0]);
  235. break;
  236. }
  237. }
  238. /// <summary>
  239. /// 消防安全
  240. /// </summary>
  241. /// <param name="str"></param>
  242. public void AudioHZ_LanguageSounds(string str)
  243. {
  244. switch (str)
  245. {
  246. case "XFAQ_Start":
  247. PlayLanguage(sceses_XiaoFangAnQuanList[0]);
  248. break;
  249. }
  250. }
  251. /// <summary>
  252. /// 化学教育音效
  253. /// </summary>
  254. /// <param name="str"></param>
  255. public void AudioJiaoYu_LanguageSounds(string str)
  256. {
  257. switch (str)
  258. {
  259. case "HX_SoundStart":
  260. PlayLanguage(sceses_JiaoYuList[0]);
  261. break;
  262. case "HX_1":
  263. PlayLanguage(sceses_JiaoYuList[1]);
  264. break;
  265. case "HX_2":
  266. PlayLanguage(sceses_JiaoYuList[2]);
  267. break;
  268. case "HX_3":
  269. PlayLanguage(sceses_JiaoYuList[3]);
  270. break;
  271. case "HX_4":
  272. PlayLanguage(sceses_JiaoYuList[4]);
  273. break;
  274. case "HX_5":
  275. PlayLanguage(sceses_JiaoYuList[5]);
  276. break;
  277. case "HX_6":
  278. PlayLanguage(sceses_JiaoYuList[6]);
  279. break;
  280. }
  281. }
  282. /// <summary>
  283. /// 博物馆音效
  284. /// </summary>
  285. /// <param name="str"></param>
  286. public void AudioBoWuGun_LanguageSounds(string str)
  287. {
  288. Debug.Log(str);
  289. switch (str)
  290. {
  291. case "matafeiyan":
  292. PlayLanguage(sceses_BoWuGunList[0]);
  293. break;
  294. case "yuwanggoujianjian":
  295. PlayLanguage(sceses_BoWuGunList[1]);
  296. break;
  297. case "tangsancai":
  298. PlayLanguage(sceses_BoWuGunList[2]);
  299. break;
  300. case "qinghuaci":
  301. PlayLanguage(sceses_BoWuGunList[3]);
  302. break;
  303. case "BWG_end":
  304. PlayLanguage(sceses_BoWuGunList[4]);
  305. break;
  306. }
  307. }
  308. /// <summary>
  309. /// 党建音乐
  310. /// </summary>
  311. /// <param name="str"></param>
  312. public void AudioPartyBuilding_LanguageSounds(string str)
  313. {
  314. switch (str)
  315. {
  316. case "PartyBuilding_zgcl"://中共成立
  317. PlayLanguage((AudioClip)sceses_PartyBuildingList[0]);
  318. break;
  319. case "PartyBuilding_gxscd"://过雪山草地
  320. PlayLanguage((AudioClip)sceses_PartyBuildingList[1]);
  321. break;
  322. case "PartyBuilding_zxzd"://政协制度
  323. PlayLanguage((AudioClip)sceses_PartyBuildingList[2]);
  324. break;
  325. case "PartyBuilding_ggkf"://改革开放
  326. PlayLanguage((AudioClip)sceses_PartyBuildingList[3]);
  327. break;
  328. case "PartyBuilding_kgdd"://开国大典
  329. PlayLanguage((AudioClip)sceses_PartyBuildingList[4]);
  330. break;
  331. }
  332. }
  333. /// <summary>
  334. /// 是否播放背景音乐
  335. /// </summary>
  336. public bool IsPlayMusic
  337. {
  338. get { return !audioMusic.mute; }
  339. set
  340. {
  341. audioMusic.mute = !value;
  342. PlayerPrefs.SetInt("isMusicOpen", value ? 1 : 0);
  343. }
  344. }
  345. /// <summary>
  346. /// 是否播放音效
  347. /// </summary>
  348. public bool IsPlaySound
  349. {
  350. get { return !audioSound.mute; }
  351. set
  352. {
  353. audioSound.mute = !value;
  354. PlayerPrefs.SetInt("isSoundOpen", value ? 1 : 0);
  355. }
  356. }
  357. /// <summary>
  358. /// 是否播放配音
  359. /// </summary>
  360. public bool IsPlayLanguage
  361. {
  362. get { return !audioLanguage.mute; }
  363. set
  364. {
  365. audioLanguage.mute = !value;
  366. PlayerPrefs.SetInt("isLanguageOpen", value ? 1 : 0);
  367. }
  368. }
  369. /// <summary>
  370. /// 背景音乐大小,调节大小
  371. /// </summary>
  372. public float MusicVolume
  373. {
  374. get { return audioMusic.volume; }
  375. set
  376. {
  377. audioMusic.volume = value;
  378. PlayerPrefs.SetFloat("MusicVolume", Mathf.Clamp(value, 0f, 1f));
  379. }
  380. }
  381. /// <summary>
  382. /// 。调节大小
  383. /// </summary>
  384. public float SoundVolume
  385. {
  386. get { return audioSound.volume; }
  387. set
  388. {
  389. audioSound.volume = value;
  390. PlayerPrefs.SetFloat("SoundVolume", Mathf.Clamp(value, 0f, 1f));
  391. }
  392. }
  393. /// <summary>
  394. /// 音效大小
  395. /// </summary>
  396. public float LanguageVolume
  397. {
  398. get { return audioLanguage.volume; }
  399. set
  400. {
  401. audioLanguage.volume = value;
  402. PlayerPrefs.SetFloat("LanguageVolume", Mathf.Clamp(value, 0f, 1f));
  403. }
  404. }
  405. void Awake()
  406. {
  407. // DontDestroyOnLoad(this.gameObject);
  408. //Instance = this;
  409. //sceses_BJList = Resources.LoadAll("Sounds/BJSounds");//自动加载文件夹下的声音
  410. audioSound = gameObject.AddComponent<AudioSource>();
  411. audioMusic = gameObject.AddComponent<AudioSource>();
  412. audioLanguage = gameObject.AddComponent<AudioSource>();
  413. audioMusic.loop = true;
  414. audioMusic.playOnAwake = false;
  415. MusicVolume = bjSoundVolume;
  416. this.IsPlayMusic = (PlayerPrefs.GetInt("isMusicOpen", 1) > 0);
  417. this.IsPlaySound = (PlayerPrefs.GetInt("isSoundOpen", 1) > 0);
  418. this.IsPlayLanguage = (PlayerPrefs.GetInt("isLanguageOpen", 1) > 0);
  419. this.MusicVolume = PlayerPrefs.GetFloat("MusicVolume", bjSoundVolume);
  420. this.SoundVolume = PlayerPrefs.GetFloat("SoundVolume", 1f);
  421. this.LanguageVolume = PlayerPrefs.GetFloat("LanguageVolume", 1f);
  422. }
  423. private void Start()
  424. {
  425. // StartCoroutine(MusicPlay());
  426. }
  427. IEnumerator MusicPlay()
  428. {
  429. yield return new WaitForSeconds(0.1f);
  430. audioMusic.volume = 0.2f;//背景初始音效为0.2f
  431. }
  432. /// <summary>
  433. /// 播放背景音乐
  434. /// </summary>
  435. public void PlayMusic(AudioClip clip, bool isLoop)
  436. {
  437. audioMusic.clip = null;
  438. audioLanguage.clip = null;
  439. if (clip == null)
  440. {
  441. return;
  442. }
  443. if (isLoop == false)
  444. {
  445. audioMusic.loop = false;
  446. audioMusic.clip = clip;
  447. audioMusic.Play();
  448. }
  449. else
  450. {
  451. audioMusic.loop = true;
  452. audioMusic.clip = clip;
  453. audioMusic.Play();
  454. }
  455. //if (audioMusic.isPlaying && clip.name == audioMusic.clip.name)
  456. //{
  457. // return;
  458. //}
  459. }
  460. /// <summary>
  461. /// 停止播放音乐
  462. /// </summary>
  463. public void StopMusic()
  464. {
  465. audioMusic.clip = null;
  466. audioMusic.Stop();
  467. audioLanguage.clip = null;
  468. audioLanguage.Stop();
  469. audioSound.clip = null;
  470. audioSound.Stop();
  471. }
  472. /// <summary>
  473. /// 播放音效
  474. /// </summary>
  475. public void PlaySound(AudioClip clip)
  476. {
  477. if (clip == null)
  478. {
  479. return;
  480. }
  481. audioSound.PlayOneShot(clip);
  482. }
  483. /// <summary>
  484. /// 播放配音
  485. /// </summary>
  486. public void PlayLanguage(AudioClip clip)
  487. {
  488. audioLanguage.clip = null;
  489. if (clip == null)
  490. {
  491. return;
  492. }
  493. audioLanguage.clip = clip;
  494. audioLanguage.Play();
  495. // audioLanguage.PlayOneShot(clip);
  496. }
  497. }