AudioMgr.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. //兔子暂时替换一个音效
  107. if (name == "rabbit_injured") {
  108. name = "rabbit_injured2";
  109. audioSource.volume = 1;
  110. //Debug.Log("兔子音效 Volume =" + audioSource.volume);
  111. }
  112. this.Play("Audios/Animal/" + name, audioSource);
  113. }
  114. /// <summary>
  115. /// 上弹夹
  116. /// </summary>
  117. public void PlayBeLoaded()
  118. {
  119. this.audioSource.volume = 1;
  120. this.Play("Audios/be_loaded", null);
  121. }
  122. /// <summary>
  123. /// 进入野兔
  124. /// </summary>
  125. public void PlayGoHareHunt()
  126. {
  127. this.audioSource.volume = 1;
  128. this.Play("Audios/Other/GoHareHunt", null);
  129. }
  130. /// <summary>
  131. /// 进入野鸡
  132. /// </summary>
  133. public void PlayGoPheasuntHunt()
  134. {
  135. if (!CommonConfig.StandaloneModeOrPlatformB) return;
  136. this.audioSource.volume = 1;
  137. this.Play("Audios/Other/GoPheasuntHunt", null);
  138. }
  139. /// <summary>
  140. /// 进入猎狼
  141. /// </summary>
  142. public void PlayGoWolfHunt()
  143. {
  144. if (!CommonConfig.StandaloneModeOrPlatformB) return;
  145. this.audioSource.volume = 1;
  146. this.Play("Audios/Other/GoWolfHunt", null);
  147. }
  148. /// <summary>
  149. /// 进入射箭场景
  150. /// </summary>
  151. public void PlayLetgo()
  152. {
  153. if (!CommonConfig.StandaloneModeOrPlatformB) return;
  154. this.audioSource.volume = 1;
  155. this.Play("Audios/Other/letgo", null);
  156. }
  157. /// <summary>
  158. /// 射中9-10 环欢呼声
  159. /// </summary>
  160. public void PlayRings()
  161. {
  162. if (!CommonConfig.StandaloneModeOrPlatformB) return;
  163. this.audioSource.volume = 1;
  164. this.Play("Audios/Other/Rings", null);
  165. }
  166. // 回调函数,当场景加载完成时调用
  167. // 一般情况下重新挑战开始时候调用
  168. private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
  169. {
  170. if (!needPlayStartAudio) return;
  171. needPlayStartAudio = false;
  172. switch (scene.name)
  173. {
  174. case "Game":
  175. PlayLetgo();
  176. break;
  177. case "GameChallenge":
  178. PlayStartAudioByGameType();
  179. break;
  180. }
  181. }
  182. public void PlayStartAudioByGameType() {
  183. if (!CommonConfig.StandaloneModeOrPlatformB) return;
  184. if (GameMgr.gameType == 3)
  185. {
  186. //兔子
  187. PlayGoHareHunt();
  188. }
  189. else if (GameMgr.gameType == 4)
  190. {
  191. //野鸡
  192. PlayGoPheasuntHunt();
  193. }
  194. else if (GameMgr.gameType == 5)
  195. {
  196. //狼
  197. PlayGoWolfHunt();
  198. }
  199. }
  200. }