/// <summary>
            /// 播放声音。
            /// </summary>
            /// <param name="serialId">声音的序列编号。</param>
            /// <param name="soundAsset">声音资源。</param>
            /// <param name="playSoundParams">播放声音参数。</param>
            /// <param name="errorCode">错误码。</param>
            /// <returns>用于播放的声音代理。</returns>
            public ISoundAgent PlaySound(int serialId, 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);
            }
Exemple #2
0
    /// <summary>
    /// 增加声音代理辅助器。
    /// </summary>
    /// <param name="soundHelper">声音辅助器接口。</param>
    /// <param name="soundAgentHelper">要增加的声音代理辅助器。</param>
    public void AddSoundAgentHelper(Transform parent)
    {
        GameObject gameObject = new GameObject(m_Name + "[SoundAgent]" + m_SoundAgents.Count);
        SoundAgent soundAgent = gameObject.AddComponent <SoundAgent>();

        if (parent != null)
        {
            gameObject.transform.parent = parent;
        }
        soundAgent.Init(this);
        m_SoundAgents.Add(soundAgent);
    }
    void Awake()
    {
        if( mInstance != null )
        {
            Debug.LogError( string.Format( "Only one instance of SoundAgent allowed! Destroying:" + gameObject.name +", Other:" + mInstance.gameObject.name ) );
            Destroy( gameObject );
            return;
        }

        mInstance = this;
    }