/**************************************************************************** * Copyright (c) 2017 snowcold * Copyright (c) 2017 ~ 2024 liangxiegame UNDER MIT LICENSE * * https://qframework.cn * https://github.com/liangxiegame/QFramework * https://gitee.com/liangxiegame/QFramework ****************************************************************************/ using System; using System.Linq; namespace QFramework { using System.Collections.Generic; using UnityEngine; [MonoSingletonPath("QFramework/AudioKit/AudioManager")] public class AudioManager : MonoBehaviour, ISingleton { public AudioPlayer MusicPlayer { get; private set; } public AudioPlayer VoicePlayer { get; private set; } public void OnSingletonInit() { SafeObjectPool.Instance.Init(10, 1); MusicPlayer = AudioPlayer.Allocate(AudioKit.Settings.MusicVolume); MusicPlayer.UsedCache = false; MusicPlayer.IsLoop = true; VoicePlayer = AudioPlayer.Allocate(AudioKit.Settings.VoiceVolume); VoicePlayer.UsedCache = false; CheckAudioListener(); gameObject.transform.position = Vector3.zero; AudioKit.Settings.IsMusicOn.Register(musicOn => { if (musicOn) { if (!string.IsNullOrEmpty(CurrentMusicName)) { AudioKit.PlayMusic(CurrentMusicName); } } else { MusicPlayer.Stop(); } }).UnRegisterWhenGameObjectDestroyed(gameObject); AudioKit.Settings.IsVoiceOn.Register(voiceOn => { if (voiceOn) { if (!string.IsNullOrEmpty(CurrentVoiceName)) { AudioKit.PlayVoice(CurrentVoiceName); } } else { VoicePlayer.Stop(); } }).UnRegisterWhenGameObjectDestroyed(gameObject); AudioKit.Settings.IsSoundOn.Register(soundOn => { if (soundOn) { } else { ForEachAllSound(player => player.Stop()); } }).UnRegisterWhenGameObjectDestroyed(gameObject); } private static Dictionary> mSoundPlayerInPlaying = new Dictionary>(30); public void ForEachAllSound(Action operation) { foreach (var audioPlayer in mSoundPlayerInPlaying.SelectMany(keyValuePair => keyValuePair.Value)) { operation(audioPlayer); } } public void AddSoundPlayer2Pool(AudioPlayer audioPlayer) { if (mSoundPlayerInPlaying.ContainsKey(audioPlayer.GetName)) { mSoundPlayerInPlaying[audioPlayer.GetName].Add(audioPlayer); } else { mSoundPlayerInPlaying.Add(audioPlayer.GetName, new List { audioPlayer }); } } public void RemoveSoundPlayerFromPool(AudioPlayer audioPlayer) { mSoundPlayerInPlaying[audioPlayer.GetName].Remove(audioPlayer); } #region 对外接口 public void Init() { Debug.Log("AudioManager.Init"); } private AudioListener mAudioListener; public void CheckAudioListener() { // 确保有一个AudioListener if (!mAudioListener) { mAudioListener = FindObjectOfType(); } if (!mAudioListener) { mAudioListener = gameObject.AddComponent(); } } public string CurrentMusicName { get; set; } public string CurrentVoiceName { get; set; } #endregion public static void PlayVoiceOnce(string voiceName) { if (string.IsNullOrEmpty(voiceName)) { return; } var unit = SafeObjectPool.Instance.Allocate(); unit.SetAudio(Instance.gameObject, voiceName, false); } #region 单例实现 public static AudioManager Instance => MonoSingletonProperty.Instance; #endregion public void ClearAllPlayingSound() { mSoundPlayerInPlaying.Clear(); } } }