Audio Add 音频模块提交一部分,TODO暂定

Audio Add 音频模块提交一部分,TODO暂定
This commit is contained in:
ALEXTANG
2022-05-26 00:10:47 +08:00
parent 73a8fcf0bd
commit b560feb5cb
6 changed files with 435 additions and 0 deletions

View File

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

View File

@@ -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<AudioManager>
{
public Dictionary<string, AssetData> AudioClipPool = new Dictionary<string, AssetData>();
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());
}
}
}
}

View File

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

View File

@@ -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<AudioSource>();
_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();
}
}
}
}

View File

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

View File

@@ -233,6 +233,17 @@ namespace TEngine
#endif
}
public void GetAssetAsync(string path, bool withSubAssets, System.Action<AssetData> 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)