Browse Source

添加语音逻辑

leiyasi 1 year ago
parent
commit
f13c435542

+ 5 - 4
GameClient/Assets/Game/HotUpdate/Data/StoryDialogDataManager.cs

@@ -12,15 +12,16 @@ namespace GFGGame
             _waitBgChange = false;
         }
 
-        private bool _waitPicFade = false;
-        private bool _waitBgChange = false;
-        private float _dialogShowDelay = 0.0f;
-        private bool _waiting = false;
+        private bool _waitPicFade = false;          // 等待立绘加载
+        private bool _waitBgChange = false;         // 等待背景更换
+        private float _dialogShowDelay = 0.0f;      // 对话延迟出现
+        private bool _waiting = false;              // 全局等待,如等待对话框的播放动画结束,或者等待某些演出结束
 
         // Dialog wait pic's animation finished 
         public bool waitPicFade { get { return _waitPicFade; } set { _waitPicFade = value; } }
         public bool waitBgChange { get { return _waitBgChange; } set { _waitBgChange = value; } }
         public float dialogShowDelay { get { return _dialogShowDelay; } set { _dialogShowDelay = value; } }
+        
 
         public bool waiting { get { return _waiting; } set { _waiting = value; } }
     }

+ 124 - 0
GameClient/Assets/Game/HotUpdate/Sound/VoiceManager.cs

@@ -0,0 +1,124 @@
+using UnityEngine;
+using FairyGUI;
+using GFGGame.Launcher;
+using YooAsset;
+using System.Collections;
+using System.Threading.Tasks;
+using System;
+
+namespace GFGGame
+{
+    public class VoiceManager : SingletonMonoBase<VoiceManager>
+    {
+        private AudioSource player;
+        private AssetOperationHandle handle;
+        private Coroutine coroutine;
+
+        private bool _isOn = true;
+        public bool isOn
+        {
+            get
+            {
+                return _isOn;
+            }
+            set
+            {
+                if (_isOn != value)
+                {
+                    _isOn = value;
+                    if (_isOn)
+                    {
+                        GRoot.inst.soundVolume = 1;
+                    }
+                    else
+                    {
+                        GRoot.inst.soundVolume = 0;
+                        Stop();
+                    }
+                    LocalCache.SetBool(LauncherConfig.SOUND_KEY, _isOn);
+                }
+            }
+        }
+
+        private void Awake()
+        {
+            player = this.gameObject.AddComponent<AudioSource>();
+            isOn = LocalCache.GetBool(LauncherConfig.SOUND_KEY, true);
+        }
+
+        public void LoadRes(string path)
+        {
+            if (!YooAssets.CheckResExist(path))
+            {
+                return;
+            }
+            handle = YooAssets.LoadAssetSync<AudioClip>(path);
+            player.clip = handle.AssetObject as AudioClip;
+        }
+
+        public void PlayVoice()
+        {
+            if(player.clip != null)
+            {
+                player.Play();
+            }
+        }
+
+        public void StopVoice()
+        {
+            player.Stop();
+        }
+
+        public int GetClipLength()
+        {
+            if (player.clip != null)
+            {
+                return (int)Mathf.Ceil(player.clip.length);
+            }
+            return 0;
+        }
+
+        public void PlayOneShotCroutine(string path, Action action = null)
+        {
+            if (coroutine != null)
+            {
+                StopCoroutine(coroutine);
+            }
+            handle?.Release();
+            if (_isOn)
+            {
+                if (YooAssets.CheckResExist(path))
+                {
+                    coroutine = StartCoroutine(PlayOneShot(path, action));
+                }
+                else
+                {
+                    action?.Invoke();
+                }
+            }
+        }
+
+        private IEnumerator PlayOneShot(string path, Action action = null)
+        {
+            //AudioClip clip = GFGAsset.Load<AudioClip>(path);
+            handle = YooAssets.LoadAssetAsync<AudioClip>(path);
+            yield return handle;
+            player.clip = handle.AssetObject as AudioClip;
+            player.Play();
+            WaitForSound(action);
+        }
+
+        public void Stop()
+        {
+            player.Stop();
+        }
+
+        // 音频播放完成的回调函数 
+        private async Task WaitForSound(Action action = null)
+        {
+            int milliseconds = (int)(player.clip.length * 1000);
+            await Task.Delay(milliseconds);
+            action?.Invoke();
+        }
+    }
+}

+ 11 - 0
GameClient/Assets/Game/HotUpdate/Sound/VoiceManager.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: b7a686a7c8d91d5419fedb4cd492f9ce
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 15 - 7
GameClient/Assets/Game/HotUpdate/Views/MainStory/StoryDialogView.cs

@@ -142,7 +142,6 @@ namespace GFGGame
             if (_storyStartID == MainStoryDataManager.priorId)
             {
                 _ui.m_c1.selectedIndex = 1;
-                // _ui.m_btnAutoPlay.selected = true;
                 OnClickBtnAutoPlay();
                 _speedAutoPlay = 1;
                 FightDataManager.Instance.dialogSpeed = _speedAutoPlay;
@@ -179,11 +178,8 @@ namespace GFGGame
 
         private void OnClickBtnBack()
         {
-             
+            //Hide();
             ViewManager.Show<StoryChapterView>(MainStoryDataManager.currentChapterCfgId, ViewManager.GetGoBackDatas(typeof(StoryChapterView).FullName), true);
-            this.Hide();
-            //ViewManager.GoBackFrom(typeof(StoryDialogView).FullName);
-            // Over(false);
         }
 
         private void OnClickBtnNext()
@@ -426,6 +422,11 @@ namespace GFGGame
             string roleName = storyDialogCfg.name;
             string headRes = storyDialogCfg.head;
             string headAniRes = storyDialogCfg.headAni;
+
+            VoiceManager.Instance.StopVoice();
+            // 如果配置了语音,读取语音
+            VoiceManager.Instance.LoadRes(ResPathUtil.GetCardSoundPath("111111111"));
+
             if (roleName == "self")
             {
                 roleName = RoleDataManager.roleName;
@@ -468,7 +469,6 @@ namespace GFGGame
                         {
                             _dressUpObjUI.dressUpObj.CancelAction(true);
                         }
-                        //_dressUpObjUI.dressUpObj.PutOnDressUpData(_dressUpObjUI.dressUpObj.DressUpDataClone(), new int[] { ConstDressUpItemType.SHOU_CHI_WU });
                     }
                     if (headAniCfg != null && headAniCfg.faceId > 0)
                     {
@@ -550,6 +550,7 @@ namespace GFGGame
             }
             _wordList = Regex.Split(words, "&&");
             _wordIndex = 0;
+
             _typingEffect = new TypingFadeEffectPro(_wordTextField);
             _typingEffect.typeFinishedAction = ShowCurrentWords;
 
@@ -592,7 +593,8 @@ namespace GFGGame
             _wordIndex++;
             if (_autoPlay)
             {
-                Timers.inst.Add(GameConst.NEXT_WORDS_INTERVAL_MAX / _speedAutoPlay, 1, ShowNextWords);
+                int interval = (int)Mathf.Max(VoiceManager.Instance.GetClipLength(), GameConst.NEXT_WORDS_INTERVAL_MAX / _speedAutoPlay);
+                Timers.inst.Add(interval, 1, ShowNextWords);
             }
         }
 
@@ -638,10 +640,16 @@ namespace GFGGame
             StartTyping();
         }
 
+        /// <summary>
+        /// 开启打字机显示
+        /// </summary>
         private void StartTyping()
         {
             _typingEffect.SetSpeed(_speedAutoPlay);
             _typingEffect.Start();
+
+            // 如果配置了语音,则播放语音
+            VoiceManager.Instance.PlayVoice();
         }
 
         private void StopTyping()