| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- 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<AudioMgr>();
- DontDestroyOnLoad(audioMgr);
- }
- }
- void Awake() {
- this.audioSource = this.gameObject.AddComponent<AudioSource>();
- 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<AudioClip>(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<AudioSource>();
- if (audioSource == null) {
- audioSource = target.AddComponent<AudioSource>();
- }
- 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) {
- this.Play("Audios/Animal/" + name, audioSource);
- }
- /// <summary>
- /// 上弹夹
- /// </summary>
- public void PlayBeLoaded()
- {
- this.audioSource.volume = 1;
- this.Play("Audios/be_loaded", null);
- }
- /// <summary>
- /// 进入野兔
- /// </summary>
- public void PlayGoHareHunt()
- {
- this.audioSource.volume = 1;
- this.Play("Audios/Other/GoHareHunt", null);
- }
- /// <summary>
- /// 进入野鸡
- /// </summary>
- public void PlayGoPheasuntHunt()
- {
- if (!CommonConfig.StandaloneModeOrPlatformB) return;
- this.audioSource.volume = 1;
- this.Play("Audios/Other/GoPheasuntHunt", null);
- }
- /// <summary>
- /// 进入猎狼
- /// </summary>
- public void PlayGoWolfHunt()
- {
- if (!CommonConfig.StandaloneModeOrPlatformB) return;
- this.audioSource.volume = 1;
- this.Play("Audios/Other/GoWolfHunt", null);
- }
- /// <summary>
- /// 进入射箭场景
- /// </summary>
- public void PlayLetgo()
- {
- if (!CommonConfig.StandaloneModeOrPlatformB) return;
- this.audioSource.volume = 1;
- this.Play("Audios/Other/letgo", null);
- }
- /// <summary>
- /// 射中9-10 环欢呼声
- /// </summary>
- 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();
- }
- }
- }
|