private void LoadSoundSuccessCallback(string soundAssetName, object soundAsset, float duration, object userData) { LoadSoundInfo loadSoundInfo = (LoadSoundInfo)userData; if (loadSoundInfo == null) { Debug.LogError("加载声音的信息为空"); return; } m_SoundsBeingLoaded.Remove(loadSoundInfo.SerialId); //释放需要在加载后立即释放的声音 if (m_SoundsToReleaseOnLoad.Contains(loadSoundInfo.SerialId)) { Debug.Log("在加载成功后释放了声音:" + loadSoundInfo.SerialId.ToString()); m_SoundsToReleaseOnLoad.Remove(loadSoundInfo.SerialId); m_SoundHelper.ReleaseSoundAsset(soundAsset); return; } //播放声音 PlaySoundErrorCode?errorCode = null; SoundAgent soundAgent = loadSoundInfo.SoundGroup.PlaySound(loadSoundInfo.SerialId, soundAsset, loadSoundInfo.PlaySoundParams, out errorCode); if (soundAgent != null) { //获取到声音代理辅助器,并设置绑定的实体或位置 PlaySoundInfo playSoundInfo = (PlaySoundInfo)loadSoundInfo.UserData; SoundAgentHelperBase soundAgentHelper = soundAgent.Helper; if (playSoundInfo.BindingEntity != null) { soundAgentHelper.SetBindingEntity(playSoundInfo.BindingEntity); } else { soundAgentHelper.SetWorldPosition(playSoundInfo.WorldPosition); } //派发播放声音成功事件 PlaySoundSuccessEventArgs se = ReferencePool.Acquire <PlaySoundSuccessEventArgs>(); m_EventManager.Fire(this, se.Fill(loadSoundInfo.UserData, loadSoundInfo.SerialId, soundAssetName, soundAgent, duration)); } else { m_SoundsToReleaseOnLoad.Remove(loadSoundInfo.SerialId); m_SoundHelper.ReleaseSoundAsset(soundAsset); string errorMessage = string.Format("声音组: {0} 播放声音 '{1}' 失败.", loadSoundInfo.SoundGroup.Name, soundAssetName); //派发播放声音失败事件 PlaySoundFailureEventArgs fe = ReferencePool.Acquire <PlaySoundFailureEventArgs>(); m_EventManager.Fire(this, fe.Fill(loadSoundInfo.UserData, loadSoundInfo.SerialId, soundAssetName, loadSoundInfo.SoundGroup.Name, loadSoundInfo.PlaySoundParams, errorCode.Value, errorMessage)); Debug.LogError("播放声音失败:" + errorMessage); } }
/// <summary> /// 播放声音 /// </summary> /// <param name="soundAssetName">声音资源名称</param> /// <param name="soundGroupName">声音组名称</param> /// <param name="playSoundParams">播放声音参数</param> /// <param name="bindingEntity">声音绑定的实体</param> /// <param name="worldPosition">声音所在的世界坐标</param> /// <param name="userData">用户自定义数据</param> /// <returns>声音的序列编号</returns> public int PlaySound(string soundAssetName, string soundGroupName, PlaySoundParams playSoundParams = null, Entity.Entity bindingEntity = null, Vector3 worldPosition = default(Vector3), object userData = null) { if (m_ResourceManager == null) { Debug.LogError("资源管理器为空,无法播放声音"); return(-1); } if (m_SoundHelper == null) { Debug.LogError("声音辅助器为空,无法播放声音"); return(-1); } if (playSoundParams == null) { playSoundParams = new PlaySoundParams(); } PlaySoundInfo playSoundInfo = new PlaySoundInfo(bindingEntity, worldPosition, userData); int serialId = m_Serial++; PlaySoundErrorCode?errorCode = null; string errorMessage = null; //获取声音组 SoundGroup soundGroup = GetSoundGroup(soundGroupName); if (soundGroup == null) { errorCode = PlaySoundErrorCode.SoundGroupNotExist; errorMessage = string.Format("声音组:{0} 不存在.", soundGroupName); } else if (soundGroup.SoundAgentCount <= 0) { errorCode = PlaySoundErrorCode.SoundGroupHasNoAgent; errorMessage = string.Format("声音组: {0} 没有声音代理", soundGroupName); } //错误码有值时 if (errorCode.HasValue) { //派发播放声音失败事件 PlaySoundFailureEventArgs e = ReferencePool.Acquire <PlaySoundFailureEventArgs>(); m_EventManager.Fire(this, e.Fill(playSoundInfo, serialId, soundAssetName, soundGroupName, playSoundParams, errorCode.Value, errorMessage)); Debug.LogError("播放声音失败:" + errorMessage); return(serialId); } //加载声音 m_SoundsBeingLoaded.Add(serialId); m_ResourceManager.LoadAsset(soundAssetName, m_LoadAssetCallbacks, new LoadSoundInfo(serialId, soundGroup, playSoundParams, playSoundInfo)); return(serialId); }
private void LoadSoundFailureCallback(string soundAssetName, LoadResourceStatus status, string errorMessage, object userData) { LoadSoundInfo loadSoundInfo = (LoadSoundInfo)userData; if (loadSoundInfo == null) { Debug.LogError("加载声音的信息为空"); return; } m_SoundsBeingLoaded.Remove(loadSoundInfo.SerialId); m_SoundsToReleaseOnLoad.Remove(loadSoundInfo.SerialId); string appendErrorMessage = string.Format("加载声音失败,资源名: {0}, 状态: {1}, 错误信息:{2}", soundAssetName, status.ToString(), errorMessage); //派发播放声音失败事件 PlaySoundFailureEventArgs e = ReferencePool.Acquire <PlaySoundFailureEventArgs>(); m_EventManager.Fire(this, e.Fill(loadSoundInfo.UserData, loadSoundInfo.SerialId, soundAssetName, loadSoundInfo.SoundGroup.Name, loadSoundInfo.PlaySoundParams, PlaySoundErrorCode.LoadAssetFailure, appendErrorMessage)); }