AudioMgr.cs 5.8 KB

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