Skip to content

Commit

Permalink
[增加]1. 增加自定义序列编号的函数
Browse files Browse the repository at this point in the history
  • Loading branch information
AlianBlank committed Nov 8, 2024
1 parent ec65b6f commit 123a5ef
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 61 deletions.
3 changes: 2 additions & 1 deletion Runtime/Sound/Sound/ISoundManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,9 @@ public interface ISoundManager
/// <param name="priority">加载声音资源的优先级。</param>
/// <param name="playSoundParams">播放声音参数。</param>
/// <param name="userData">用户自定义数据。</param>
/// <param name="serialId">序列编号</param>
/// <returns>声音的序列编号。</returns>
UniTask<int> PlaySound(string soundAssetName, string soundGroupName, int priority, PlaySoundParams playSoundParams, object userData);
UniTask<int> PlaySound(string soundAssetName, string soundGroupName, int priority, PlaySoundParams playSoundParams, object userData, int serialId);

/// <summary>
/// 停止播放声音。
Expand Down
37 changes: 30 additions & 7 deletions Runtime/Sound/Sound/SoundManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,18 @@ public UniTask<int> PlaySound(string soundAssetName, string soundGroupName, int
return PlaySound(soundAssetName, soundGroupName, priority, playSoundParams, null);
}

/// <summary>
/// 播放声音。并设置指定的序列编号
/// </summary>
/// <param name="soundAssetName">声音资源名称。</param>
/// <param name="soundGroupName">声音组名称。</param>
/// <param name="serialId">加载声音资源的优先级。</param>
/// <returns>声音的序列编号。</returns>
public UniTask<int> PlaySoundBySerialId(string soundAssetName, string soundGroupName, int serialId)
{
return PlaySound(soundAssetName, soundGroupName, Constant.DefaultPriority, null, null, serialId);
}

/// <summary>
/// 播放声音。
/// </summary>
Expand Down Expand Up @@ -401,8 +413,9 @@ public async UniTask<int> PlaySound(string soundAssetName, string soundGroupName
/// <param name="priority">加载声音资源的优先级。</param>
/// <param name="playSoundParams">播放声音参数。</param>
/// <param name="userData">用户自定义数据。</param>
/// <param name="serialId">序列编号</param>
/// <returns>声音的序列编号。</returns>
public async UniTask<int> PlaySound(string soundAssetName, string soundGroupName, int priority, PlaySoundParams playSoundParams, object userData)
public async UniTask<int> PlaySound(string soundAssetName, string soundGroupName, int priority, PlaySoundParams playSoundParams, object userData, int serialId = -1)
{
if (_assetManager == null)
{
Expand All @@ -419,7 +432,17 @@ public async UniTask<int> 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);
Expand All @@ -439,7 +462,7 @@ public async UniTask<int> 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);

Expand All @@ -448,23 +471,23 @@ public async UniTask<int> 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<AudioClip>(soundAssetName);

void OnAssetOperationHandleOnCompleted(AssetHandle assetHandle)
{
var assetObject = assetHandle.GetAssetObject<AudioClip>();
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;
}

/// <summary>
Expand Down
Loading

0 comments on commit 123a5ef

Please sign in to comment.