From 1ab047b5fdd933c38ba0519ec2e83a44512ea8d7 Mon Sep 17 00:00:00 2001
From: yyl <yyl>
Date: 星期四, 26 三月 2026 17:46:11 +0800
Subject: [PATCH] webgl代码合并 1

---
 Main/Config/ConfigManager.cs |   80 +++++++++++++++++++++++++++++++++++++++
 1 files changed, 79 insertions(+), 1 deletions(-)

diff --git a/Main/Config/ConfigManager.cs b/Main/Config/ConfigManager.cs
index c70f54f..364bfb3 100644
--- a/Main/Config/ConfigManager.cs
+++ b/Main/Config/ConfigManager.cs
@@ -177,6 +177,51 @@
         int iterator = 0;
         int totalConfigs = configTypes.Count;
 
+#if UNITY_WEBGL && !UNITY_EDITOR
+        // ============================================================
+        // WebGL 浼樺寲锛氫袱闃舵鍔犺浇
+        // 闃舵1: 涓�娆℃�у苟鍙戝彂璧锋墍鏈� TextAsset 鍔犺浇锛堝悓 bundle 鍙笅杞戒竴娆★級
+        // 闃舵2: 浠庡唴瀛樹腑鍒嗘壒瑙f瀽锛屾瘡鎵� Yield 闃叉娴忚鍣ㄥ崱姝�
+        // ============================================================
+
+        // 闃舵1: 骞跺彂鍔犺浇鎵�鏈夐厤缃枃鏈埌鍐呭瓨
+        Debug.Log("[ConfigManager] WebGL 闃舵1: 鎵归噺鍔犺浇閰嶇疆鏂囦欢...");
+        var configList = configTypes.ToList();
+        var configDataMap = new Dictionary<Type, string[]>(totalConfigs);
+        var loadTasks = new List<UniTask>(totalConfigs);
+
+        foreach (var configType in configList)
+        {
+            var ct = configType; // closure capture
+            loadTasks.Add(LoadConfigTextAsync(ct).ContinueWith(texts =>
+            {
+                if (texts != null)
+                    configDataMap[ct] = texts;
+                else
+                    Debug.LogError($"鎵句笉鍒伴厤缃枃浠�: {ct.Name}");
+            }));
+        }
+        await UniTask.WhenAll(loadTasks);
+        loadingProgress = 0.5f; // 缃戠粶鍔犺浇瀹屾垚 50%
+        Debug.Log($"[ConfigManager] WebGL 闃舵1瀹屾垚: {configDataMap.Count}/{totalConfigs} 涓厤缃凡鍔犺浇鍒板唴瀛�");
+
+        // 闃舵2: 浠庡唴瀛樹腑鍒嗘壒瑙f瀽鍒濆鍖�
+        const int parseBatchSize = 10;
+        int parsed = 0;
+        foreach (var configType in configList)
+        {
+            if (configDataMap.TryGetValue(configType, out var texts))
+            {
+                InitConfigFromTexts(configType, texts);
+            }
+            parsed++;
+            loadingProgress = 0.5f + 0.5f * parsed / totalConfigs;
+
+            // 姣� parseBatchSize 涓鍑轰富绾跨▼
+            if (parsed % parseBatchSize == 0)
+                await UniTask.Yield();
+        }
+#else
         List<UniTask> loadTasks = new List<UniTask>();
 
         // 閫愪釜鍔犺浇閰嶇疆骞舵洿鏂拌繘搴�
@@ -199,7 +244,7 @@
                 sw.Stop();
 #endif
 
-                loadingProgress = (float)(iterator++ + 1) / totalConfigs;
+                loadingProgress = (float)(++iterator) / totalConfigs;
             });
             
             loadTasks.Add(uniTask);
@@ -207,6 +252,7 @@
         }
 
         await UniTask.WhenAll(loadTasks);
+#endif
 
         // 鍔犺浇瀹屾垚鍚庤缃甶sLoadFinished涓簍rue
         loadingProgress = 1f;
@@ -481,4 +527,36 @@
         ClearConfigDictionary<ZhanlingConfig>();
     }
 
+    /// <summary>
+    /// 鍙姞杞介厤缃枃鏈紝涓嶅仛瑙f瀽銆傜敤浜� WebGL 鎵归噺棰勫姞杞姐��
+    /// </summary>
+    private async UniTask<string[]> LoadConfigTextAsync(Type configType)
+    {
+        string configName = configType.Name;
+        if (configName.EndsWith("Config"))
+            configName = configName.Substring(0, configName.Length - 6);
+        return await ResManager.Instance.LoadConfigAsync(configName);
+    }
+
+    /// <summary>
+    /// 浠庡凡鍔犺浇鐨勬枃鏈垵濮嬪寲閰嶇疆锛堢函鍐呭瓨鎿嶄綔锛屾棤缃戠粶锛夈��
+    /// </summary>
+    private void InitConfigFromTexts(Type configType, string[] texts)
+    {
+        var methodInfo = configType.GetMethod("Init",
+            System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.FlattenHierarchy);
+        if (methodInfo != null)
+        {
+            methodInfo.Invoke(null, new object[] { texts });
+            var isInitField = configType.GetField("isInit",
+                System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
+            if (isInitField != null)
+                isInitField.SetValue(null, true);
+        }
+        else
+        {
+            Debug.LogError($"閰嶇疆绫� {configType.Name} 娌℃湁闈欐�両nit鏂规硶");
+        }
+    }
+
 }

--
Gitblit v1.8.0