Modules
This commit is contained in:
ALEXTANG
2022-05-27 12:37:49 +08:00
parent 494b7beef3
commit 6bd585d64e
7 changed files with 0 additions and 887 deletions

Binary file not shown.

View File

@@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: c9052261b332b294f97edcc2812f4eea
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,531 +0,0 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;
using UnityEngine.Audio;
namespace TEngine
{
public enum AudioType
{
Sound,
Music,
Voice,
Max
}
public class AudioMgr : UnitySingleton<AudioMgr>
{
#region Propreties
private float _volume = 1f;
private bool _enable = true;
private bool _disabled = false;
public AudioMixer audioMixer { get; set; }
float[] _agentVolume = new float[(int)AudioType.Max];
private AudioAgent[] _audioAgents = new AudioAgent[(int)AudioType.Max];
public Dictionary<string, AssetData> AudioClipPool = new Dictionary<string, AssetData>();
#endregion
#region
/// <summary>
/// 总音量
/// </summary>
public float Volume
{
get
{
if (_disabled)
{
return 0.0f;
}
return _volume;
}
set
{
if (_disabled)
{
return;
}
_volume = value;
AudioListener.volume = _volume;
}
}
/// <summary>
/// 总开关
/// </summary>
public bool Enable
{
get
{
if (_disabled)
{
return false;
}
return _enable;
}
set
{
if (_disabled)
{
return;
}
_enable = value;
AudioListener.volume = _enable ? _volume : 0f;
}
}
/// <summary>
/// 背景音量
/// </summary>
public float MusicVolume
{
get
{
if (_disabled)
{
return 0.0f;
}
return _agentVolume[(int)AudioType.Music];
}
set
{
if (_disabled)
{
return;
}
float volume = Mathf.Clamp(value, 0.0001f, 1.0f);
_agentVolume[(int)AudioType.Music] = volume;
audioMixer.SetFloat("MusicVolume", Mathf.Log10(volume) * 20f);
}
}
/// <summary>
/// 音效音量
/// </summary>
public float SoundVolume
{
get
{
if (_disabled)
{
return 0.0f;
}
return _agentVolume[(int)AudioType.Sound];
}
set
{
if (_disabled)
{
return;
}
float volume = Mathf.Clamp(value, 0.0001f, 1.0f);
_agentVolume[(int)AudioType.Sound] = volume;
audioMixer.SetFloat("SoundVolume", Mathf.Log10(volume) * 20f);
}
}
/// <summary>
/// Voice音量
/// </summary>
public float VoiceVolume
{
get
{
if (_disabled)
{
return 0.0f;
}
return _agentVolume[(int)AudioType.Voice];
}
set
{
if (_disabled)
{
return;
}
float volume = Mathf.Clamp(value, 0.0001f, 1.0f);
_agentVolume[(int)AudioType.Voice] = volume;
audioMixer.SetFloat("VoiceVolume", Mathf.Log10(volume) * 20f);
}
}
public bool MusicEnable
{
get
{
if (_disabled)
{
return false;
}
float db;
if (audioMixer.GetFloat("MusicVolume", out db))
{
return db > -80f;
}
else
{
return false;
}
}
set
{
if (_disabled)
{
return;
}
if (value)
{
audioMixer.SetFloat("MusicVolume", Mathf.Log10(_agentVolume[(int)AudioType.Music]) * 20f);
}
else
{
audioMixer.SetFloat("MusicVolume", -80f);
}
}
}
public bool SoundEnable
{
get
{
if (_disabled)
{
return false;
}
return _audioAgents[(int)AudioType.Sound].Enable;
}
set
{
if (_disabled)
{
return;
}
_audioAgents[(int)AudioType.Sound].Enable = value;
}
}
public bool VoiceEnable
{
get
{
if (_disabled)
{
return false;
}
return _audioAgents[(int)AudioType.Voice].Enable;
}
set
{
if (_disabled)
{
return;
}
_audioAgents[(int)AudioType.Voice].Enable = value;
}
}
#endregion
protected override void OnLoad()
{
try
{
TypeInfo typeInfo = typeof(AudioSettings).GetTypeInfo();
PropertyInfo propertyInfo = typeInfo.GetDeclaredProperty("unityAudioDisabled");
_disabled = (bool)propertyInfo.GetValue(null);
if (_disabled)
{
return;
}
}
catch (Exception e)
{
TLogger.LogError(e.ToString());
}
audioMixer = TResources.Load<AudioMixer>("Audio/TEngineAudioMixer.mixer");
for (int i = 0; i < (int)AudioType.Max; ++i)
{
int channelMaxNum = 0;
if (i == (int)AudioType.Sound)
{
channelMaxNum = 10;
}
else
{
channelMaxNum = 1;
}
_audioAgents[i] = new AudioAgent(channelMaxNum, audioMixer.FindMatchingGroups(((AudioType)i).ToString())[0]);
_agentVolume[i] = 1.0f;
}
}
#region
public TAudio Play(AudioType type, string path, bool bLoop = false, float volume = 1.0f, bool bAsync = false, bool bInPool = false)
{
if (_disabled)
{
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 (_disabled)
{
return;
}
_audioAgents[(int)type].Stop(fadeout);
}
public void StopAll(bool fadeout)
{
if (_disabled)
{
return;
}
for (int i = 0; i < (int)AudioType.Max; ++i)
{
if (_audioAgents[i] != null)
{
_audioAgents[i].Stop(fadeout);
}
}
}
public void Restart()
{
if (_disabled)
{
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 (_disabled)
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 (_disabled)
{
return;
}
foreach (string path in list)
{
if (AudioClipPool.ContainsKey(path))
{
AudioClipPool[path].DecRef();
AudioClipPool.Remove(path);
}
}
}
public void CleanSoundPool()
{
if (_disabled)
{
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
}
#region AudioAgent
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 (_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);
}
}
}
}
#endregion
}

View File

@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 9f29360ec6919864bba8e9983366acad
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,326 +0,0 @@
using UnityEngine;
using UnityEngine.Audio;
namespace TEngine
{
public class TAudio
{
#region Propreties
private int _id = 0;
public AssetData _assetData = null;
public AudioSource _source = null;
Transform _transform = null;
private float _volume = 1.0f;
private float _duration = 0;
private float _fadeoutTimer = 0f;
private const float FadeoutDuration = 0.2f;
private bool _inPool = false;
#endregion
#region Public Propreties
enum State
{
None,
Loading,
Playing,
FadingOut,
End,
};
State _state = State.None;
class LoadRequest
{
public string path;
public bool bAsync;
}
LoadRequest _pendingLoad = null;
public int ID
{
get
{
return _id;
}
}
public float Volume
{
set
{
if (_source != null)
{
_volume = value;
_source.volume = _volume;
}
}
get
{
return _volume;
}
}
public bool IsFinish
{
get
{
if (_source != null)
{
return _state == State.End;
}
else
{
return true;
}
}
}
public float Duration
{
get
{
return _duration;
}
}
public float Length
{
get
{
if (_source != null && _source.clip != null)
{
return _source.clip.length;
}
return 0;
}
}
public Vector3 Position
{
get
{
return _transform.position;
}
set
{
_transform.position = value;
}
}
public bool IsLoop
{
get
{
if (_source != null)
{
return _source.loop;
}
else
{
return false;
}
}
set
{
if (_source != null)
{
_source.loop = value;
}
}
}
internal bool IsPlaying
{
get
{
if (_source != null && _source.isPlaying)
{
return true;
}
else
{
return false;
}
}
}
#endregion
public AudioSource AudioResource()
{
return _source;
}
public static TAudio Create(string path, bool bAsync, AudioMixerGroup audioMixerGroup = null, bool bInPool = false)
{
TAudio audio = new TAudio();
audio.Init(audioMixerGroup);
audio.Load(path, bAsync, bInPool);
return audio;
}
public void Init(AudioMixerGroup audioMixerGroup = null)
{
GameObject root = new GameObject("Audio");
root.transform.SetParent(AudioMgr.Instance.transform);
root.transform.localPosition = Vector3.zero;
_transform = root.transform;
_source = root.AddComponent<AudioSource>();
_source.playOnAwake = false;
if (audioMixerGroup != null)
{
_source.outputAudioMixerGroup = audioMixerGroup;
}
_id = _source.GetInstanceID();
}
public void Load(string path, bool bAsync, bool bInPool = false)
{
_inPool = bInPool;
if (_state == State.None || _state == State.End)
{
_duration = 0;
if (!string.IsNullOrEmpty(path))
{
if (AudioMgr.Instance.AudioClipPool.ContainsKey(path))
{
OnAssetLoadComplete(AudioMgr.Instance.AudioClipPool[path]);
return;
}
if (bAsync)
{
_state = State.Loading;
ResMgr.Instance.GetAssetAsync(path, false, OnAssetLoadComplete);
}
else
{
OnAssetLoadComplete(ResMgr.Instance.GetAsset(path, false));
}
}
}
else
{
_pendingLoad = new LoadRequest { path = path, bAsync = bAsync };
if (_state == State.Playing)
{
Stop(true);
}
}
}
public void Stop(bool fadeout = false)
{
if (_source != null)
{
if (fadeout)
{
_fadeoutTimer = FadeoutDuration;
_state = State.FadingOut;
}
else
{
_source.Stop();
_state = State.End;
}
}
}
void OnAssetLoadComplete(AssetData assetData)
{
if (assetData != null)
{
assetData.OnAsyncLoadComplete -= OnAssetLoadComplete;
if (_inPool && !AudioMgr.Instance.AudioClipPool.ContainsKey(assetData.Path))
{
assetData.AddRef();
AudioMgr.Instance.AudioClipPool.Add(assetData.Path, assetData);
}
}
if (_pendingLoad != null)
{
assetData.AddRef();
if (assetData != null)
{
assetData.DecRef();
}
_state = State.End;
string path = _pendingLoad.path;
bool bAsync = _pendingLoad.bAsync;
_pendingLoad = null;
Load(path, bAsync);
}
else if (assetData != null)
{
assetData.AddRef();
if (_assetData != null)
{
_assetData.DecRef();
}
_assetData = assetData;
_source.clip = _assetData.AssetObject as AudioClip;
if (_source.clip != null)
{
_source.Play();
_state = State.Playing;
}
else
{
_state = State.End;
}
}
else
{
_state = State.End;
}
}
public void Update(float delta)
{
if (_state == State.Playing)
{
if (!_source.isPlaying)
{
_state = State.End;
}
}
else if (_state == State.FadingOut)
{
if (_fadeoutTimer > 0f)
{
_fadeoutTimer -= delta;
_source.volume = _volume * _fadeoutTimer / FadeoutDuration;
}
else
{
Stop();
if (_pendingLoad != null)
{
string path = _pendingLoad.path;
bool bAsync = _pendingLoad.bAsync;
_pendingLoad = null;
Load(path, bAsync);
}
_source.volume = _volume;
}
}
_duration += delta;
}
public void Destroy()
{
if (_transform != null)
{
Object.Destroy(_transform.gameObject);
}
if (_assetData != null)
{
_assetData.DecRef();
}
}
}
}

View File

@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 640620be2dcc1d34ca617d92b2bac265
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: