コード例 #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);
            }
        }
コード例 #2
0
        public SoundAgent(SoundGroup soundGroup, SoundHelperBase soundHelper, SoundAgentHelperBase soundAgentHelper)
        {
            if (soundGroup == null)
            {
                Debug.LogError("用来构造声音代理对象的声音组为空");
            }

            if (soundHelper == null)
            {
                Debug.LogError("用来构造声音代理对象的声音辅助器为空");
            }

            if (soundAgentHelper == null)
            {
                Debug.LogError("用来构造声音代理对象的声音代理辅助器为空");
            }

            m_SoundGroup  = soundGroup;
            m_SoundHelper = soundHelper;
            Helper        = soundAgentHelper;
            Helper.resetSoundAgentEventHandler += OnResetSoundAgent;
            SerialId = 0;
            Reset();
        }
コード例 #3
0
 /// <summary>
 /// 增加声音代理辅助器
 /// </summary>
 public void AddSoundAgentHelper(SoundHelperBase soundHelper, SoundAgentHelperBase soundAgentHelper)
 {
     m_SoundAgents.Add(new SoundAgent(this, soundHelper, soundAgentHelper));
 }