From 947083cde2103756fecf0414642522009ac91399 Mon Sep 17 00:00:00 2001 From: ALEXTANG <574809918@qq.com> Date: Thu, 26 May 2022 00:44:25 +0800 Subject: [PATCH] Audio Agent Audio Agent --- Assets/TEngine/Runtime/Audio/AudioAgent.cs | 144 ++++++++++++++++++ .../TEngine/Runtime/Audio/AudioAgent.cs.meta | 11 ++ Assets/TEngine/Runtime/Audio/AudioManager.cs | 131 +++++++++++++++- Assets/TEngine/Runtime/Audio/TAudio.cs | 10 +- 4 files changed, 291 insertions(+), 5 deletions(-) create mode 100644 Assets/TEngine/Runtime/Audio/AudioAgent.cs create mode 100644 Assets/TEngine/Runtime/Audio/AudioAgent.cs.meta diff --git a/Assets/TEngine/Runtime/Audio/AudioAgent.cs b/Assets/TEngine/Runtime/Audio/AudioAgent.cs new file mode 100644 index 00000000..8b20116b --- /dev/null +++ b/Assets/TEngine/Runtime/Audio/AudioAgent.cs @@ -0,0 +1,144 @@ +using System.Collections.Generic; +using UnityEngine.Audio; + +namespace TEngine +{ + public class AudioAgent + { + public List _audioObjects; + AudioMixerGroup _audioMixerGroup; + int _maxChannel; + bool _bEnable = true; + + public bool Enable + { + get + { + return _bEnable; + } + set + { + if (_bEnable != value) + { + _bEnable = value; + if (!_bEnable) + { + for (int i = 0; i < _audioObjects.Count; ++i) + { + if (_audioObjects[i] != null) + { + _audioObjects[i].Stop(); + } + } + } + } + } + } + + + public AudioAgent(int maxChannel, AudioMixerGroup audioMixerGroup) + { + _maxChannel = maxChannel; + _audioObjects = new List(); + for (int i = 0; i < _maxChannel; i++) + { + TAudio tAudio = new TAudio(); + tAudio.Init(audioMixerGroup); + _audioObjects.Add(tAudio); + } + _audioMixerGroup = audioMixerGroup; + + } + + public void AddAudio(int Num) + { + _maxChannel += Num; + for (int i = 0; i < Num; i++) + { + _audioObjects.Add(null); + } + } + + + + public TAudio Play(string path, bool bAsync, bool bInPool = false) + { + if (!_bEnable) return null; + int freeChannel = -1; + float duration = -1; + int num = 0; + for (int i = 0; i < _audioObjects.Count; ++i) + { + if (_audioObjects[i] != null && _audioObjects[i]._assetData != null && _audioObjects[i].IsFinish == false) + { + if (path.Equals(_audioObjects[i]._assetData.Path)) + num++; + } + } + + for (int i = 0; i < _audioObjects.Count; i++) + { + if (AudioManager.Instance._soundConfigDic != null && AudioManager.Instance._soundConfigDic.ContainsKey(path) && AudioManager.Instance._soundConfigDic[path] == num) + { + if (_audioObjects[i] != null && _audioObjects[i]._assetData != null && path == _audioObjects[i]._assetData.Path) + { + if (_audioObjects[i].Duration > duration) + { + duration = _audioObjects[i].Duration; + freeChannel = i; + } + } + } + else + { + if (_audioObjects[i]._assetData == null || _audioObjects[i].IsFinish == true) + { + freeChannel = i; + break; + } + else if (_audioObjects[i].Duration > duration) + { + duration = _audioObjects[i].Duration; + freeChannel = i; + } + } + } + + if (freeChannel >= 0) + { + if (_audioObjects[freeChannel] == null) + _audioObjects[freeChannel] = TAudio.Create(path, bAsync, _audioMixerGroup, bInPool); + else + _audioObjects[freeChannel].Load(path, bAsync, bInPool); + return _audioObjects[freeChannel]; + } + else + { + TLogger.LogError($"Here is no channel to play audio {path}"); + return null; + } + } + + public void Stop(bool fadeout) + { + for (int i = 0; i < _audioObjects.Count; ++i) + { + if (_audioObjects[i] != null) + { + _audioObjects[i].Stop(fadeout); + } + } + } + + public void Update(float delta) + { + for (int i = 0; i < _audioObjects.Count; ++i) + { + if (_audioObjects[i] != null) + { + _audioObjects[i].Update(delta); + } + } + } + } +} diff --git a/Assets/TEngine/Runtime/Audio/AudioAgent.cs.meta b/Assets/TEngine/Runtime/Audio/AudioAgent.cs.meta new file mode 100644 index 00000000..90d1c77e --- /dev/null +++ b/Assets/TEngine/Runtime/Audio/AudioAgent.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e3c9ae2cf3ff0954f82e1103fb47c332 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/TEngine/Runtime/Audio/AudioManager.cs b/Assets/TEngine/Runtime/Audio/AudioManager.cs index 5df27545..0a8fc80c 100644 --- a/Assets/TEngine/Runtime/Audio/AudioManager.cs +++ b/Assets/TEngine/Runtime/Audio/AudioManager.cs @@ -1,4 +1,5 @@ -using System; +using Newtonsoft.Json; +using System; using System.Collections.Generic; using System.Linq; using System.Reflection; @@ -16,6 +17,8 @@ namespace TEngine public class AudioManager : UnitySingleton { + private AudioAgent[] _audioAgents = new AudioAgent[(int)AudioType.Max]; + public Dictionary _soundConfigDic = new Dictionary(); public Dictionary AudioClipPool = new Dictionary(); private float _volume = 1f; private bool _enable = true; @@ -74,6 +77,7 @@ namespace TEngine { return; } + //_soundConfigDic = JsonConvert.DeserializeObject>(str); } catch (Exception e) { @@ -81,5 +85,130 @@ namespace TEngine } } + #region 外部调用播放操作 + public TAudio Play(AudioType type, string path, bool bLoop = false, float volume = 1.0f, bool bAsync = false, bool bInPool = false) + { + if (_bUnityAudioDisabled) + { + return null; + } + TAudio audio = _audioAgents[(int)type].Play(path, bAsync, bInPool); + { + if (audio != null) + { + audio.IsLoop = bLoop; + audio.Volume = volume; + } + return audio; + } + } + + public void Stop(AudioType type, bool fadeout) + { + if (_bUnityAudioDisabled) + { + return; + } + _audioAgents[(int)type].Stop(fadeout); + } + + public void StopAll(bool fadeout) + { + if (_bUnityAudioDisabled) + { + return; + } + for (int i = 0; i < (int)AudioType.Max; ++i) + { + if (_audioAgents[i] != null) + { + _audioAgents[i].Stop(fadeout); + } + } + + } + + public void Restart() + { + if (_bUnityAudioDisabled) + { + return; + } + CleanSoundPool(); + for (int i = 0; i < (int)AudioType.Max; ++i) + { + if (_audioAgents[i] != null) + { + for (int j = 0; j < _audioAgents[i]._audioObjects.Count; ++j) + { + if (_audioAgents[i]._audioObjects[j] != null) + { + _audioAgents[i]._audioObjects[j].Destroy(); + _audioAgents[i]._audioObjects[j] = null; + } + } + } + _audioAgents[i] = null; + } + OnLoad(); + } + #endregion + + + #region Pool + public void PutInAudioPool(List list) + { + if (_bUnityAudioDisabled) + return; + foreach (string path in list) + { + if (!AudioClipPool.ContainsKey(path)) + { + AssetData assetData = ResMgr.Instance.GetAsset(path, false); + AudioClipPool?.Add(assetData.Path, assetData); + } + } + } + + public void RemoveClipFromPool(List list) + { + if (_bUnityAudioDisabled) + { + return; + } + foreach (string path in list) + { + if (AudioClipPool.ContainsKey(path)) + { + AudioClipPool[path].DecRef(); + AudioClipPool.Remove(path); + } + } + } + + public void CleanSoundPool() + { + if (_bUnityAudioDisabled) + { + return; + } + foreach (var dic in AudioClipPool) + { + dic.Value.DecRef(); + } + AudioClipPool.Clear(); + } + + private void Update() + { + for (int i = 0; i < _audioAgents.Length; ++i) + { + if (_audioAgents[i] != null) + { + _audioAgents[i].Update(Time.deltaTime); + } + } + } + #endregion } } diff --git a/Assets/TEngine/Runtime/Audio/TAudio.cs b/Assets/TEngine/Runtime/Audio/TAudio.cs index fc56142f..39e93288 100644 --- a/Assets/TEngine/Runtime/Audio/TAudio.cs +++ b/Assets/TEngine/Runtime/Audio/TAudio.cs @@ -140,7 +140,7 @@ namespace TEngine return _source; } - public static TAudio Create(string path, bool bAsync, AudioMixerGroup audioMixerGroup, bool bInPool = false) + public static TAudio Create(string path, bool bAsync, AudioMixerGroup audioMixerGroup = null, bool bInPool = false) { TAudio audio = new TAudio(); audio.Init(audioMixerGroup); @@ -149,7 +149,7 @@ namespace TEngine return audio; } - public void Init(AudioMixerGroup audioMixerGroup) + public void Init(AudioMixerGroup audioMixerGroup = null) { GameObject host = new GameObject("Audio"); host.transform.SetParent(AudioManager.Instance.transform); @@ -157,7 +157,10 @@ namespace TEngine _transform = host.transform; _source = host.AddComponent(); _source.playOnAwake = false; - _source.outputAudioMixerGroup = audioMixerGroup; + if (audioMixerGroup != null) + { + _source.outputAudioMixerGroup = audioMixerGroup; + } _id = _source.GetInstanceID(); } @@ -223,7 +226,6 @@ namespace TEngine assetData.AddRef(); AudioManager.Instance.AudioClipPool.Add(assetData.Path, assetData); } - }