Enable AudioMixer

Enable AudioMixer
This commit is contained in:
ALEXTANG
2022-05-26 10:59:02 +08:00
parent dab3575459
commit 10f86934a5
5 changed files with 438 additions and 20 deletions

View File

@@ -18,18 +18,26 @@ namespace TEngine
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, int> _soundConfigDic = new Dictionary<string, int>();
public Dictionary<string, AssetData> AudioClipPool = new Dictionary<string, AssetData>();
private float _volume = 1f;
private bool _enable = true;
private bool _bUnityAudioDisabled = false;
#endregion
#region
/// <summary>
/// 总音量
/// </summary>
public float Volume
{
get
{
if (_bUnityAudioDisabled)
if (_disabled)
{
return 0.0f;
}
@@ -37,7 +45,7 @@ namespace TEngine
}
set
{
if (_bUnityAudioDisabled)
if (_disabled)
{
return;
}
@@ -46,11 +54,14 @@ namespace TEngine
}
}
/// <summary>
/// 总开关
/// </summary>
public bool Enable
{
get
{
if (_bUnityAudioDisabled)
if (_disabled)
{
return false;
}
@@ -58,7 +69,7 @@ namespace TEngine
}
set
{
if (_bUnityAudioDisabled)
if (_disabled)
{
return;
}
@@ -67,29 +78,197 @@ namespace TEngine
}
}
/// <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");
_bUnityAudioDisabled = (bool)propertyInfo.GetValue(null);
if (_bUnityAudioDisabled)
_disabled = (bool)propertyInfo.GetValue(null);
if (_disabled)
{
return;
}
//_soundConfigDic = JsonConvert.DeserializeObject<Dictionary<string, int>>(str);
}
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 (_bUnityAudioDisabled)
if (_disabled)
{
return null;
}
@@ -106,7 +285,7 @@ namespace TEngine
public void Stop(AudioType type, bool fadeout)
{
if (_bUnityAudioDisabled)
if (_disabled)
{
return;
}
@@ -115,7 +294,7 @@ namespace TEngine
public void StopAll(bool fadeout)
{
if (_bUnityAudioDisabled)
if (_disabled)
{
return;
}
@@ -131,7 +310,7 @@ namespace TEngine
public void Restart()
{
if (_bUnityAudioDisabled)
if (_disabled)
{
return;
}
@@ -159,7 +338,7 @@ namespace TEngine
#region Pool
public void PutInAudioPool(List<string> list)
{
if (_bUnityAudioDisabled)
if (_disabled)
return;
foreach (string path in list)
{
@@ -173,7 +352,7 @@ namespace TEngine
public void RemoveClipFromPool(List<string> list)
{
if (_bUnityAudioDisabled)
if (_disabled)
{
return;
}
@@ -189,7 +368,7 @@ namespace TEngine
public void CleanSoundPool()
{
if (_bUnityAudioDisabled)
if (_disabled)
{
return;
}
@@ -213,6 +392,7 @@ namespace TEngine
#endregion
}
#region AudioAgent
public class AudioAgent
{
public List<TAudio> _audioObjects;
@@ -273,7 +453,10 @@ namespace TEngine
public TAudio Play(string path, bool bAsync, bool bInPool = false)
{
if (!_bEnable) return null;
if (!_bEnable)
{
return null;
}
int freeChannel = -1;
float duration = -1;
int num = 0;
@@ -282,7 +465,9 @@ namespace TEngine
if (_audioObjects[i] != null && _audioObjects[i]._assetData != null && _audioObjects[i].IsFinish == false)
{
if (path.Equals(_audioObjects[i]._assetData.Path))
{
num++;
}
}
}
@@ -295,6 +480,7 @@ namespace TEngine
if (_audioObjects[i].Duration > duration)
{
duration = _audioObjects[i].Duration;
freeChannel = i;
}
}
@@ -304,11 +490,13 @@ namespace TEngine
if (_audioObjects[i]._assetData == null || _audioObjects[i].IsFinish == true)
{
freeChannel = i;
break;
}
else if (_audioObjects[i].Duration > duration)
{
duration = _audioObjects[i].Duration;
freeChannel = i;
}
}
@@ -317,9 +505,13 @@ namespace TEngine
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
@@ -351,4 +543,5 @@ namespace TEngine
}
}
}
#endregion
}

View File

@@ -66,9 +66,13 @@ namespace TEngine
get
{
if (_source != null)
{
return _state == State.End;
}
else
{
return true;
}
}
}
@@ -109,14 +113,20 @@ namespace TEngine
get
{
if (_source != null)
{
return _source.loop;
}
else
{
return false;
}
}
set
{
if (_source != null)
{
_source.loop = value;
}
}
}
internal bool IsPlaying
@@ -233,7 +243,9 @@ namespace TEngine
{
assetData.AddRef();
if (assetData != null)
{
assetData.DecRef();
}
_state = State.End;
string path = _pendingLoad.path;
bool bAsync = _pendingLoad.bAsync;
@@ -244,9 +256,10 @@ namespace TEngine
{
assetData.AddRef();
if (_assetData != null)
{
_assetData.DecRef();
}
_assetData = assetData;
_source.clip = _assetData.AssetObject as AudioClip;
if (_source.clip != null)
{
@@ -269,7 +282,9 @@ namespace TEngine
if (_state == State.Playing)
{
if (!_source.isPlaying)
{
_state = State.End;
}
}
else if (_state == State.FadingOut)
{
@@ -299,7 +314,7 @@ namespace TEngine
{
if (_transform != null)
{
GameObject.Destroy(_transform.gameObject);
Object.Destroy(_transform.gameObject);
}
if (_assetData != null)

View File

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

View File

@@ -0,0 +1,194 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!244 &-5700573295807266226
AudioMixerEffectController:
m_ObjectHideFlags: 3
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name:
m_EffectID: 4484537926adf3943b8d15631fe4c9a8
m_EffectName: Attenuation
m_MixLevel: 2a6cc932f56dd434391cf093c811be4a
m_Parameters: []
m_SendTarget: {fileID: 0}
m_EnableWetMix: 0
m_Bypass: 0
--- !u!243 &-2688087525938382097
AudioMixerGroupController:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Voice
m_AudioMixer: {fileID: 24100000}
m_GroupID: 3385c639676b0174a96c1ba9d8e7d7e8
m_Children: []
m_Volume: 351c3633d7a368b48b9993d12d4b49d1
m_Pitch: 9ae2cf17f804a73499b51997d4703eee
m_Send: 00000000000000000000000000000000
m_Effects:
- {fileID: 3233559901938498695}
m_UserColorIndex: 0
m_Mute: 0
m_Solo: 0
m_BypassEffects: 0
--- !u!244 &-2140790635248599779
AudioMixerEffectController:
m_ObjectHideFlags: 3
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name:
m_EffectID: 45c78cf1965841c4cb0d3c8abef208ec
m_EffectName: Send
m_MixLevel: 6a8d829224d69b046b7ea67a0682d9e8
m_Parameters: []
m_SendTarget: {fileID: 0}
m_EnableWetMix: 0
m_Bypass: 0
--- !u!241 &24100000
AudioMixerController:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: TEngineAudioMixer
m_OutputGroup: {fileID: 0}
m_MasterGroup: {fileID: 24300002}
m_Snapshots:
- {fileID: 24500006}
m_StartSnapshot: {fileID: 24500006}
m_SuspendThreshold: -80
m_EnableSuspend: 1
m_UpdateMode: 0
m_ExposedParameters:
- guid: 5542a83cb62d88f4bb57676181f23921
name: MusicVolume
- guid: c48081496ad8d11438c24892996c3a81
name: SoundVolume
- guid: 351c3633d7a368b48b9993d12d4b49d1
name: VoiceVolume
m_AudioMixerGroupViews:
- guids:
- 5a6f013f4ed308e49981ccc37373e521
- 1f218ab66c2b2b641b0d2ac1c8714263
- 503f9da271c2c6543b27f7c13a51e875
- 3385c639676b0174a96c1ba9d8e7d7e8
name: View
m_CurrentViewIndex: 0
m_TargetSnapshot: {fileID: 24500006}
--- !u!243 &24300002
AudioMixerGroupController:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Master
m_AudioMixer: {fileID: 24100000}
m_GroupID: 5a6f013f4ed308e49981ccc37373e521
m_Children:
- {fileID: 9185764251035995994}
- {fileID: -2688087525938382097}
- {fileID: 3168079212996900356}
m_Volume: f0efd2d64837bfe4d98d42033680eafc
m_Pitch: 86a3dfc09cd72544983864f031c3d96c
m_Send: 00000000000000000000000000000000
m_Effects:
- {fileID: 24400004}
- {fileID: -2140790635248599779}
m_UserColorIndex: 0
m_Mute: 0
m_Solo: 0
m_BypassEffects: 0
--- !u!244 &24400004
AudioMixerEffectController:
m_ObjectHideFlags: 3
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name:
m_EffectID: 43f3c3fd8be9c2649aaef2ae5860bb30
m_EffectName: Attenuation
m_MixLevel: c02fd21fa18282e498be1c97d0eef2ac
m_Parameters: []
m_SendTarget: {fileID: 0}
m_EnableWetMix: 0
m_Bypass: 0
--- !u!245 &24500006
AudioMixerSnapshotController:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Snapshot
m_AudioMixer: {fileID: 24100000}
m_SnapshotID: 5f631808614c40848b912e405ee09d3c
m_FloatValues: {}
m_TransitionOverrides: {}
--- !u!243 &3168079212996900356
AudioMixerGroupController:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Sound
m_AudioMixer: {fileID: 24100000}
m_GroupID: 503f9da271c2c6543b27f7c13a51e875
m_Children: []
m_Volume: c48081496ad8d11438c24892996c3a81
m_Pitch: 8806fa02b4e88c64b9bc81979e7cbe4e
m_Send: 00000000000000000000000000000000
m_Effects:
- {fileID: 8680063345532094417}
m_UserColorIndex: 0
m_Mute: 0
m_Solo: 0
m_BypassEffects: 0
--- !u!244 &3233559901938498695
AudioMixerEffectController:
m_ObjectHideFlags: 3
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name:
m_EffectID: 7e159eaf73c3d5f4d8b9cb86ca2fbdc9
m_EffectName: Attenuation
m_MixLevel: 35f98f9c693a7ad4f84978dca6c3cb3e
m_Parameters: []
m_SendTarget: {fileID: 0}
m_EnableWetMix: 0
m_Bypass: 0
--- !u!244 &8680063345532094417
AudioMixerEffectController:
m_ObjectHideFlags: 3
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name:
m_EffectID: 14375aad2b8c71942bb00259d2b03c8f
m_EffectName: Attenuation
m_MixLevel: 733f99698ea57374f807c226629a12c5
m_Parameters: []
m_SendTarget: {fileID: 0}
m_EnableWetMix: 0
m_Bypass: 0
--- !u!243 &9185764251035995994
AudioMixerGroupController:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Music
m_AudioMixer: {fileID: 24100000}
m_GroupID: 1f218ab66c2b2b641b0d2ac1c8714263
m_Children: []
m_Volume: 5542a83cb62d88f4bb57676181f23921
m_Pitch: d1b13bce1c2d3c047975c89e79aeff05
m_Send: 00000000000000000000000000000000
m_Effects:
- {fileID: -5700573295807266226}
m_UserColorIndex: 0
m_Mute: 0
m_Solo: 0
m_BypassEffects: 0

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 3162afbd7fefd6c40bce53982808aa3c
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 0
userData:
assetBundleName:
assetBundleVariant: