From b560feb5cb7b3364166f6ba4f936817e8938bc81 Mon Sep 17 00:00:00 2001 From: ALEXTANG <574809918@qq.com> Date: Thu, 26 May 2022 00:10:47 +0800 Subject: [PATCH] =?UTF-8?q?Audio=20Add=20=E9=9F=B3=E9=A2=91=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E6=8F=90=E4=BA=A4=E4=B8=80=E9=83=A8=E5=88=86=EF=BC=8C?= =?UTF-8?q?TODO=E6=9A=82=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Audio Add 音频模块提交一部分,TODO暂定 --- Assets/TEngine/Runtime/Audio.meta | 8 + Assets/TEngine/Runtime/Audio/AudioManager.cs | 85 +++++ .../Runtime/Audio/AudioManager.cs.meta | 11 + Assets/TEngine/Runtime/Audio/TAudio.cs | 309 ++++++++++++++++++ Assets/TEngine/Runtime/Audio/TAudio.cs.meta | 11 + Assets/TEngine/Runtime/Res/ResMgr.cs | 11 + 6 files changed, 435 insertions(+) create mode 100644 Assets/TEngine/Runtime/Audio.meta create mode 100644 Assets/TEngine/Runtime/Audio/AudioManager.cs create mode 100644 Assets/TEngine/Runtime/Audio/AudioManager.cs.meta create mode 100644 Assets/TEngine/Runtime/Audio/TAudio.cs create mode 100644 Assets/TEngine/Runtime/Audio/TAudio.cs.meta diff --git a/Assets/TEngine/Runtime/Audio.meta b/Assets/TEngine/Runtime/Audio.meta new file mode 100644 index 00000000..a30fc79d --- /dev/null +++ b/Assets/TEngine/Runtime/Audio.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: c9052261b332b294f97edcc2812f4eea +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/TEngine/Runtime/Audio/AudioManager.cs b/Assets/TEngine/Runtime/Audio/AudioManager.cs new file mode 100644 index 00000000..5df27545 --- /dev/null +++ b/Assets/TEngine/Runtime/Audio/AudioManager.cs @@ -0,0 +1,85 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using UnityEngine; + +namespace TEngine +{ + public enum AudioType + { + Sound, + Music, + Voice, + Max + } + + public class AudioManager : UnitySingleton + { + public Dictionary AudioClipPool = new Dictionary(); + private float _volume = 1f; + private bool _enable = true; + private bool _bUnityAudioDisabled = false; + + public float Volume + { + get + { + if (_bUnityAudioDisabled) + { + return 0.0f; + } + return _volume; + } + set + { + if (_bUnityAudioDisabled) + { + return; + } + _volume = value; + AudioListener.volume = _volume; + } + } + + public bool Enable + { + get + { + if (_bUnityAudioDisabled) + { + return false; + } + return _enable; + } + set + { + if (_bUnityAudioDisabled) + { + return; + } + _enable = value; + AudioListener.volume = _enable ? _volume : 0f; + } + } + + protected override void OnLoad() + { + try + { + TypeInfo typeInfo = typeof(AudioSettings).GetTypeInfo(); + PropertyInfo propertyInfo = typeInfo.GetDeclaredProperty("unityAudioDisabled"); + _bUnityAudioDisabled = (bool)propertyInfo.GetValue(null); + if (_bUnityAudioDisabled) + { + return; + } + } + catch (Exception e) + { + TLogger.LogError(e.ToString()); + } + } + + } +} diff --git a/Assets/TEngine/Runtime/Audio/AudioManager.cs.meta b/Assets/TEngine/Runtime/Audio/AudioManager.cs.meta new file mode 100644 index 00000000..fddec7eb --- /dev/null +++ b/Assets/TEngine/Runtime/Audio/AudioManager.cs.meta @@ -0,0 +1,11 @@ +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 new file mode 100644 index 00000000..fc56142f --- /dev/null +++ b/Assets/TEngine/Runtime/Audio/TAudio.cs @@ -0,0 +1,309 @@ +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, bool bInPool = false) + { + TAudio audio = new TAudio(); + audio.Init(audioMixerGroup); + audio.Load(path, bAsync, bInPool); + + return audio; + } + + public void Init(AudioMixerGroup audioMixerGroup) + { + GameObject host = new GameObject("Audio"); + host.transform.SetParent(AudioManager.Instance.transform); + host.transform.localPosition = Vector3.zero; + _transform = host.transform; + _source = host.AddComponent(); + _source.playOnAwake = false; + _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 (AudioManager.Instance.AudioClipPool.ContainsKey(path)) + { + OnAssetLoadComplete(AudioManager.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 && !AudioManager.Instance.AudioClipPool.ContainsKey(assetData.Path)) + { + assetData.AddRef(); + AudioManager.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) + { + GameObject.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 new file mode 100644 index 00000000..041f0abb --- /dev/null +++ b/Assets/TEngine/Runtime/Audio/TAudio.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 640620be2dcc1d34ca617d92b2bac265 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/TEngine/Runtime/Res/ResMgr.cs b/Assets/TEngine/Runtime/Res/ResMgr.cs index 385a9c1e..f8257f39 100644 --- a/Assets/TEngine/Runtime/Res/ResMgr.cs +++ b/Assets/TEngine/Runtime/Res/ResMgr.cs @@ -233,6 +233,17 @@ namespace TEngine #endif } + public void GetAssetAsync(string path, bool withSubAssets, System.Action onComplete) + { + if (string.IsNullOrEmpty(path)) onComplete(null); + +#if ASSETBUNDLE_ENABLE + _assetConfig.GetAssetAtPathAsync(path, withSubAssets, onComplete); +#else + onComplete(GetAsset(path, withSubAssets)); +#endif + } + #endregion public bool Exists(string path)