mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
Audio Agent
Audio Agent
This commit is contained in:
144
Assets/TEngine/Runtime/Audio/AudioAgent.cs
Normal file
144
Assets/TEngine/Runtime/Audio/AudioAgent.cs
Normal file
@@ -0,0 +1,144 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine.Audio;
|
||||
|
||||
namespace TEngine
|
||||
{
|
||||
public class AudioAgent
|
||||
{
|
||||
public List<TAudio> _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<TAudio>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/TEngine/Runtime/Audio/AudioAgent.cs.meta
Normal file
11
Assets/TEngine/Runtime/Audio/AudioAgent.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e3c9ae2cf3ff0954f82e1103fb47c332
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -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<AudioManager>
|
||||
{
|
||||
private AudioAgent[] _audioAgents = new AudioAgent[(int)AudioType.Max];
|
||||
public Dictionary<string, int> _soundConfigDic = new Dictionary<string, int>();
|
||||
public Dictionary<string, AssetData> AudioClipPool = new Dictionary<string, AssetData>();
|
||||
private float _volume = 1f;
|
||||
private bool _enable = true;
|
||||
@@ -74,6 +77,7 @@ namespace TEngine
|
||||
{
|
||||
return;
|
||||
}
|
||||
//_soundConfigDic = JsonConvert.DeserializeObject<Dictionary<string, int>>(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<string> 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<string> 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
|
||||
}
|
||||
}
|
||||
|
@@ -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<AudioSource>();
|
||||
_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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user