From 123a5efcc3a3ba4981b8f6307b040d3c47905da4 Mon Sep 17 00:00:00 2001 From: Blank Date: Fri, 8 Nov 2024 17:31:48 +0800 Subject: [PATCH] =?UTF-8?q?[=E5=A2=9E=E5=8A=A0]1.=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E5=BA=8F=E5=88=97=E7=BC=96=E5=8F=B7?= =?UTF-8?q?=E7=9A=84=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Runtime/Sound/Sound/ISoundManager.cs | 3 +- Runtime/Sound/Sound/SoundManager.cs | 37 +++++++++-- Runtime/Sound/SoundComponent.cs | 97 +++++++++++++--------------- 3 files changed, 76 insertions(+), 61 deletions(-) diff --git a/Runtime/Sound/Sound/ISoundManager.cs b/Runtime/Sound/Sound/ISoundManager.cs index b21bfd1..3f7dc8f 100644 --- a/Runtime/Sound/Sound/ISoundManager.cs +++ b/Runtime/Sound/Sound/ISoundManager.cs @@ -193,8 +193,9 @@ public interface ISoundManager /// 加载声音资源的优先级。 /// 播放声音参数。 /// 用户自定义数据。 + /// 序列编号 /// 声音的序列编号。 - UniTask PlaySound(string soundAssetName, string soundGroupName, int priority, PlaySoundParams playSoundParams, object userData); + UniTask PlaySound(string soundAssetName, string soundGroupName, int priority, PlaySoundParams playSoundParams, object userData, int serialId); /// /// 停止播放声音。 diff --git a/Runtime/Sound/Sound/SoundManager.cs b/Runtime/Sound/Sound/SoundManager.cs index b8c0d4a..96039bc 100644 --- a/Runtime/Sound/Sound/SoundManager.cs +++ b/Runtime/Sound/Sound/SoundManager.cs @@ -367,6 +367,18 @@ public UniTask PlaySound(string soundAssetName, string soundGroupName, int return PlaySound(soundAssetName, soundGroupName, priority, playSoundParams, null); } + /// + /// 播放声音。并设置指定的序列编号 + /// + /// 声音资源名称。 + /// 声音组名称。 + /// 加载声音资源的优先级。 + /// 声音的序列编号。 + public UniTask PlaySoundBySerialId(string soundAssetName, string soundGroupName, int serialId) + { + return PlaySound(soundAssetName, soundGroupName, Constant.DefaultPriority, null, null, serialId); + } + /// /// 播放声音。 /// @@ -401,8 +413,9 @@ public async UniTask PlaySound(string soundAssetName, string soundGroupName /// 加载声音资源的优先级。 /// 播放声音参数。 /// 用户自定义数据。 + /// 序列编号 /// 声音的序列编号。 - public async UniTask PlaySound(string soundAssetName, string soundGroupName, int priority, PlaySoundParams playSoundParams, object userData) + public async UniTask PlaySound(string soundAssetName, string soundGroupName, int priority, PlaySoundParams playSoundParams, object userData, int serialId = -1) { if (_assetManager == null) { @@ -419,7 +432,17 @@ public async UniTask PlaySound(string soundAssetName, string soundGroupName playSoundParams = PlaySoundParams.Create(); } - int serialId = ++m_Serial; + int newSerialId; + if (serialId >= 0) + { + newSerialId = serialId; + } + else + { + newSerialId = ++m_Serial; + } + + PlaySoundErrorCode? errorCode = null; string errorMessage = null; SoundGroup soundGroup = (SoundGroup)GetSoundGroup(soundGroupName); @@ -439,7 +462,7 @@ public async UniTask PlaySound(string soundAssetName, string soundGroupName if (m_PlaySoundFailureEventHandler != null) { PlaySoundFailureEventArgs playSoundFailureEventArgs = - PlaySoundFailureEventArgs.Create(serialId, soundAssetName, soundGroupName, playSoundParams, errorCode.Value, errorMessage, userData); + PlaySoundFailureEventArgs.Create(newSerialId, soundAssetName, soundGroupName, playSoundParams, errorCode.Value, errorMessage, userData); m_PlaySoundFailureEventHandler(this, playSoundFailureEventArgs); // ReferencePool.Release(playSoundFailureEventArgs); @@ -448,23 +471,23 @@ public async UniTask PlaySound(string soundAssetName, string soundGroupName ReferencePool.Release(playSoundParams); } - return serialId; + return newSerialId; } throw new GameFrameworkException(errorMessage); } - m_SoundsBeingLoaded.Add(serialId); + m_SoundsBeingLoaded.Add(newSerialId); var assetOperationHandle = await _assetManager.LoadAssetAsync(soundAssetName); void OnAssetOperationHandleOnCompleted(AssetHandle assetHandle) { var assetObject = assetHandle.GetAssetObject(); - LoadAssetSuccessCallback(soundAssetName, assetObject, assetObject.length, PlaySoundInfo.Create(serialId, soundGroup, playSoundParams, userData)); + LoadAssetSuccessCallback(soundAssetName, assetObject, assetObject.length, PlaySoundInfo.Create(newSerialId, soundGroup, playSoundParams, userData)); } assetOperationHandle.Completed += OnAssetOperationHandleOnCompleted; - return serialId; + return newSerialId; } /// diff --git a/Runtime/Sound/SoundComponent.cs b/Runtime/Sound/SoundComponent.cs index 1c8fd64..caaf826 100644 --- a/Runtime/Sound/SoundComponent.cs +++ b/Runtime/Sound/SoundComponent.cs @@ -31,22 +31,19 @@ public sealed partial class SoundComponent : GameFrameworkComponent private EventComponent m_EventComponent = null; private AudioListener m_AudioListener = null; - // [SerializeField] private bool m_EnablePlaySoundUpdateEvent = false; - - [SerializeField] private Transform m_InstanceRoot = null; [SerializeField] private AudioMixer m_AudioMixer = null; - [SerializeField] private string m_SoundHelperTypeName = "UnityGameFramework.Runtime.DefaultSoundHelper"; + [SerializeField] private string m_SoundHelperTypeName = "GameFrameX.Sound.Runtime.DefaultSoundHelper"; [SerializeField] private SoundHelperBase m_CustomSoundHelper = null; - [SerializeField] private string m_SoundGroupHelperTypeName = "UnityGameFramework.Runtime.DefaultSoundGroupHelper"; + [SerializeField] private string m_SoundGroupHelperTypeName = "GameFrameX.Sound.Runtime.DefaultSoundGroupHelper"; [SerializeField] private SoundGroupHelperBase m_CustomSoundGroupHelper = null; - [SerializeField] private string m_SoundAgentHelperTypeName = "UnityGameFramework.Runtime.DefaultSoundAgentHelper"; + [SerializeField] private string m_SoundAgentHelperTypeName = "GameFrameX.Sound.Runtime.DefaultSoundAgentHelper"; [SerializeField] private SoundAgentHelperBase m_CustomSoundAgentHelper = null; @@ -127,10 +124,7 @@ private void Start() return; } - - { - m_SoundManager.SetResourceManager(GameFrameworkEntry.GetModule()); - } + m_SoundManager.SetResourceManager(GameFrameworkEntry.GetModule()); SoundHelperBase soundHelper = Helper.CreateHelper(m_SoundHelperTypeName, m_CustomSoundHelper); if (soundHelper == null) @@ -155,10 +149,10 @@ private void Start() for (int i = 0; i < m_SoundGroups.Length; i++) { - if (!AddSoundGroup(m_SoundGroups[i].Name, m_SoundGroups[i].AvoidBeingReplacedBySamePriority, - m_SoundGroups[i].Mute, m_SoundGroups[i].Volume, m_SoundGroups[i].AgentHelperCount)) + var group = m_SoundGroups[i]; + if (!AddSoundGroup(group.Name, group.AvoidBeingReplacedBySamePriority, group.Mute, group.Volume, group.AgentHelperCount)) { - Log.Warning("Add sound group '{0}' failure.", m_SoundGroups[i].Name); + Log.Warning("Add sound group '{0}' failure.", group.Name); continue; } } @@ -230,16 +224,14 @@ public bool AddSoundGroup(string soundGroupName, int soundAgentHelperCount) /// 声音组音量。 /// 声音代理辅助器数量。 /// 是否增加声音组成功。 - public bool AddSoundGroup(string soundGroupName, bool soundGroupAvoidBeingReplacedBySamePriority, - bool soundGroupMute, float soundGroupVolume, int soundAgentHelperCount) + public bool AddSoundGroup(string soundGroupName, bool soundGroupAvoidBeingReplacedBySamePriority, bool soundGroupMute, float soundGroupVolume, int soundAgentHelperCount) { if (m_SoundManager.HasSoundGroup(soundGroupName)) { return false; } - SoundGroupHelperBase soundGroupHelper = - Helper.CreateHelper(m_SoundGroupHelperTypeName, m_CustomSoundGroupHelper, SoundGroupCount); + SoundGroupHelperBase soundGroupHelper = Helper.CreateHelper(m_SoundGroupHelperTypeName, m_CustomSoundGroupHelper, SoundGroupCount); if (soundGroupHelper == null) { Log.Error("Can not create sound group helper."); @@ -253,8 +245,7 @@ public bool AddSoundGroup(string soundGroupName, bool soundGroupAvoidBeingReplac if (m_AudioMixer != null) { - AudioMixerGroup[] audioMixerGroups = - m_AudioMixer.FindMatchingGroups(Utility.Text.Format("Master/{0}", soundGroupName)); + AudioMixerGroup[] audioMixerGroups = m_AudioMixer.FindMatchingGroups(Utility.Text.Format("Master/{0}", soundGroupName)); if (audioMixerGroups.Length > 0) { soundGroupHelper.AudioMixerGroup = audioMixerGroups[0]; @@ -265,8 +256,7 @@ public bool AddSoundGroup(string soundGroupName, bool soundGroupAvoidBeingReplac } } - if (!m_SoundManager.AddSoundGroup(soundGroupName, soundGroupAvoidBeingReplacedBySamePriority, - soundGroupMute, soundGroupVolume, soundGroupHelper)) + if (!m_SoundManager.AddSoundGroup(soundGroupName, soundGroupAvoidBeingReplacedBySamePriority, soundGroupMute, soundGroupVolume, soundGroupHelper)) { return false; } @@ -318,7 +308,7 @@ public bool IsLoadingSound(int serialId) /// 声音的序列编号。 public UniTask PlaySound(string soundAssetName, string soundGroupName) { - return PlaySound(soundAssetName, soundGroupName, DefaultPriority, null, null, null); + return PlaySound(soundAssetName, soundGroupName, DefaultPriority, null, null, null, -1); } /// @@ -330,7 +320,19 @@ public UniTask PlaySound(string soundAssetName, string soundGroupName) /// 声音的序列编号。 public UniTask PlaySound(string soundAssetName, string soundGroupName, int priority) { - return PlaySound(soundAssetName, soundGroupName, priority, null, null, null); + return PlaySound(soundAssetName, soundGroupName, priority, null, null, null, -1); + } + + /// + /// 播放声音。 + /// + /// 声音资源名称。 + /// 声音组名称。 + /// 序列编号。 + /// 声音的序列编号。 + public UniTask PlaySoundBySerialId(string soundAssetName, string soundGroupName, int serialId) + { + return PlaySound(soundAssetName, soundGroupName, DefaultPriority, null, null, null, serialId); } /// @@ -342,7 +344,7 @@ public UniTask PlaySound(string soundAssetName, string soundGroupName, int /// 声音的序列编号。 public UniTask PlaySound(string soundAssetName, string soundGroupName, PlaySoundParams playSoundParams) { - return PlaySound(soundAssetName, soundGroupName, DefaultPriority, playSoundParams, null, null); + return PlaySound(soundAssetName, soundGroupName, DefaultPriority, playSoundParams, null, null, -1); } /// @@ -354,7 +356,7 @@ public UniTask PlaySound(string soundAssetName, string soundGroupName, Play /// 声音的序列编号。 public UniTask PlaySound(string soundAssetName, string soundGroupName, Entity.Runtime.Entity bindingEntity) { - return PlaySound(soundAssetName, soundGroupName, DefaultPriority, null, bindingEntity, null); + return PlaySound(soundAssetName, soundGroupName, DefaultPriority, null, bindingEntity, null, -1); } /// @@ -378,7 +380,7 @@ public UniTask PlaySound(string soundAssetName, string soundGroupName, Vect /// 声音的序列编号。 public UniTask PlaySound(string soundAssetName, string soundGroupName, object userData) { - return PlaySound(soundAssetName, soundGroupName, DefaultPriority, null, null, userData); + return PlaySound(soundAssetName, soundGroupName, DefaultPriority, null, null, userData, -1); } /// @@ -389,10 +391,9 @@ public UniTask PlaySound(string soundAssetName, string soundGroupName, obje /// 加载声音资源的优先级。 /// 播放声音参数。 /// 声音的序列编号。 - public UniTask PlaySound(string soundAssetName, string soundGroupName, int priority, - PlaySoundParams playSoundParams) + public UniTask PlaySound(string soundAssetName, string soundGroupName, int priority, PlaySoundParams playSoundParams) { - return PlaySound(soundAssetName, soundGroupName, priority, playSoundParams, null, null); + return PlaySound(soundAssetName, soundGroupName, priority, playSoundParams, null, null, -1); } /// @@ -404,10 +405,9 @@ public UniTask PlaySound(string soundAssetName, string soundGroupName, int /// 播放声音参数。 /// 用户自定义数据。 /// 声音的序列编号。 - public UniTask PlaySound(string soundAssetName, string soundGroupName, int priority, - PlaySoundParams playSoundParams, object userData) + public UniTask PlaySound(string soundAssetName, string soundGroupName, int priority, PlaySoundParams playSoundParams, object userData) { - return PlaySound(soundAssetName, soundGroupName, priority, playSoundParams, null, userData); + return PlaySound(soundAssetName, soundGroupName, priority, playSoundParams, null, userData, -1); } /// @@ -419,10 +419,9 @@ public UniTask PlaySound(string soundAssetName, string soundGroupName, int /// 播放声音参数。 /// 声音绑定的实体。 /// 声音的序列编号。 - public async UniTask PlaySound(string soundAssetName, string soundGroupName, int priority, - PlaySoundParams playSoundParams, Entity.Runtime.Entity bindingEntity) + public async UniTask PlaySound(string soundAssetName, string soundGroupName, int priority, PlaySoundParams playSoundParams, Entity.Runtime.Entity bindingEntity) { - return await PlaySound(soundAssetName, soundGroupName, priority, playSoundParams, bindingEntity, null); + return await PlaySound(soundAssetName, soundGroupName, priority, playSoundParams, bindingEntity, null, -1); } /// @@ -434,12 +433,11 @@ public async UniTask PlaySound(string soundAssetName, string soundGroupName /// 播放声音参数。 /// 声音绑定的实体。 /// 用户自定义数据。 + /// 序列编号 /// 声音的序列编号。 - public async UniTask PlaySound(string soundAssetName, string soundGroupName, int priority, - PlaySoundParams playSoundParams, Entity.Runtime.Entity bindingEntity, object userData) + public async UniTask PlaySound(string soundAssetName, string soundGroupName, int priority, PlaySoundParams playSoundParams, Entity.Runtime.Entity bindingEntity, object userData, int serialId) { - return await m_SoundManager.PlaySound(soundAssetName, soundGroupName, priority, playSoundParams, - PlaySoundInfo.Create(bindingEntity, Vector3.zero, userData)); + return await m_SoundManager.PlaySound(soundAssetName, soundGroupName, priority, playSoundParams, PlaySoundInfo.Create(bindingEntity, Vector3.zero, userData), serialId); } /// @@ -451,8 +449,7 @@ public async UniTask PlaySound(string soundAssetName, string soundGroupName /// 播放声音参数。 /// 声音所在的世界坐标。 /// 声音的序列编号。 - public UniTask PlaySound(string soundAssetName, string soundGroupName, int priority, - PlaySoundParams playSoundParams, Vector3 worldPosition) + public UniTask PlaySound(string soundAssetName, string soundGroupName, int priority, PlaySoundParams playSoundParams, Vector3 worldPosition) { return PlaySound(soundAssetName, soundGroupName, priority, playSoundParams, worldPosition, null); } @@ -467,11 +464,9 @@ public UniTask PlaySound(string soundAssetName, string soundGroupName, int /// 声音所在的世界坐标。 /// 用户自定义数据。 /// 声音的序列编号。 - public UniTask PlaySound(string soundAssetName, string soundGroupName, int priority, - PlaySoundParams playSoundParams, Vector3 worldPosition, object userData) + public UniTask PlaySound(string soundAssetName, string soundGroupName, int priority, PlaySoundParams playSoundParams, Vector3 worldPosition, object userData) { - return m_SoundManager.PlaySound(soundAssetName, soundGroupName, priority, playSoundParams, - PlaySoundInfo.Create(null, worldPosition, userData)); + return m_SoundManager.PlaySound(soundAssetName, soundGroupName, priority, playSoundParams, PlaySoundInfo.Create(null, worldPosition, userData), -1); } /// @@ -567,8 +562,7 @@ public void ResumeSound(int serialId, float fadeInSeconds) /// 是否增加声音代理辅助器成功。 private bool AddSoundAgentHelper(string soundGroupName, SoundGroupHelperBase soundGroupHelper, int index) { - SoundAgentHelperBase soundAgentHelper = - Helper.CreateHelper(m_SoundAgentHelperTypeName, m_CustomSoundAgentHelper, index); + SoundAgentHelperBase soundAgentHelper = Helper.CreateHelper(m_SoundAgentHelperTypeName, m_CustomSoundAgentHelper, index); if (soundAgentHelper == null) { Log.Error("Can not create sound agent helper."); @@ -582,8 +576,7 @@ private bool AddSoundAgentHelper(string soundGroupName, SoundGroupHelperBase sou if (m_AudioMixer != null) { - AudioMixerGroup[] audioMixerGroups = - m_AudioMixer.FindMatchingGroups(Utility.Text.Format("Master/{0}/{1}", soundGroupName, index)); + AudioMixerGroup[] audioMixerGroups = m_AudioMixer.FindMatchingGroups(Utility.Text.Format("Master/{0}/{1}", soundGroupName, index)); if (audioMixerGroups.Length > 0) { soundAgentHelper.AudioMixerGroup = audioMixerGroups[0]; @@ -620,10 +613,8 @@ private void OnPlaySoundSuccess(object sender, GameFrameX.Sound.PlaySoundSuccess private void OnPlaySoundFailure(object sender, GameFrameX.Sound.PlaySoundFailureEventArgs eventArgs) { - string logMessage = - Utility.Text.Format( - "Play sound failure, asset name '{0}', sound group name '{1}', error code '{2}', error message '{3}'.", - eventArgs.SoundAssetName, eventArgs.SoundGroupName, eventArgs.ErrorCode, eventArgs.ErrorMessage); + string logMessage = Utility.Text.Format("Play sound failure, asset name '{0}', sound group name '{1}', error code '{2}', error message '{3}'.", + eventArgs.SoundAssetName, eventArgs.SoundGroupName, eventArgs.ErrorCode, eventArgs.ErrorMessage); if (eventArgs.ErrorCode == PlaySoundErrorCode.IgnoredDueToLowPriority) { Log.Info(logMessage);