Exemplo n.º 1
0
        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);
            }
        }
Exemplo n.º 2
0
        /// <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);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 填充播放声音成功事件
        /// </summary>
        /// <returns>播放声音成功事件</returns>
        public PlaySoundSuccessEventArgs Fill(object userData, int serialId, string soundAssetName, SoundAgent soundAgent, float duration)
        {
            PlaySoundInfo playSoundInfo = (PlaySoundInfo)userData;

            SerialId       = serialId;
            SoundAssetName = soundAssetName;
            BindingEntity  = playSoundInfo.BindingEntity;
            UserData       = playSoundInfo.UserData;

            SoundAgent = soundAgent;
            Duration   = duration;

            return(this);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 填充播放声音更新事件
        /// </summary>
        /// <returns>播放声音更新事件。</returns>
        public PlaySoundUpdateEventArgs Fill(object userData, int serialId, string soundAssetName, string soundGroupName, PlaySoundParams playSoundParams, float progress)
        {
            PlaySoundInfo playSoundInfo = (PlaySoundInfo)userData;

            SerialId        = serialId;
            SoundAssetName  = soundAssetName;
            SoundGroupName  = soundGroupName;
            PlaySoundParams = playSoundParams;
            BindingEntity   = playSoundInfo.BindingEntity;
            UserData        = playSoundInfo.UserData;

            Progress = progress;

            return(this);
        }
        /// <summary>
        /// 填充播放声音失败事件
        /// </summary>
        /// <returns>播放声音失败事件</returns>
        public PlaySoundFailureEventArgs Fill(object userData, int serialId, string soundAssetName, string soundGroupName, PlaySoundParams playSoundParams, PlaySoundErrorCode errorCode, string errorMessage)
        {
            PlaySoundInfo playSoundInfo = (PlaySoundInfo)userData;

            SerialId        = serialId;
            SoundAssetName  = soundAssetName;
            SoundGroupName  = soundGroupName;
            PlaySoundParams = playSoundParams;
            BindingEntity   = playSoundInfo.BindingEntity;
            UserData        = playSoundInfo.UserData;

            ErrorCode    = errorCode;
            ErrorMessage = errorMessage;

            return(this);
        }
        /// <summary>
        /// 填充播放声音时加载依赖资源事件
        /// </summary>
        /// <returns>播放声音时加载依赖资源事件</returns>
        public PlaySoundDependencyAssetEventArgs Fill(object userData, int serialId, string soundAssetName, string soundGroupName, PlaySoundParams playSoundParams, string dependencyAssetName, int loadedCount, int totalCount)
        {
            PlaySoundInfo playSoundInfo = (PlaySoundInfo)userData;

            SerialId        = serialId;
            SoundAssetName  = soundAssetName;
            SoundGroupName  = soundGroupName;
            PlaySoundParams = playSoundParams;
            BindingEntity   = playSoundInfo.BindingEntity;
            UserData        = playSoundInfo.UserData;

            DependencyAssetName = dependencyAssetName;
            LoadedCount         = loadedCount;
            TotalCount          = totalCount;

            return(this);
        }