From 10f86934a5fcf06935397a6839f6196b34d19c37 Mon Sep 17 00:00:00 2001 From: ALEXTANG <574809918@qq.com> Date: Thu, 26 May 2022 10:59:02 +0800 Subject: [PATCH] Enable AudioMixer Enable AudioMixer --- Assets/TEngine/Runtime/Audio/AudioMgr.cs | 229 ++++++++++++++++-- Assets/TEngine/Runtime/Audio/TAudio.cs | 19 +- Assets/TResources/Audio.meta | 8 + .../TResources/Audio/TEngineAudioMixer.mixer | 194 +++++++++++++++ .../Audio/TEngineAudioMixer.mixer.meta | 8 + 5 files changed, 438 insertions(+), 20 deletions(-) create mode 100644 Assets/TResources/Audio.meta create mode 100644 Assets/TResources/Audio/TEngineAudioMixer.mixer create mode 100644 Assets/TResources/Audio/TEngineAudioMixer.mixer.meta diff --git a/Assets/TEngine/Runtime/Audio/AudioMgr.cs b/Assets/TEngine/Runtime/Audio/AudioMgr.cs index 45dd2346..6d6ab3e3 100644 --- a/Assets/TEngine/Runtime/Audio/AudioMgr.cs +++ b/Assets/TEngine/Runtime/Audio/AudioMgr.cs @@ -18,18 +18,26 @@ namespace TEngine 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 _soundConfigDic = new Dictionary(); public Dictionary AudioClipPool = new Dictionary(); - private float _volume = 1f; - private bool _enable = true; - private bool _bUnityAudioDisabled = false; + #endregion + #region 控制器 + /// + /// 总音量 + /// 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 } } + /// + /// 总开关 + /// 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 } } + /// + /// 背景音量 + /// + 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"); - _bUnityAudioDisabled = (bool)propertyInfo.GetValue(null); - if (_bUnityAudioDisabled) + _disabled = (bool)propertyInfo.GetValue(null); + if (_disabled) { return; } - //_soundConfigDic = JsonConvert.DeserializeObject>(str); } 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 (_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 list) { - if (_bUnityAudioDisabled) + if (_disabled) return; foreach (string path in list) { @@ -173,7 +352,7 @@ namespace TEngine public void RemoveClipFromPool(List 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 _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 } diff --git a/Assets/TEngine/Runtime/Audio/TAudio.cs b/Assets/TEngine/Runtime/Audio/TAudio.cs index 40fb000f..c1ce0722 100644 --- a/Assets/TEngine/Runtime/Audio/TAudio.cs +++ b/Assets/TEngine/Runtime/Audio/TAudio.cs @@ -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) diff --git a/Assets/TResources/Audio.meta b/Assets/TResources/Audio.meta new file mode 100644 index 00000000..9941c48a --- /dev/null +++ b/Assets/TResources/Audio.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: a28c58ed649f9ff46a7c8a483765b835 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/TResources/Audio/TEngineAudioMixer.mixer b/Assets/TResources/Audio/TEngineAudioMixer.mixer new file mode 100644 index 00000000..c100bb12 --- /dev/null +++ b/Assets/TResources/Audio/TEngineAudioMixer.mixer @@ -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 diff --git a/Assets/TResources/Audio/TEngineAudioMixer.mixer.meta b/Assets/TResources/Audio/TEngineAudioMixer.mixer.meta new file mode 100644 index 00000000..2555a6a0 --- /dev/null +++ b/Assets/TResources/Audio/TEngineAudioMixer.mixer.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 3162afbd7fefd6c40bce53982808aa3c +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 0 + userData: + assetBundleName: + assetBundleVariant: