Exemple #1
0
 void OnPlaySoundEffect(SoundClip soundClip, SoundClipPools.SoundClipParam param)
 {
     if (soundClip == null)
     {
         LogModule.ErrorLog("soundClip is null");
         return;
     }
     PlaySoundEffect(soundClip, param.m_volumeFactor);
 }
Exemple #2
0
    private void OnPlaySoundEffect(SoundClip soundClip, SoundClipPools.SoundClipParam param, bool reset)
    {
        if (soundClip == null)
        {
            Logger.Error("soundClip is null");
            return;
        }

        PlaySoundEffect(soundClip, param, reset);
    }
Exemple #3
0
    /// <summary>
    ///     播放soundClip中的音效
    /// </summary>
    /// <param name="soundClip"></param>
    /// <param name="volume"></param>
    /// <returns></returns>
    private void PlaySoundEffect(SoundClip soundClip, SoundClipPools.SoundClipParam param, bool reset = false)
    {
        if (mVoicePlaying)
        {
            return;
        }

        if (mEnableSFX && !string.IsNullOrEmpty(soundClip.mPath))
        {
            if (soundClip.Audioclip == null)
            {
                Logger.Error("PlaySoundEffect soundClip.Audioclip is null");
                return;
            }

            var leastImportantIndex      = 0;
            var nCurMaxPlayingCount      = 0;  //最大播放次数
            var nFirstEmptyIndex         = -1; //第一个空位
            var nFirstSameClipValidIndex = -1; //第一个已经停止的上次播放过的位置

            var firstUse = -1;
            for (var nIndex = 0; nIndex < mSFXChannelsCount; ++nIndex) //先找已经在播放或者播放过的
            {
                if (mSFXChannel[nIndex] == null)
                {
                    return; //error
                }

                if (nFirstEmptyIndex == -1 && !mSFXChannel[nIndex].m_AudioSource.isPlaying) //记录第一个可用的空位置
                {
                    nFirstEmptyIndex = nIndex;
                }

                if (mSFXChannel[nIndex].m_AudioSource.clip == null)
                {
                    continue;
                }

                if (mSFXChannel[nIndex].m_uID == soundClip.mUID) //有播放过的内容
                {
                    if (nFirstSameClipValidIndex == -1 && !mSFXChannel[nIndex].m_AudioSource.isPlaying)
                    //记录第一个已经停止的上次播放过的位置
                    {
                        nFirstSameClipValidIndex = nIndex;
                    }


                    if (mSFXChannel[nIndex].m_AudioSource.isPlaying)
                    {
                        ++nCurMaxPlayingCount; //正在播放的计数
                        if (firstUse == -1)
                        {
                            firstUse = nIndex;
                        }
                    }
                    if (nCurMaxPlayingCount >= soundClip.mCurMaxPlayingCount) //已经超过了,不播放了
                    {
                        break;
                    }
                }

                if (mSFXChannel[leastImportantIndex].m_AudioSource.priority < mSFXChannel[nIndex].m_AudioSource.priority)
                //记录最低优先级
                {
                    leastImportantIndex = nIndex;
                }
            }

            if (nCurMaxPlayingCount < soundClip.mCurMaxPlayingCount) //没到播放数量限制,直接播放
            {
                var nValidIndex = -1;                                //先选择已经停止播放的,如果没有,选第一个空的,如果也没有空的,替换优先数字最高的
                if (nFirstSameClipValidIndex != -1)
                {
                    nValidIndex = nFirstSameClipValidIndex;
                }
                else
                {
                    if (nFirstEmptyIndex != -1)
                    {
                        nValidIndex = nFirstEmptyIndex;
                    }
                    else
                    {
                        nValidIndex = leastImportantIndex;
                    }
                }

                if (nValidIndex >= 0 && nValidIndex < mSFXChannelsCount)
                {
                    var channel = mSFXChannel[nValidIndex];
                    channel.m_AudioSource.Stop();
                    if (channel.m_tag != 0)
                    {
                        mTaggedAudio.Remove(channel.m_tag);
                    }
                    SetAudioSource(channel, soundClip, param.m_volumeFactor);
                    if (param.m_tag != 0)
                    {
                        channel.m_tag = param.m_tag;
                        mTaggedAudio.Add(channel.m_tag, channel);
                    }
                    channel.m_AudioSource.PlayDelayed(soundClip.mDelay);
                }
            }
            else
            {
                if (reset)
                {
                    if (firstUse != -1)
                    {
                        mSFXChannel[firstUse].m_AudioSource.PlayDelayed(soundClip.mDelay);
                    }
                }


                //达到播放上限,不播放
                //Logger.Info("Warning PlaySoundEffect " + soundClip.m_name + " PlayingCount = " + nCurMaxPlayingCount);
            }
        }
    }
Exemple #4
0
    private void OnPlayBGMWithFade(SoundClip bgSoundClip, SoundClipPools.SoundClipParam param, bool reset)
    {
        if (mBGAudioSource != null && bgSoundClip != null)
        {
            mLastMusicID = param.m_clipID;
            if (mBGAudioSource.m_AudioSource.isPlaying) //正常播放上一首背景音乐
            {
                if (mNextSoundClip != null && mNextSoundClip.mUID == bgSoundClip.mUID)
                {
                    return;
                }
                mFadeOutTime  = param.m_fadeOutTime;
                mFadeInTime   = param.m_fadeInTime;
                mFadeOutTimer = 0;
                mFadeInTimer  = 0;
                if (mFadeOutTime <= 0)
                {
                    //force Remove clip from pool, special for bgmusic
                    var deluid = mBGAudioSource.m_uID;
                    SetAudioSource(mBGAudioSource, bgSoundClip, 1.0f);
                    mSoundClipPools.ForceRemoveClipByID(deluid);

                    mCurBGVolume = bgSoundClip.mVolume;
                    if (mFadeInTime <= 0)
                    {
                        mBGAudioSource.m_AudioSource.Play();
                        mFadeMode = FadeMode.None;
                    }
                    else
                    {
                        mBGAudioSource.m_AudioSource.volume = 0;
                        mBGAudioSource.m_AudioSource.Play();
                        mFadeMode = FadeMode.FadeIn;
                    }
                }
                else
                {
                    mNextSoundClip = bgSoundClip;
                    mFadeMode      = FadeMode.FadeOut;
                }
            }
            else //没在播放,直接播放
            {
                mFadeInTime  = param.m_fadeInTime;
                mFadeInTimer = 0;

                //force Remove clip from pool, special for bgmusic
                var deluid = mBGAudioSource.m_uID;
                SetAudioSource(mBGAudioSource, bgSoundClip, 1.0f);
                mSoundClipPools.ForceRemoveClipByID(deluid);

                mCurBGVolume = bgSoundClip.mVolume;
                if (mFadeInTime <= 0)
                {
                    mBGAudioSource.m_AudioSource.Play();
                    mFadeMode = FadeMode.None;
                }
                else
                {
                    mBGAudioSource.m_AudioSource.volume = 0;
                    mBGAudioSource.m_AudioSource.Play();
                    mFadeMode = FadeMode.FadeIn;
                }
            }
        }
    }
Exemple #5
0
    void OnPlayBGMWithFade(SoundClip bgSoundClip, SoundClipPools.SoundClipParam param)
    {
        if (m_BGAudioSource != null && bgSoundClip != null)
        {
            m_lastMusicID = param.m_clipID;
            if (m_BGAudioSource.m_AudioSource.isPlaying) //Õý³£²¥·ÅÉÏÒ»Ê×±³¾°ÒôÀÖ
            {
                if (m_NextSoundClip != null && m_NextSoundClip.m_uID == bgSoundClip.m_uID)
                {
                    return;
                }
                m_fadeOutTime  = param.m_fadeOutTime;
                m_fadeInTime   = param.m_fadeInTime;
                m_fadeOutTimer = 0;
                m_fadeInTimer  = 0;
                if (m_fadeOutTime <= 0)
                {
                    //add by sunyu 2014-07-31
                    //force Remove clip from pool, special for bgmusic
                    short deluid = m_BGAudioSource.m_uID;
                    SetAudioSource(ref m_BGAudioSource, bgSoundClip, 1.0f);
#if UNITY_WP8
                    if (m_BGAudioSource.m_uID != deluid)
                    {
                        m_SoundClipPools.ForceRemoveClipByID(deluid);
                    }
#else
                    m_SoundClipPools.ForceRemoveClipByID(deluid);
#endif

                    m_CurBGVolume = bgSoundClip.m_volume;
                    if (m_fadeInTime <= 0)
                    {
                        m_BGAudioSource.m_AudioSource.Play();
                        m_fadeMode = FadeMode.None;
                    }
                    else
                    {
                        m_BGAudioSource.m_AudioSource.volume = 0;
                        m_BGAudioSource.m_AudioSource.Play();
                        m_fadeMode = FadeMode.FadeIn;
                    }
                }
                else
                {
                    m_NextSoundClip = bgSoundClip;
                    m_fadeMode      = FadeMode.FadeOut;
                }
            }
            else //ûÔÚ²¥·Å£¬Ö±½Ó²¥·Å
            {
                m_fadeInTime  = param.m_fadeInTime;
                m_fadeInTimer = 0;

                //add by sunyu 2014-07-31
                //force Remove clip from pool, special for bgmusic
                short deluid = m_BGAudioSource.m_uID;
                SetAudioSource(ref m_BGAudioSource, bgSoundClip, 1.0f);
#if UNITY_WP8
                if (m_BGAudioSource.m_uID != deluid)
                {
                    m_SoundClipPools.ForceRemoveClipByID(deluid);
                }
#else
                m_SoundClipPools.ForceRemoveClipByID(deluid);
#endif

                m_CurBGVolume = bgSoundClip.m_volume;
                if (m_fadeInTime <= 0)
                {
                    m_BGAudioSource.m_AudioSource.Play();
                    m_fadeMode = FadeMode.None;
                }
                else
                {
                    m_BGAudioSource.m_AudioSource.volume = 0;
                    m_BGAudioSource.m_AudioSource.Play();
                    m_fadeMode = FadeMode.FadeIn;
                }
            }
        }
    }