예제 #1
0
        private void LoadSoundSuccessCallback(string soundAssetName, object soundAsset, float duration, object userData)
        {
            PlaySoundInfo playSoundInfo = userData as PlaySoundInfo;

            if (playSoundInfo == null)
            {
                throw new GameFrameworkException("Play sound info is invalid.");
            }

            PlaySoundErrorCode?errorCode  = null;
            ISoundAgent        soundAgent = playSoundInfo.SoundGroup.PlaySound(playSoundInfo.SerialId, soundAsset, playSoundInfo.PlaySoundParams, out errorCode);

            if (soundAgent != null)
            {
                m_PlaySoundSuccessEventHandler?.Invoke(this, new PlaySoundSuccessEventArgs(playSoundInfo.SerialId, soundAssetName, soundAgent, duration, playSoundInfo.UserData));
            }
            else
            {
                m_SoundHelper.ReleaseSoundAsset(soundAsset);
                string errorMessage = string.Format("Sound group '{0}' play sound '{1}' failure.", playSoundInfo.SoundGroup.Name, soundAssetName);
                if (m_PlaySoundFailureEventHandler != null)
                {
                    m_PlaySoundFailureEventHandler(this, new PlaySoundFailureEventArgs(playSoundInfo.SerialId, soundAssetName, playSoundInfo.SoundGroup.Name, playSoundInfo.PlaySoundParams, errorCode.Value, errorMessage, playSoundInfo.UserData));
                    return;
                }

                throw new GameFrameworkException(errorMessage);
            }
        }
예제 #2
0
        /// <summary>
        /// 播放声音。
        /// </summary>
        /// <param name="soundAssetName">声音资源名称。</param>
        /// <param name="soundGroupName">声音组名称。</param>
        /// <param name="priority">加载声音资源的优先级。</param>
        /// <param name="playSoundParams">播放声音参数。</param>
        /// <param name="userData">用户自定义数据。</param>
        /// <returns>声音的序列编号。</returns>
        public int PlaySound(string soundAssetName, string soundGroupName, int priority, PlaySoundParams playSoundParams, object userData)
        {
            if (m_ResourceManager == null)
            {
                throw new GameFrameworkException("You must set resource manager first.");
            }

            if (m_SoundHelper == null)
            {
                throw new GameFrameworkException("You must set sound helper first.");
            }

            if (playSoundParams == null)
            {
                playSoundParams = PlaySoundParams.Create();
            }

            int serialId = ++m_Serial;
            PlaySoundErrorCode?errorCode    = null;
            string             errorMessage = null;
            SoundGroup         soundGroup   = (SoundGroup)GetSoundGroup(soundGroupName);

            if (soundGroup == null)
            {
                errorCode    = PlaySoundErrorCode.SoundGroupNotExist;
                errorMessage = Utility.Text.Format("Sound group '{0}' is not exist.", soundGroupName);
            }
            else if (soundGroup.SoundAgentCount <= 0)
            {
                errorCode    = PlaySoundErrorCode.SoundGroupHasNoAgent;
                errorMessage = Utility.Text.Format("Sound group '{0}' is have no sound agent.", soundGroupName);
            }

            if (errorCode.HasValue)
            {
                if (m_PlaySoundFailureEventHandler != null)
                {
                    PlaySoundFailureEventArgs playSoundFailureEventArgs = PlaySoundFailureEventArgs.Create(serialId, soundAssetName, soundGroupName, playSoundParams, errorCode.Value, errorMessage, userData);
                    m_PlaySoundFailureEventHandler(this, playSoundFailureEventArgs);
                    ReferencePool.Release(playSoundFailureEventArgs);

                    if (playSoundParams.Referenced)
                    {
                        ReferencePool.Release(playSoundParams);
                    }

                    return(serialId);
                }

                throw new GameFrameworkException(errorMessage);
            }

            m_SoundsBeingLoaded.Add(serialId);
            m_ResourceManager.LoadAsset(soundAssetName, priority, m_LoadAssetCallbacks, PlaySoundInfo.Create(serialId, soundGroup, playSoundParams, userData));
            return(serialId);
        }
예제 #3
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);
            }
        }
예제 #4
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);
        }
예제 #5
0
        private void LoadAssetSuccessCallback(string soundAssetName, object soundAsset, float duration, object userData)
        {
            PlaySoundInfo playSoundInfo = (PlaySoundInfo)userData;

            if (playSoundInfo == null)
            {
                throw new Exception("Play sound info is invalid.");
            }

            if (m_SoundsToReleaseOnLoad.Contains(playSoundInfo.SerialId))
            {
                m_SoundsToReleaseOnLoad.Remove(playSoundInfo.SerialId);
                if (playSoundInfo.PlaySoundParams.Referenced)
                {
                    ReferencePool.Release(playSoundInfo.PlaySoundParams);
                }

                ReferencePool.Release(playSoundInfo);
                m_SoundHelper.ReleaseSoundAsset(soundAsset);
                return;
            }

            m_SoundsBeingLoaded.Remove(playSoundInfo.SerialId);

            PlaySoundErrorCode?errorCode  = null;
            ISoundAgent        soundAgent = playSoundInfo.SoundGroup.PlaySound(playSoundInfo.SerialId, soundAsset, playSoundInfo.PlaySoundParams, out errorCode);

            if (soundAgent != null)
            {
                ISoundAgentHelper soundAgentHelper = soundAgent.Helper;
                soundAgentHelper.OnPlaySoundSuccess(playSoundInfo.UserData, soundAgent);

                if (playSoundInfo.PlaySoundParams.Referenced)
                {
                    ReferencePool.Release(playSoundInfo.PlaySoundParams);
                }

                ReferencePool.Release(playSoundInfo);
                return;
            }

            m_SoundsToReleaseOnLoad.Remove(playSoundInfo.SerialId);
            m_SoundHelper.ReleaseSoundAsset(soundAsset);
            string errorMessage = Utility.Text.Format("Sound group '{0}' play sound '{1}' failure.", playSoundInfo.SoundGroup.Name, soundAssetName);

            if (playSoundInfo.PlaySoundParams.Referenced)
            {
                ReferencePool.Release(playSoundInfo.PlaySoundParams);
            }

            ReferencePool.Release(playSoundInfo);
            throw new Exception(errorMessage);
        }
예제 #6
0
        /// <summary>
        /// 播放声音。
        /// </summary>
        /// <param name="soundAssetName">声音资源名称。</param>
        /// <param name="soundGroupName">声音组名称。</param>
        /// <param name="playSoundParams">播放声音参数。</param>
        /// <param name="userData">用户自定义数据。</param>
        /// <returns>声音的序列编号。</returns>
        public int PlaySound(string soundAssetName, string soundGroupName, PlaySoundParams playSoundParams, object userData)
        {
            if (m_ResourceManager == null)
            {
                throw new GameFrameworkException("You must set resource manager first.");
            }

            if (m_SoundHelper == null)
            {
                throw new GameFrameworkException("You must set sound helper first.");
            }

            if (playSoundParams == null)
            {
                playSoundParams = new PlaySoundParams();
            }

            int serialId = m_Serial++;
            PlaySoundErrorCode?errorCode    = null;
            string             errorMessage = null;
            SoundGroup         soundGroup   = (SoundGroup)GetSoundGroup(soundGroupName);

            if (soundGroup == null)
            {
                errorCode    = PlaySoundErrorCode.SoundGroupNotExist;
                errorMessage = string.Format("Sound group '{0}' is not exist.", soundGroupName);
            }
            else if (soundGroup.SoundAgentCount <= 0)
            {
                errorCode    = PlaySoundErrorCode.SoundGroupHasNoAgent;
                errorMessage = string.Format("Sound group '{0}' is have no sound agent.", soundGroupName);
            }

            if (errorCode.HasValue)
            {
                if (m_PlaySoundFailureEventHandler != null)
                {
                    m_PlaySoundFailureEventHandler(this, new PlaySoundFailureEventArgs(serialId, soundAssetName, soundGroupName, playSoundParams, errorCode.Value, errorMessage, userData));
                    return(serialId);
                }

                throw new GameFrameworkException(errorMessage);
            }

            m_SoundsBeingLoaded.Add(serialId);
            m_ResourceManager.LoadAsset(soundAssetName, m_LoadAssetCallbacks, new PlaySoundInfo(serialId, soundGroup, playSoundParams, userData));
            return(serialId);
        }
예제 #7
0
        private void LoadSoundSuccessCallback(string soundAssetName, object soundAsset, float duration, object userData)
        {
            PlaySoundInfo playSoundInfo = (PlaySoundInfo)userData;

            if (playSoundInfo == null)
            {
                throw new GameFrameworkException("Play sound info is invalid.");
            }

            if (m_SoundsToReleaseOnLoad.Contains(playSoundInfo.SerialId))
            {
                m_SoundsToReleaseOnLoad.Remove(playSoundInfo.SerialId);
                ReferencePool.Release(playSoundInfo);
                m_SoundHelper.ReleaseSoundAsset(soundAsset);
                return;
            }

            m_SoundsBeingLoaded.Remove(playSoundInfo.SerialId);

            PlaySoundErrorCode?errorCode  = null;
            ISoundAgent        soundAgent = playSoundInfo.SoundGroup.PlaySound(playSoundInfo.SerialId, soundAsset, playSoundInfo.PlaySoundParams, out errorCode);

            if (soundAgent != null)
            {
                if (m_PlaySoundSuccessEventHandler != null)
                {
                    m_PlaySoundSuccessEventHandler(this, new PlaySoundSuccessEventArgs(playSoundInfo.SerialId, soundAssetName, soundAgent, duration, playSoundInfo.UserData));
                }

                ReferencePool.Release(playSoundInfo);
                return;
            }

            m_SoundsToReleaseOnLoad.Remove(playSoundInfo.SerialId);
            m_SoundHelper.ReleaseSoundAsset(soundAsset);
            string errorMessage = Utility.Text.Format("Sound group '{0}' play sound '{1}' failure.", playSoundInfo.SoundGroup.Name, soundAssetName);

            if (m_PlaySoundFailureEventHandler != null)
            {
                m_PlaySoundFailureEventHandler(this, new PlaySoundFailureEventArgs(playSoundInfo.SerialId, soundAssetName, playSoundInfo.SoundGroup.Name, playSoundInfo.PlaySoundParams, errorCode.Value, errorMessage, playSoundInfo.UserData));
                ReferencePool.Release(playSoundInfo);
                return;
            }

            ReferencePool.Release(playSoundInfo);
            throw new GameFrameworkException(errorMessage);
        }
예제 #8
0
        private void LoadSoundSuccessCallback(string soundAssetName, UnityEngine.Object soundAsset, float duration, object userData)
        {
            SoundInfo soundInfo = (SoundInfo)userData;

            if (soundInfo == null)
            {
                throw new Exception("Play sound info is invalid.");
            }

            m_SoundsBeingLoaded.Remove(soundInfo.SerialId);
            if (m_SoundsToReleaseOnLoad.Contains(soundInfo.SerialId))
            {
                Log.Info("Release sound '{0}' on loading success.", soundInfo.SerialId.ToString());
                m_SoundsToReleaseOnLoad.Remove(soundInfo.SerialId);
                GameEntry.Resource.UnloadAsset(soundAsset);
                return;
            }

            PlaySoundErrorCode?errorCode  = null;
            SoundAgent         soundAgent = soundInfo.SoundGroup.PlaySound(soundInfo.SerialId, soundAsset, soundInfo.PlaySoundParams, out errorCode);

            if (soundAgent != null)
            {
                if (PlaySoundSuccess != null)
                {
                    PlaySoundSuccess(soundInfo.SerialId, soundAssetName, soundAgent, duration, soundInfo.UserData);
                }
            }
            else
            {
                m_SoundsToReleaseOnLoad.Remove(soundInfo.SerialId);
                GameEntry.Resource.UnloadAsset(soundAsset);
                string errorMessage = string.Format("Sound group '{0}' play sound '{1}' failure.", soundInfo.SoundGroup.Name, soundAssetName);


                if (PlaySoundFailure != null)
                {
                    PlaySoundFailure(soundInfo.SerialId, soundAssetName, soundInfo.SoundGroup.Name, soundInfo.PlaySoundParams, errorCode.Value, errorMessage, soundInfo.UserData);
                    return;
                }

                throw new Exception(errorMessage);
            }
        }
예제 #9
0
        private void LoadSoundSuccessCallback(string soundAssetName, object soundAsset, float duration, object userData)
        {
            PlaySoundInfo playSoundInfo = (PlaySoundInfo)userData;

            if (playSoundInfo == null)
            {
                throw new System.Exception("Play sound info is invalid.");
            }

            m_SoundsBeingLoaded.Remove(playSoundInfo.SerialId);
            if (m_SoundsToReleaseOnLoad.Contains(playSoundInfo.SerialId))
            {
                Log.Debug("Release sound '{0}' on loading success.", playSoundInfo.SerialId.ToString());
                m_SoundsToReleaseOnLoad.Remove(playSoundInfo.SerialId);
                m_SoundHelper.ReleaseSoundAsset(soundAsset);
                return;
            }

            PlaySoundErrorCode?errorCode  = null;
            ISoundAgent        soundAgent = playSoundInfo.SoundGroup.PlaySound(playSoundInfo.SerialId, soundAsset, playSoundInfo.PlaySoundParams, out errorCode);

            if (soundAgent != null)
            {
                if (m_PlaySoundSuccessEventHandler != null)
                {
                    m_PlaySoundSuccessEventHandler(this, new PlaySoundSuccessEventArgs(playSoundInfo.SerialId, soundAssetName, soundAgent, duration, playSoundInfo.UserData));
                }
            }
            else
            {
                m_SoundsToReleaseOnLoad.Remove(playSoundInfo.SerialId);
                m_SoundHelper.ReleaseSoundAsset(soundAsset);
                string errorMessage = string.Format("Sound group '{0}' play sound '{1}' failure.", playSoundInfo.SoundGroup.Name, soundAssetName);
                if (m_PlaySoundFailureEventHandler != null)
                {
                    m_PlaySoundFailureEventHandler(this, new PlaySoundFailureEventArgs(playSoundInfo.SerialId, soundAssetName, playSoundInfo.SoundGroup.Name, playSoundInfo.PlaySoundParams, errorCode.Value, errorMessage, playSoundInfo.UserData));
                    return;
                }

                throw new System.Exception(errorMessage);
            }
        }
예제 #10
0
        /// <summary>
        /// 播放声音
        /// </summary>
        /// <param name="soundAssetName">声音资源名称</param>
        /// <param name="soundGroupName">声音组名称</param>
        /// <param name="priority">加载声音资源的优先级</param>
        /// <param name="playSoundParams">播放声音参数</param>
        /// <param name="userData">用户自定义数据</param>
        /// <returns>声音的序列编号</returns>
        public int PlaySound(string soundAssetName, string soundGroupName, int priority, PlaySoundParams playSoundParams, object userData)
        {
            if (playSoundParams == null)
            {
                playSoundParams = new PlaySoundParams();
            }

            int serialId = m_Serial++;
            PlaySoundErrorCode?errorCode    = null;
            string             errorMessage = null;
            SoundGroup         soundGroup   = GetSoundGroup(soundGroupName);

            if (soundGroup == null)
            {
                errorCode    = PlaySoundErrorCode.SoundGroupNotExist;
                errorMessage = string.Format("Sound group '{0}' is not exist.", soundGroupName);
            }
            else if (soundGroup.SoundAgentCount <= 0)
            {
                errorCode    = PlaySoundErrorCode.SoundGroupHasNoAgent;
                errorMessage = string.Format("Sound group '{0}' is have no sound agent.", soundGroupName);
            }

            if (errorCode.HasValue)
            {
                if (PlaySoundFailure != null)
                {
                    PlaySoundFailure(serialId, soundAssetName, soundGroupName, playSoundParams, errorCode.Value, errorMessage, userData);
                    return(serialId);
                }

                throw new Exception(errorMessage);
            }

            m_SoundsBeingLoaded.Add(serialId);
            GameEntry.Resource.LoadAsset(soundAssetName, typeof(AudioClip), priority, m_LoadAssetCallbacks, new SoundInfo(serialId, soundGroup, playSoundParams, userData));
            return(serialId);
        }
예제 #11
0
파일: SoundGroup.cs 프로젝트: 542767059/ABC
        /// <summary>
        /// 播放声音
        /// </summary>
        /// <param name="serialId">声音的序列编号</param>
        /// <param name="soundAsset">声音资源</param>
        /// <param name="playSoundParams">播放声音参数</param>
        /// <param name="errorCode">错误码</param>
        /// <returns>用于播放的声音代理</returns>
        public SoundAgent PlaySound(int serialId, UnityEngine.Object soundAsset, PlaySoundParams playSoundParams, out PlaySoundErrorCode?errorCode)
        {
            errorCode = null;
            SoundAgent candidateAgent = null;

            foreach (SoundAgent soundAgent in m_SoundAgents)
            {
                if (!soundAgent.IsPlaying)
                {
                    candidateAgent = soundAgent;
                    break;
                }

                if (soundAgent.Priority < playSoundParams.Priority)
                {
                    if (candidateAgent == null || soundAgent.Priority < candidateAgent.Priority)
                    {
                        candidateAgent = soundAgent;
                    }
                }
                else if (!m_AvoidBeingReplacedBySamePriority && soundAgent.Priority == playSoundParams.Priority)
                {
                    if (candidateAgent == null || soundAgent.SetSoundAssetTime < candidateAgent.SetSoundAssetTime)
                    {
                        candidateAgent = soundAgent;
                    }
                }
            }

            if (candidateAgent == null)
            {
                errorCode = PlaySoundErrorCode.IgnoredDueToLowPriority;
                return(null);
            }

            if (!candidateAgent.SetSoundAsset(soundAsset))
            {
                errorCode = PlaySoundErrorCode.SetSoundAssetFailure;
                return(null);
            }

            candidateAgent.SerialId           = serialId;
            candidateAgent.Time               = playSoundParams.Time;
            candidateAgent.MuteInSoundGroup   = playSoundParams.MuteInSoundGroup;
            candidateAgent.Loop               = playSoundParams.Loop;
            candidateAgent.Priority           = playSoundParams.Priority;
            candidateAgent.VolumeInSoundGroup = playSoundParams.VolumeInSoundGroup;
            candidateAgent.Pitch              = playSoundParams.Pitch;
            candidateAgent.PanStereo          = playSoundParams.PanStereo;
            candidateAgent.SpatialBlend       = playSoundParams.SpatialBlend;
            candidateAgent.MaxDistance        = playSoundParams.MaxDistance;
            candidateAgent.DopplerLevel       = playSoundParams.DopplerLevel;
            candidateAgent.Play(playSoundParams.FadeInSeconds);
            return(candidateAgent);
        }
예제 #12
0
        /// <summary>
        /// 播放声音
        /// </summary>
        /// <param name="soundAsset">声音资源</param>
        /// <param name="soundGroupName">声音组名称。</param>
        /// <param name="priority">加载声音资源的优先级。</param>
        /// <param name="playSoundParams">播放声音参数。</param>
        /// <param name="userData">用户自定义数据。</param>
        /// <returns>声音的序列编号。</returns>
        /// <exception cref="GameFrameworkException"></exception>
        public int PlaySound(object soundAsset, string soundGroupName, int priority, PlaySoundParams playSoundParams,
                             object userData)
        {
            if (m_ResourceManager == null)
            {
                throw new GameFrameworkException("You must set resource manager first.");
            }

            if (m_SoundHelper == null)
            {
                throw new GameFrameworkException("You must set sound helper first.");
            }

            if (playSoundParams == null)
            {
                playSoundParams = new PlaySoundParams();
            }

            int serialId = m_Serial++;
            PlaySoundErrorCode?errorCode    = null;
            string             errorMessage = null;
            SoundGroup         soundGroup   = (SoundGroup)GetSoundGroup(soundGroupName);

            if (soundGroup == null)
            {
                errorCode    = PlaySoundErrorCode.SoundGroupNotExist;
                errorMessage = string.Format("Sound group '{0}' is not exist.", soundGroupName);
            }
            else if (soundGroup.SoundAgentCount <= 0)
            {
                errorCode    = PlaySoundErrorCode.SoundGroupHasNoAgent;
                errorMessage = string.Format("Sound group '{0}' is have no sound agent.", soundGroupName);
            }

            if (errorCode.HasValue)
            {
                if (m_PlaySoundFailureEventHandler != null)
                {
                    m_PlaySoundFailureEventHandler(this, new PlaySoundFailureEventArgs(serialId, soundAsset.ToString(), soundGroupName, playSoundParams, errorCode.Value, errorMessage, userData));
                    return(serialId);
                }

                throw new GameFrameworkException(errorMessage);
            }

            m_SoundsBeingLoaded.Add(serialId);

            var playSoundInfo = new PlaySoundInfo(serialId, soundGroup, playSoundParams, userData);
            var soundAgent    = playSoundInfo.SoundGroup.PlaySound(serialId, soundAsset, playSoundParams, out errorCode);

            if (soundAgent != null)
            {
                if (m_PlaySoundSuccessEventHandler != null)
                {
                    m_PlaySoundSuccessEventHandler(this, new PlaySoundSuccessEventArgs(serialId, soundAsset.ToString(), soundAgent, 0f, userData));
                }
            }
            else
            {
                m_SoundsToReleaseOnLoad.Remove(serialId);
                m_SoundHelper.ReleaseSoundAsset(soundAsset);
                errorMessage = string.Format("Sound group '{0}' play sound asset '{1}' failure.", soundGroupName, soundAsset);
                if (m_PlaySoundFailureEventHandler != null)
                {
                    m_PlaySoundFailureEventHandler(this, new PlaySoundFailureEventArgs(serialId, soundAsset.ToString(), soundGroupName, playSoundParams, errorCode.Value, errorMessage, userData));
                }
                else
                {
                    throw new GameFrameworkException(errorMessage);
                }
            }

            return(serialId);
        }
예제 #13
0
        /// <summary>
        /// 播放声音。
        /// </summary>
        /// <param name="soundAssetName">声音资源名称。</param>
        /// <param name="soundGroupName">声音组名称。</param>
        /// <param name="priority">加载声音资源的优先级。</param>
        /// <param name="playSoundParams">播放声音参数。</param>
        /// <param name="userData">用户自定义数据。</param>
        /// <returns>声音的序列编号。</returns>
        public async CFTask <int> PlaySound(string soundAssetName, string soundGroupName, int priority, PlaySoundParams playSoundParams, object userData)
        {
            if (m_ResourceManager == null)
            {
                throw new Exception("You must set resource manager first.");
            }

            if (m_SoundHelper == null)
            {
                throw new Exception("You must set sound helper first.");
            }

            if (playSoundParams == null)
            {
                playSoundParams = PlaySoundParams.Create();
            }

            int serialId = ++m_Serial;
            PlaySoundErrorCode?errorCode    = null;
            string             errorMessage = null;
            SoundGroup         soundGroup   = (SoundGroup)GetSoundGroup(soundGroupName);

            if (soundGroup == null)
            {
                errorCode    = PlaySoundErrorCode.SoundGroupNotExist;
                errorMessage = Utility.Text.Format("Sound group '{0}' is not exist.", soundGroupName);
            }
            else if (soundGroup.SoundAgentCount <= 0)
            {
                errorCode    = PlaySoundErrorCode.SoundGroupHasNoAgent;
                errorMessage = Utility.Text.Format("Sound group '{0}' is have no sound agent.", soundGroupName);
            }

            if (errorCode.HasValue)
            {
                if (playSoundParams.Referenced)
                {
                    ReferencePool.Release(playSoundParams);
                }

                Log.Info(errorMessage);

                return(serialId);

                throw new Exception(errorMessage);
            }

            m_SoundsBeingLoaded.Add(serialId);

            LoadAssetCallbacks loadAsset = await m_ResourceManager.LoadAsset(soundAssetName, AssetCategory.Audio, m_LoadAssetCallbacks, priority, PlaySoundInfo.Create(serialId, soundGroup, playSoundParams, userData));

            if (loadAsset.LoadAssetStatus == LoadAssetStatus.Success)
            {
                //loadAsset.InvokeCallbackSuccess();
                return(serialId);
            }
            else
            {
                //loadAsset.InvokeCallbackFailed();
                return(0);
            }
        }