Exemplo n.º 1
0
        /// <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);
        }
Exemplo n.º 2
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);
            }
        }
Exemplo n.º 3
0
        private void OnPlaySoundSuccess(int serialId, string soundAssetName, SoundAgent soundAgent, float duration, object userData)
        {
            PlaySoundInfo playSoundInfo = (PlaySoundInfo)userData;

            if (playSoundInfo != null)
            {
                SoundAgentHelper soundAgentHelper = soundAgent.Helper;
                if (playSoundInfo.BindingEntity != null)
                {
                    soundAgentHelper.SetBindingEntity(playSoundInfo.BindingEntity);
                }
                else
                {
                    soundAgentHelper.SetWorldPosition(playSoundInfo.WorldPosition);
                }
            }

            if (m_EnablePlaySoundSuccessEvent)
            {
                Debug.Log("声音播放成功事件");
            }
        }