From 6bd585d64e5ffda38aeadb2037ee79edcd5d956c Mon Sep 17 00:00:00 2001 From: ALEXTANG <574809918@qq.com> Date: Fri, 27 May 2022 12:37:49 +0800 Subject: [PATCH] Modules Modules --- .../TEngine/Modules~/AudioModule.unitypackage | Bin 0 -> 5901 bytes .../UIModle.unitypackage | Bin Assets/TEngine/Runtime/Audio.meta | 8 - Assets/TEngine/Runtime/Audio/AudioMgr.cs | 531 ------------------ Assets/TEngine/Runtime/Audio/AudioMgr.cs.meta | 11 - Assets/TEngine/Runtime/Audio/TAudio.cs | 326 ----------- Assets/TEngine/Runtime/Audio/TAudio.cs.meta | 11 - 7 files changed, 887 deletions(-) create mode 100644 Assets/TEngine/Modules~/AudioModule.unitypackage rename Assets/TEngine/{UIFrameWork~ => Modules~}/UIModle.unitypackage (100%) delete mode 100644 Assets/TEngine/Runtime/Audio.meta delete mode 100644 Assets/TEngine/Runtime/Audio/AudioMgr.cs delete mode 100644 Assets/TEngine/Runtime/Audio/AudioMgr.cs.meta delete mode 100644 Assets/TEngine/Runtime/Audio/TAudio.cs delete mode 100644 Assets/TEngine/Runtime/Audio/TAudio.cs.meta diff --git a/Assets/TEngine/Modules~/AudioModule.unitypackage b/Assets/TEngine/Modules~/AudioModule.unitypackage new file mode 100644 index 0000000000000000000000000000000000000000..c79955db22a66afb7a2f2d87a665dee31bdfe32c GIT binary patch literal 5901 zcmV+o7xL&IiwFo?Rghu?0AX@tXmn+5a4vLVascfe*=zhnxX%Uuhj_Bpy5>Gw4{EJi zZ#)qZnIw}M-6m@`t5z)tBKqI~ULXj*2>K!lf-heGjOUa8g5S*KPIfcdZd0q7pV-}G zj&HtuzM1(}c2&_)BrIDday?14ElssGNmEQm)+JH9Y2)lg6jjkM4ERe+>2La3Qe{;Y zHCa=X4N;OMRoM`O3 zUxj}gLN7G=pVQy-C-}}VI!5v1IGXukup`JIKi_k zHt)b5nor`x<7pVhING^lQxNdW@F_r(2kqyu6VG;pS5L{~`3y&Qpcw54)>hhReA5)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FKsmLCI5>iR`b7zgZ%eXmgfIUFR~9A0ROWE$)=;3*wt0bvpi4Lk>Qv~H5IVL zwy9jy@Bb_MkpHij(n|jx{6EO{nD~FXB54xThmZ20|NAIS{$Jw$wXsGm`(G{jU$aA4 za@d;uwFU0l;H{lQX(4hy`JZK@>iDw5}|D!k-2%W4;wxU4JcT4pw z14Al!*_32Y#TZ?b|7R$J|JO%prGFp(pRU$^|3e)7zh26+_+J}u4Edige`%v}FaDpR j)_(s(5(oM3qcq3=1}AVRLmA3Y{_Eu*2Zk`10L}ma(Hy8@ literal 0 HcmV?d00001 diff --git a/Assets/TEngine/UIFrameWork~/UIModle.unitypackage b/Assets/TEngine/Modules~/UIModle.unitypackage similarity index 100% rename from Assets/TEngine/UIFrameWork~/UIModle.unitypackage rename to Assets/TEngine/Modules~/UIModle.unitypackage diff --git a/Assets/TEngine/Runtime/Audio.meta b/Assets/TEngine/Runtime/Audio.meta deleted file mode 100644 index a30fc79d..00000000 --- a/Assets/TEngine/Runtime/Audio.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: c9052261b332b294f97edcc2812f4eea -folderAsset: yes -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/TEngine/Runtime/Audio/AudioMgr.cs b/Assets/TEngine/Runtime/Audio/AudioMgr.cs deleted file mode 100644 index 40dade4a..00000000 --- a/Assets/TEngine/Runtime/Audio/AudioMgr.cs +++ /dev/null @@ -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 - { - #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 AudioClipPool = new Dictionary(); - #endregion - - #region 控制器 - /// - /// 总音量 - /// - public float Volume - { - get - { - if (_disabled) - { - return 0.0f; - } - return _volume; - } - set - { - if (_disabled) - { - return; - } - _volume = value; - AudioListener.volume = _volume; - } - } - - /// - /// 总开关 - /// - public bool Enable - { - get - { - if (_disabled) - { - return false; - } - return _enable; - } - set - { - if (_disabled) - { - return; - } - _enable = value; - AudioListener.volume = _enable ? _volume : 0f; - } - } - - /// - /// 背景音量 - /// - 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); - } - } - - /// - /// 音效音量 - /// - 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); - } - } - - /// - /// Voice音量 - /// - 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("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 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 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 _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 (_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 -} diff --git a/Assets/TEngine/Runtime/Audio/AudioMgr.cs.meta b/Assets/TEngine/Runtime/Audio/AudioMgr.cs.meta deleted file mode 100644 index fddec7eb..00000000 --- a/Assets/TEngine/Runtime/Audio/AudioMgr.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9f29360ec6919864bba8e9983366acad -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/TEngine/Runtime/Audio/TAudio.cs b/Assets/TEngine/Runtime/Audio/TAudio.cs deleted file mode 100644 index e32a94a2..00000000 --- a/Assets/TEngine/Runtime/Audio/TAudio.cs +++ /dev/null @@ -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(); - _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(); - } - } - } -} diff --git a/Assets/TEngine/Runtime/Audio/TAudio.cs.meta b/Assets/TEngine/Runtime/Audio/TAudio.cs.meta deleted file mode 100644 index 041f0abb..00000000 --- a/Assets/TEngine/Runtime/Audio/TAudio.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 640620be2dcc1d34ca617d92b2bac265 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: