AudioMgr.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. /* 音频管理者 */
  6. public class AudioMgr : MonoBehaviour
  7. {
  8. private AudioSource audioSource;
  9. public static AudioMgr ins;
  10. //是否需要开始播放音效
  11. static bool needPlayStartAudio = false;
  12. public static bool bNeedPlayStartAudio
  13. {
  14. get => needPlayStartAudio;
  15. set
  16. {
  17. if (CommonConfig.StandaloneModeOrPlatformB)
  18. {
  19. // 满足条件时才允许设置为 true
  20. needPlayStartAudio = value;
  21. }
  22. else
  23. {
  24. // 不满足条件时,保持为 false
  25. needPlayStartAudio = false;
  26. Debug.LogWarning("B端使用此bNeedPlayStartAudio属性");
  27. }
  28. }
  29. }
  30. public static void Init()
  31. {
  32. if (!ins) {
  33. GameObject audioMgr = new GameObject("AudioMgr");
  34. ins = audioMgr.AddComponent<AudioMgr>();
  35. DontDestroyOnLoad(audioMgr);
  36. }
  37. }
  38. void Awake() {
  39. this.audioSource = this.gameObject.AddComponent<AudioSource>();
  40. if (CommonConfig.StandaloneModeOrPlatformB)
  41. {
  42. SceneManager.sceneLoaded += OnSceneLoaded;
  43. }
  44. }
  45. void OnDestroy()
  46. {
  47. if (CommonConfig.StandaloneModeOrPlatformB)
  48. {
  49. // 确保在销毁时移除事件监听
  50. SceneManager.sceneLoaded -= OnSceneLoaded;
  51. }
  52. }
  53. public void Play(string path, AudioSource audioSource) {
  54. AudioClip audioClip = Resources.Load<AudioClip>(path);
  55. if (audioSource == null) {
  56. audioSource = this.audioSource;
  57. }
  58. audioSource.clip = audioClip;
  59. if (UserSettings.ins.openEffect) {
  60. audioSource.Play();
  61. }
  62. }
  63. public static AudioSource GetAudioSource(GameObject target) {
  64. AudioSource audioSource = target.GetComponent<AudioSource>();
  65. if (audioSource == null) {
  66. audioSource = target.AddComponent<AudioSource>();
  67. }
  68. return audioSource;
  69. }
  70. public void PlayGunShoot(AudioSource audioSource)
  71. {
  72. this.Play("Audios/gun_shoot", audioSource);
  73. }
  74. public void PlayShoot(AudioSource audioSource) {
  75. this.Play("Audios/shoot", audioSource);
  76. }
  77. public void PlayHit(AudioSource audioSource) {
  78. this.Play("Audios/hit", audioSource);
  79. }
  80. public void PlayCheer(bool cheer) {
  81. this.audioSource.volume = 1;
  82. this.Play("Audios/" + (cheer ? "喝彩" : "喝倒彩"), null);
  83. }
  84. //按钮播放音效
  85. public void PlayBtn() {
  86. //连接了枪情况下播放枪声音
  87. //if (BluetoothAim.ins && BluetoothAim.ins.isMainConnectToGun() && GlobalData.MyDeviceMode == DeviceMode.Gun)
  88. //{
  89. // this.audioSource.volume = 1;// 0.15f;
  90. // this.Play("Audios/gun_shoot", null);
  91. //}
  92. //else {
  93. this.audioSource.volume = 1;
  94. this.Play("Audios/btn", null);
  95. // }
  96. }
  97. public void PlayWin() {
  98. this.audioSource.volume = 1;
  99. this.Play("Audios/win", null);
  100. }
  101. public void PlayArrowEnter() {
  102. this.audioSource.volume = 1;
  103. this.Play("Audios/Animal/arrow_enter", null);
  104. }
  105. public void PlayAnimalEffect(string name, AudioSource audioSource) {
  106. this.Play("Audios/Animal/" + name, audioSource);
  107. }
  108. /// <summary>
  109. /// 上弹夹
  110. /// </summary>
  111. public void PlayBeLoaded()
  112. {
  113. this.audioSource.volume = 1;
  114. this.Play("Audios/be_loaded", null);
  115. }
  116. /// <summary>
  117. /// 进入野兔
  118. /// </summary>
  119. public void PlayGoHareHunt()
  120. {
  121. this.audioSource.volume = 1;
  122. this.Play("Audios/Other/GoHareHunt", null);
  123. }
  124. /// <summary>
  125. /// 进入野鸡
  126. /// </summary>
  127. public void PlayGoPheasuntHunt()
  128. {
  129. if (!CommonConfig.StandaloneModeOrPlatformB) return;
  130. this.audioSource.volume = 1;
  131. this.Play("Audios/Other/GoPheasuntHunt", null);
  132. }
  133. /// <summary>
  134. /// 进入猎狼
  135. /// </summary>
  136. public void PlayGoWolfHunt()
  137. {
  138. if (!CommonConfig.StandaloneModeOrPlatformB) return;
  139. this.audioSource.volume = 1;
  140. this.Play("Audios/Other/GoWolfHunt", null);
  141. }
  142. /// <summary>
  143. /// 进入射箭场景
  144. /// </summary>
  145. public void PlayLetgo()
  146. {
  147. if (!CommonConfig.StandaloneModeOrPlatformB) return;
  148. this.audioSource.volume = 1;
  149. this.Play("Audios/Other/letgo", null);
  150. }
  151. /// <summary>
  152. /// 射中9-10 环欢呼声
  153. /// </summary>
  154. public void PlayRings()
  155. {
  156. if (!CommonConfig.StandaloneModeOrPlatformB) return;
  157. this.audioSource.volume = 1;
  158. this.Play("Audios/Other/Rings", null);
  159. }
  160. // 回调函数,当场景加载完成时调用
  161. // 一般情况下重新挑战开始时候调用
  162. private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
  163. {
  164. if (!needPlayStartAudio) return;
  165. needPlayStartAudio = false;
  166. switch (scene.name)
  167. {
  168. case "Game":
  169. PlayLetgo();
  170. break;
  171. case "GameChallenge":
  172. PlayStartAudioByGameType();
  173. break;
  174. }
  175. }
  176. public void PlayStartAudioByGameType() {
  177. if (!CommonConfig.StandaloneModeOrPlatformB) return;
  178. if (GameMgr.gameType == 3)
  179. {
  180. //兔子
  181. PlayGoHareHunt();
  182. }
  183. else if (GameMgr.gameType == 4)
  184. {
  185. //野鸡
  186. PlayGoPheasuntHunt();
  187. }
  188. else if (GameMgr.gameType == 5)
  189. {
  190. //狼
  191. PlayGoWolfHunt();
  192. }
  193. }
  194. }