ソースを参照

添加剧情的资源加载

leiyasi 1 年間 前
コミット
17cc97fb35

+ 86 - 3
GameClient/Assets/Game/HotUpdate/Views/MainStory/StoryDialogView.cs

@@ -5,6 +5,9 @@ using System.Collections;
 using System.Collections.Generic;
 using System.Text.RegularExpressions;
 using System;
+using YooAsset;
+using System.Threading.Tasks;
+using GFGGame.Launcher;
 
 namespace GFGGame
 {
@@ -109,6 +112,8 @@ namespace GFGGame
         protected override void OnShown()
         {
             base.OnShown();
+
+            // 初始化
             MusicManager.Instance.Stop();
             MusicManager.Instance.SetNormalVolumn(0.6f);
             MusicManager.Instance.SetVolume(0);
@@ -150,9 +155,6 @@ namespace GFGGame
             }
 
             _ui.m_btnSkip.visible = skipable;
-
-            ShowNextStep(_storyStartID);
-
             _ui.m_c1.selectedIndex = 0;
             _ui.m_btnAutoPlay.selected = false;
 
@@ -173,6 +175,9 @@ namespace GFGGame
             _ui.m_btnBack.visible = InstanceZonesDataManager.CheckLevelPass(100001001);
             StoryDialogDataManager.Instance.Clear();
 
+            InitStepListById(_storyStartID);
+            // 检查资源的初始化
+            Timers.inst.StartCoroutine(CheckResLoad());
         }
 
         protected override void OnHide()
@@ -912,5 +917,83 @@ namespace GFGGame
             PrefabManager.Instance.Restore(cg);
             OnStepComplete();
         }
+
+        /// <summary>
+        /// 检查资源加载是否完成
+        /// </summary>
+        private IEnumerator CheckResLoad()
+        {
+            List<string> resList = new List<string>();
+
+            for (int i = 0; i < _stepListToRead.Count; i++)
+            {
+                StoryDialogCfg cfg = _stepListToRead[i];
+
+                if (cfg.bgRes.Length > 0)
+                {
+                    string bgRes = ResPathUtil.GetSceneBgPath(cfg.bgRes);
+                    if (!resList.Contains(bgRes))
+                    {
+                        resList.Add(bgRes);
+                    }
+                }
+
+                if (cfg.aniRes.Length > 0)
+                {
+                    string res = cfg.aniRes.Split('/')[1];
+                    string aniRes = ResPathUtil.GetStoryDialogCGPath(cfg.aniRes, res);
+                    if (!resList.Contains(aniRes))
+                    {
+                        resList.Add(aniRes);
+                    }
+                }
+            }
+
+            ResourceDownloaderOperation downloaderOperation = YooAssets.CreateBundleDownloader(resList.ToArray(), 3, 3);
+            if (downloaderOperation.TotalDownloadCount == 0)
+            {
+                ShowNextStep(_storyStartID);
+                yield break;
+            }
+
+            // 开始加载资源
+            ViewManager.Show<LoadingView>();
+            LoadingView.Instance.SetDesc("正在加载剧情资源...");
+
+            downloaderOperation.OnDownloadErrorCallback =
+            (fileName, error) =>
+            {
+                Debug.LogError($"加载{fileName}失败 {error}");
+            };
+
+
+            downloaderOperation.OnDownloadProgressCallback =
+            (totalDownloadCount, currentDownloadCount, totalDownloadSizeBytes, currentDownloadSizeBytes) =>
+            {
+                string currentSizeMB = (currentDownloadSizeBytes / 1048576f).ToString("f1");
+                string totalSizeMB = (totalDownloadSizeBytes / 1048576f).ToString("f1");
+                var progress = (float)currentDownloadSizeBytes / totalDownloadSizeBytes;
+                //LauncherView.Instance.SetDesc($"正在下载资源,{currentDownloadCount}/{totalDownloadCount}", $"{currentSizeMB}MB/{totalSizeMB}MB", true);
+                LoadingView.Instance.SetProgress((int)(progress * 100));
+            };
+
+            downloaderOperation.BeginDownload();
+            yield return downloaderOperation;
+
+            // 检测下载结果
+            if (downloaderOperation.Status != EOperationStatus.Succeed)
+            {
+                Alert.Show("下载失败!请检查网络状态后重试。")
+                    .SetLeftButton(true, "返回", (data) =>
+                    {
+                        ViewManager.Hide<LoadingView>();
+                        Hide();
+                    });
+                yield break;
+            }
+
+            ViewManager.Hide<LoadingView>();
+            ShowNextStep(_storyStartID);
+        }
     }
 }