Example #1
0
 private void UpdateSoundVolumeChange(CSoundEntry soundEntry, float finalVolume)
 {
     if (soundEntry.IsChanging && soundEntry.Item.audioSource != null)
     {
         //播放时,更改背景音乐的过度
         if (soundEntry.Item.audioSource.volume > BGMFinalVolume)
         {
             soundEntry.Item.audioSource.volume -= Time.deltaTime / BGMChangeVolumeTime * finalVolume;
             if (soundEntry.Item.audioSource.volume <= finalVolume)
             {
                 soundEntry.Item.audioSource.volume = finalVolume;
                 soundEntry.IsChanging = false;
             }
         }
         else
         {
             soundEntry.Item.audioSource.volume += Time.deltaTime / BGMChangeVolumeTime * finalVolume;
             if (soundEntry.Item.audioSource.volume >= finalVolume)
             {
                 soundEntry.Item.audioSource.volume = finalVolume;
                 soundEntry.IsChanging = false;
             }
         }
     }
 }
Example #2
0
    private void LoadAndPlayMusic(CSoundEntry soundEntry, string soundName, float fadeInTime)
    {
        if (!string.IsNullOrEmpty(soundName) && soundName == soundEntry.Item.audioName && soundEntry.CurPhase == BgmChangePhase.OLD_FADE_OUT)
        {
            soundEntry.NewName    = soundName;
            soundEntry.CurPhase   = BgmChangePhase.NEW_FADE_IN;
            soundEntry.FadeInTime = fadeInTime;
        }
        else if (soundEntry.Item.audioName != soundName)
        {
            soundEntry.NewName    = soundName;
            soundEntry.CurPhase   = BgmChangePhase.OLD_FADE_OUT;
            soundEntry.FadeInTime = fadeInTime;

            if (!string.IsNullOrEmpty(soundEntry.NewName) && !IsClipCached(soundEntry.NewName))
            {
                Action <UnityEngine.Object> callback = asset =>
                {
                    if (!soundEntry.IsEnabled)
                    {
                        return;
                    }
                    CacheClipFromLoading(soundEntry.NewName, soundName, asset);
                };
                CAssetBundleManager.AsyncLoadResource(soundEntry.NewName, callback, false);
            }
        }
    }
Example #3
0
 private void SafeInitGameObject(CSoundEntry soundEntry, string name)
 {
     if (soundEntry.Item.attachedObj == null)
     {
         GameObject bgm_GameObject = new GameObject(name);
         bgm_GameObject.transform.SetParent(AudioItems.transform, false);
         soundEntry.Item.attachedObj = bgm_GameObject;
     }
 }
Example #4
0
 private void SafeInitGameObject(CSoundEntry soundEntry, string name)
 {
     if (soundEntry.Item.audioSource == null)
     {
         soundEntry.Item.id = 0;
         GameObject bgm_GameObject = new GameObject(name);
         bgm_GameObject.transform.SetParent(transform, false);
         soundEntry.Item.audioSource = bgm_GameObject.AddComponent <AudioSource>();
     }
 }
Example #5
0
 private void ChangeSoundVolume(CSoundEntry soundEntry, bool immediately, float volume)
 {
     if (soundEntry.Item.audioSource != null)
     {
         if (immediately)
         {
             if (soundEntry.CurPhase == BgmChangePhase.NONE && !soundEntry.IsChanging)
             {
                 soundEntry.Item.audioSource.volume = volume;
             }
         }
         else
         {
             soundEntry.IsChanging = true;
         }
     }
 }
Example #6
0
 private void LoadAndPlayMusic(CSoundEntry soundEntry, string eventName, string switchGroup, string switchState)
 {
     if (eventName != soundEntry.Item.audioName || switchGroup != soundEntry.Item.switchGroup || switchState != soundEntry.Item.switchState)
     {
         if (string.IsNullOrEmpty(eventName))
         {
             soundEntry.Item.Stop();
         }
         else
         {
             if (eventName != soundEntry.Item.audioName)
             {
                 soundEntry.Item.Stop();
             }
             soundEntry.Item.PlayWithSwitch(eventName, switchGroup, switchState);
         }
     }
 }
Example #7
0
    private void GetProperSettingOfItem(CSoundEntry soundEntry, AudioClip audioClip, string audioName)
    {
        EffectAudioSourceItem item = soundEntry.Item;

        if (item != null)
        {
            if (!string.IsNullOrEmpty(item.audioName))
            {
                //AudioClip curClip = _ClipCache[item.audioName];
                //Destroy(curClip);
                _ClipCache.Remove(item.audioName);
            }
            AudioSource audioSource = item.audioSource;
            if (audioSource != null)
            {
                audioSource.clip     = audioClip;
                audioSource.loop     = true;
                audioSource.priority = 0;
                audioSource.volume   = 0;
            }
            item.audioName = audioName;
        }
    }
Example #8
0
    private void UpdateSoundFadeInOut(CSoundEntry soundEntry, float finalVolume)
    {
        if (soundEntry.IsEnabled && soundEntry.Item.audioSource != null)
        {
            if (soundEntry.CurPhase == BgmChangePhase.OLD_FADE_OUT)
            {
                if (soundEntry.Item.audioSource == null || string.IsNullOrEmpty(soundEntry.Item.audioName))
                {
                    soundEntry.CurPhase = BgmChangePhase.NEW_FADE_IN;
                }
                else
                {
                    if (soundEntry.FadeInTime <= 0)
                    {
                        soundEntry.Item.audioSource.volume = 0;
                    }
                    else
                    {
                        soundEntry.Item.audioSource.volume -= (Time.deltaTime / soundEntry.FadeInTime * finalVolume);
                    }

                    if (soundEntry.Item.audioSource.volume <= 0)
                    {
                        soundEntry.Item.audioSource.volume = 0;
                        soundEntry.Item.audioSource.Stop();

                        soundEntry.CurPhase = BgmChangePhase.NEW_FADE_IN;
                    }
                }
            }
            else if (soundEntry.CurPhase == BgmChangePhase.NEW_FADE_IN)
            {
                if (soundEntry.Item.audioSource == null)
                {
                    soundEntry.CurPhase = BgmChangePhase.NONE;
                }
                else
                {
                    if (string.IsNullOrEmpty(soundEntry.NewName))
                    {
                        soundEntry.Item.audioSource.clip = null;
                        soundEntry.Item.audioName        = string.Empty;
                        soundEntry.CurPhase = BgmChangePhase.NONE;
                    }
                    else if (IsClipCached(soundEntry.NewName))
                    {
                        if (!soundEntry.Item.audioSource.isPlaying)
                        {
                            //PlayAudio(_ClipCache[_newBgmName], true, Vector3.zero, _newBgmName);
                            GetProperSettingOfItem(soundEntry, _ClipCache[soundEntry.NewName], soundEntry.NewName);
                            soundEntry.Item.audioSource.Play();
                            soundEntry.Item.audioName = soundEntry.NewName;
                        }

                        if (soundEntry.FadeInTime <= 0)
                        {
                            soundEntry.Item.audioSource.volume = finalVolume;
                        }
                        else
                        {
                            soundEntry.Item.audioSource.volume += (Time.deltaTime / soundEntry.FadeInTime * finalVolume);
                        }
                        if (soundEntry.Item.audioSource.volume >= finalVolume)
                        {
                            soundEntry.Item.audioSource.volume = finalVolume;
                            soundEntry.CurPhase = BgmChangePhase.NONE;
                            soundEntry.NewName  = string.Empty;
                        }
                    }
                }
            }
        }
    }