using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; /* 音频管理者 */ public class AudioMgr : MonoBehaviour { private AudioSource audioSource; public static AudioMgr ins; //是否需要开始播放音效 static bool needPlayStartAudio = false; public static bool bNeedPlayStartAudio { get => needPlayStartAudio; set { if (CommonConfig.StandaloneModeOrPlatformB) { // 满足条件时才允许设置为 true needPlayStartAudio = value; } else { // 不满足条件时,保持为 false needPlayStartAudio = false; Debug.LogWarning("B端使用此bNeedPlayStartAudio属性"); } } } public static void Init() { if (!ins) { GameObject audioMgr = new GameObject("AudioMgr"); ins = audioMgr.AddComponent(); DontDestroyOnLoad(audioMgr); } } void Awake() { this.audioSource = this.gameObject.AddComponent(); if (CommonConfig.StandaloneModeOrPlatformB) { SceneManager.sceneLoaded += OnSceneLoaded; } } void OnDestroy() { if (CommonConfig.StandaloneModeOrPlatformB) { // 确保在销毁时移除事件监听 SceneManager.sceneLoaded -= OnSceneLoaded; } } public void Play(string path, AudioSource audioSource) { AudioClip audioClip = Resources.Load(path); if (audioSource == null) { audioSource = this.audioSource; } audioSource.clip = audioClip; if (UserSettings.ins.openEffect) { audioSource.Play(); } } public static AudioSource GetAudioSource(GameObject target) { AudioSource audioSource = target.GetComponent(); if (audioSource == null) { audioSource = target.AddComponent(); } return audioSource; } public void PlayGunShoot(AudioSource audioSource) { this.Play("Audios/gun_shoot", audioSource); } public void PlayShoot(AudioSource audioSource) { this.Play("Audios/shoot", audioSource); } public void PlayHit(AudioSource audioSource) { this.Play("Audios/hit", audioSource); } public void PlayCheer(bool cheer) { this.audioSource.volume = 1; this.Play("Audios/" + (cheer ? "喝彩" : "喝倒彩"), null); } //按钮播放音效 public void PlayBtn() { //连接了枪情况下播放枪声音 //if (BluetoothAim.ins && BluetoothAim.ins.isMainConnectToGun() && GlobalData.MyDeviceMode == DeviceMode.Gun) //{ // this.audioSource.volume = 1;// 0.15f; // this.Play("Audios/gun_shoot", null); //} //else { this.audioSource.volume = 1; this.Play("Audios/btn", null); // } } public void PlayWin() { this.audioSource.volume = 1; this.Play("Audios/win", null); } public void PlayArrowEnter() { this.audioSource.volume = 1; this.Play("Audios/Animal/arrow_enter", null); } public void PlayAnimalEffect(string name, AudioSource audioSource) { //兔子暂时替换一个音效 if (name == "rabbit_injured") { name = "rabbit_injured2"; audioSource.volume = 1; //Debug.Log("兔子音效 Volume =" + audioSource.volume); } this.Play("Audios/Animal/" + name, audioSource); } /// /// 上弹夹 /// public void PlayBeLoaded() { this.audioSource.volume = 1; this.Play("Audios/be_loaded", null); } /// /// 进入野兔 /// public void PlayGoHareHunt() { this.audioSource.volume = 1; this.Play("Audios/Other/GoHareHunt", null); } /// /// 进入野鸡 /// public void PlayGoPheasuntHunt() { if (!CommonConfig.StandaloneModeOrPlatformB) return; this.audioSource.volume = 1; this.Play("Audios/Other/GoPheasuntHunt", null); } /// /// 进入猎狼 /// public void PlayGoWolfHunt() { if (!CommonConfig.StandaloneModeOrPlatformB) return; this.audioSource.volume = 1; this.Play("Audios/Other/GoWolfHunt", null); } /// /// 进入射箭场景 /// public void PlayLetgo() { if (!CommonConfig.StandaloneModeOrPlatformB) return; this.audioSource.volume = 1; this.Play("Audios/Other/letgo", null); } /// /// 射中9-10 环欢呼声 /// public void PlayRings() { if (!CommonConfig.StandaloneModeOrPlatformB) return; this.audioSource.volume = 1; this.Play("Audios/Other/Rings", null); } // 回调函数,当场景加载完成时调用 // 一般情况下重新挑战开始时候调用 private void OnSceneLoaded(Scene scene, LoadSceneMode mode) { if (!needPlayStartAudio) return; needPlayStartAudio = false; switch (scene.name) { case "Game": PlayLetgo(); break; case "GameChallenge": PlayStartAudioByGameType(); break; } } public void PlayStartAudioByGameType() { if (!CommonConfig.StandaloneModeOrPlatformB) return; if (GameMgr.gameType == 3) { //兔子 PlayGoHareHunt(); } else if (GameMgr.gameType == 4) { //野鸡 PlayGoPheasuntHunt(); } else if (GameMgr.gameType == 5) { //狼 PlayGoWolfHunt(); } } }