From 1566a91aadaaccad72c140dbcd38dd17dc0e5c0a Mon Sep 17 00:00:00 2001
From: yyl <yyl>
Date: 星期一, 14 七月 2025 14:56:51 +0800
Subject: [PATCH] 18 子 2D卡牌客户端搭建 / 2D卡牌客户端搭建 配置表问题
---
Assets/Editor/ConfigGen/ConfigGenerater.cs | 152 ++++++++++++++++++++++++++++++++++++++++++++------
Assets/Launch/Launch.cs | 4 +
2 files changed, 136 insertions(+), 20 deletions(-)
diff --git a/Assets/Editor/ConfigGen/ConfigGenerater.cs b/Assets/Editor/ConfigGen/ConfigGenerater.cs
index 7e3d3f0..cec9736 100644
--- a/Assets/Editor/ConfigGen/ConfigGenerater.cs
+++ b/Assets/Editor/ConfigGen/ConfigGenerater.cs
@@ -5,7 +5,6 @@
using System.Text;
using UnityEditor;
using UnityEngine;
-using System.Linq;
/// <summary>
/// 閰嶇疆鐢熸垚鍣� - 鐢ㄤ簬鑷姩鐢熸垚閰嶇疆绠$悊鍣ㄤ唬鐮�
@@ -16,7 +15,7 @@
private const string ConfigsPath = "Assets/Scripts/Main/Config/Configs";
private const string ConfigManagerPath = "Assets/Scripts/Main/Config/ConfigManager.cs";
- private static List<string> ExcludeClassList = new List<string>()
+ public static List<string> ExcludeClassList = new List<string>()
{
// 鐗规畩琛ㄦ牸
"InitialFunctionConfig",
@@ -71,7 +70,7 @@
/// 鑾峰彇鎵�鏈夐厤缃被
/// </summary>
/// <returns>閰嶇疆绫诲悕绉板垪琛�</returns>
- private static List<string> GetAllConfigClasses()
+ public static List<string> GetAllConfigClasses()
{
// 鑾峰彇閰嶇疆鐩綍鐨勫畬鏁磋矾寰�
string fullConfigsPath = Path.Combine(Application.dataPath, ConfigsPath.Replace("Assets/", ""));
@@ -145,7 +144,11 @@
sb.AppendLine("using UnityEngine;");
sb.AppendLine("using Cysharp.Threading.Tasks;");
sb.AppendLine("using System.Reflection;");
+ sb.AppendLine("using System.Linq;");
sb.AppendLine();
+ sb.AppendLine("#if UNITY_EDITOR");
+ sb.AppendLine("using UnityEditor;");
+ sb.AppendLine("#endif");
sb.AppendLine();
sb.AppendLine("public class ConfigManager : ManagerBase<ConfigManager>");
sb.AppendLine("{");
@@ -175,8 +178,7 @@
sb.AppendLine(" isLoadFinished = false;");
sb.AppendLine();
sb.AppendLine(" // 鍔犺浇閰嶇疆鏂囦欢");
- sb.AppendLine($" int totalConfigs = {configClasses.Count};");
- sb.AppendLine(" List<Type> configTypes = new List<Type>() {");
+ sb.AppendLine(" HashSet<Type> configTypes = new HashSet<Type>() {");
for (int i = 0; i < configClasses.Count; i++)
{
sb.Append($" typeof({configClasses[i]})");
@@ -186,7 +188,7 @@
sb.AppendLine();
sb.AppendLine("#if UNITY_EDITOR");
sb.AppendLine(" HashSet<Type> configHashSet = new HashSet<Type>();");
- sb.AppendLine(" if (System.IO.File.Exists(Application.dataPath + \"/fastConfig.txt\"))");
+ sb.AppendLine(" if (System.IO.File.Exists(Application.dataPath + \"/fastConfig.txt\") && Launch.Instance.isOpenConfigTesting)");
sb.AppendLine(" {");
sb.AppendLine(" string[] strConfgsArr = System.IO.File.ReadAllLines(Application.dataPath + \"/fastConfig.txt\");");
sb.AppendLine(" foreach (string str in strConfgsArr)");
@@ -196,38 +198,50 @@
sb.AppendLine(" }");
sb.AppendLine(" }");
sb.AppendLine(" // 缂栬緫鍣ㄤ笅鍔犲叆 璇勪及鍔犺浇鏃跺父");
- sb.AppendLine(" configTypes.AddRange(configHashSet);");
+ sb.AppendLine(" foreach (var config in configHashSet)");
+ sb.AppendLine(" {");
+ sb.AppendLine(" if (!configTypes.Add(config))");
+ sb.AppendLine(" {");
+ sb.AppendLine(" Debug.LogWarning($\"閰嶇疆 {config.Name} 宸茬粡瀛樺湪浜� configTypes 涓紝璺宠繃娣诲姞銆俓");");
+ sb.AppendLine(" }");
+ sb.AppendLine(" }");
sb.AppendLine(" List<string> fastName = new List<string>();");
sb.AppendLine("#endif");
+ sb.AppendLine(" int iterator = 0;");
+ sb.AppendLine(" int totalConfigs = configTypes.Count;");
+ sb.AppendLine();
sb.AppendLine(" // 閫愪釜鍔犺浇閰嶇疆骞舵洿鏂拌繘搴�");
- sb.AppendLine(" for (int i = 0; i < configTypes.Count; i++)");
+ sb.AppendLine(" foreach (var configType in configTypes)");
sb.AppendLine(" {");
sb.AppendLine(" var sw = System.Diagnostics.Stopwatch.StartNew();");
- sb.AppendLine(" LoadConfigByType(configTypes[i]);");
+ sb.AppendLine(" LoadConfigByType(configType);");
sb.AppendLine(" sw.Stop();");
sb.AppendLine("#if UNITY_EDITOR");
sb.AppendLine(" if (sw.ElapsedMilliseconds >= 100)");
sb.AppendLine(" {");
- sb.AppendLine(" Debug.LogError($\"鍔犺浇閰嶇疆 {configTypes[i].Name} 鑰楁椂杈冮暱: {sw.ElapsedMilliseconds} ms\");");
+ sb.AppendLine(" Debug.LogError($\"鍔犺浇閰嶇疆 {configType.Name} 鑰楁椂杈冮暱: {sw.ElapsedMilliseconds} ms\");");
sb.AppendLine(" }");
sb.AppendLine(" else if (sw.ElapsedMilliseconds <= 5)");
sb.AppendLine(" {");
- sb.AppendLine(" fastName.Add(configTypes[i].Name);");
+ sb.AppendLine(" fastName.Add(configType.Name);");
sb.AppendLine(" }");
- sb.AppendLine(" Debug.Log($\"鍔犺浇閰嶇疆: {configTypes[i].Name} 鐢ㄦ椂: {sw.ElapsedMilliseconds} ms\");");
+ sb.AppendLine(" Debug.Log($\"鍔犺浇閰嶇疆: {configType.Name} 鐢ㄦ椂: {sw.ElapsedMilliseconds} ms\");");
sb.AppendLine("#endif");
- sb.AppendLine(" loadingProgress = (float)(i + 1) / totalConfigs;");
+ sb.AppendLine(" loadingProgress = (float)(iterator++ + 1) / totalConfigs;");
sb.AppendLine(" }");
sb.AppendLine("#if UNITY_EDITOR");
- sb.AppendLine(" System.IO.File.WriteAllText(Application.dataPath + \"/fastConfig.txt\", string.Join(\"\\n\", fastName));");
- sb.AppendLine();
- sb.AppendLine(" //鍔犺浇瀹屽悗鍗歌浇");
- sb.AppendLine(" foreach (var configType in configTypes)");
+ sb.AppendLine(" if (Launch.Instance.isOpenConfigTesting)");
sb.AppendLine(" {");
- sb.AppendLine(" var methodInfo = configType.GetMethod(\"ForceRelease\", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.FlattenHierarchy);");
- sb.AppendLine(" if (methodInfo != null)");
+ sb.AppendLine(" System.IO.File.WriteAllText(Application.dataPath + \"/fastConfig.txt\", string.Join(\"\\n\", fastName));");
+ sb.AppendLine();
+ sb.AppendLine(" //鍔犺浇瀹屽悗鍗歌浇");
+ sb.AppendLine(" foreach (var configType in configTypes)");
sb.AppendLine(" {");
- sb.AppendLine(" methodInfo.Invoke(null, null);");
+ sb.AppendLine(" var methodInfo = configType.GetMethod(\"ForceRelease\", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.FlattenHierarchy);");
+ sb.AppendLine(" if (methodInfo != null)");
+ sb.AppendLine(" {");
+ sb.AppendLine(" methodInfo.Invoke(null, null);");
+ sb.AppendLine(" }");
sb.AppendLine(" }");
sb.AppendLine(" }");
sb.AppendLine("#endif");
@@ -325,6 +339,104 @@
sb.AppendLine($" ClearConfigDictionary<{className}>();");
}
sb.AppendLine(" }");
+ sb.AppendLine();
+ sb.AppendLine("#if UNITY_EDITOR");
+ sb.AppendLine(" [MenuItem(\"Tools/Config/鑷\")]");
+ sb.AppendLine(" public static void CheckAndGenerateFastConfig()");
+ sb.AppendLine(" {");
+ sb.AppendLine(" // 鑾峰彇 Editor Assembly");
+ sb.AppendLine(" var editorAsm = System.AppDomain.CurrentDomain.GetAssemblies()");
+ sb.AppendLine(" .FirstOrDefault(a => a.FullName.Contains(\"Editor\"));");
+ sb.AppendLine();
+ sb.AppendLine(" if (editorAsm == null)");
+ sb.AppendLine(" {");
+ sb.AppendLine(" Debug.LogError(\"[鑷] 鏈壘鍒� Editor Assembly锛屾棤娉曡嚜妫�銆俓");");
+ sb.AppendLine(" return;");
+ sb.AppendLine(" }");
+ sb.AppendLine();
+ sb.AppendLine(" // 鍙嶅皠鑾峰彇 ConfigGenerater 绫诲瀷");
+ sb.AppendLine(" var configGeneraterType = editorAsm.GetType(\"ConfigGenerater\");");
+ sb.AppendLine(" if (configGeneraterType == null)");
+ sb.AppendLine(" {");
+ sb.AppendLine(" Debug.LogError(\"[鑷] 鏈壘鍒� ConfigGenerater 绫诲瀷銆俓");");
+ sb.AppendLine(" return;");
+ sb.AppendLine(" }");
+ sb.AppendLine();
+ sb.AppendLine(" // 璋冪敤 GetAllConfigClasses 闈欐�佹柟娉�");
+ sb.AppendLine(" var getAllConfigClassesMethod = configGeneraterType.GetMethod(\"GetAllConfigClasses\", BindingFlags.Public | BindingFlags.Static);");
+ sb.AppendLine(" var allConfigClasses = getAllConfigClassesMethod?.Invoke(null, null) as List<string>;");
+ sb.AppendLine(" if (allConfigClasses == null)");
+ sb.AppendLine(" {");
+ sb.AppendLine(" Debug.LogError(\"[鑷] 鑾峰彇鍏ㄩ儴閰嶇疆绫诲け璐ャ�俓");");
+ sb.AppendLine(" return;");
+ sb.AppendLine(" }");
+ sb.AppendLine();
+ sb.AppendLine(" // 鑾峰彇 ExcludeClassList 瀛楁");
+ sb.AppendLine(" var excludeField = configGeneraterType.GetField(\"ExcludeClassList\", BindingFlags.Public | BindingFlags.Static);");
+ sb.AppendLine(" var excludeClassList = excludeField?.GetValue(null) as List<string> ?? new List<string>();");
+ sb.AppendLine();
+ sb.AppendLine(" // 鎺掗櫎涓嶉渶瑕佺殑绫�");
+ sb.AppendLine(" var checkClasses = allConfigClasses.Where(c => !excludeClassList.Contains(c)).ToList();");
+ sb.AppendLine();
+ sb.AppendLine(" List<string> fastName = new List<string>();");
+ sb.AppendLine();
+ sb.AppendLine(" foreach (var className in checkClasses)");
+ sb.AppendLine(" {");
+ sb.AppendLine(" // 杩欓噷涔熻鐢� Editor Assembly 鑾峰彇绫诲瀷");
+ sb.AppendLine(" var configType = editorAsm.GetType(className) ?? Type.GetType(className);");
+ sb.AppendLine(" if (configType == null)");
+ sb.AppendLine(" {");
+ sb.AppendLine(" Debug.LogWarning($\"[鑷] 鏈壘鍒扮被鍨�: {className}\");");
+ sb.AppendLine(" continue;");
+ sb.AppendLine(" }");
+ sb.AppendLine();
+ sb.AppendLine(" var sw = System.Diagnostics.Stopwatch.StartNew();");
+ sb.AppendLine();
+ sb.AppendLine(" // 鍙嶅皠璋冪敤闈欐�両nit鏂规硶");
+ sb.AppendLine(" string configName = configType.Name;");
+ sb.AppendLine(" if (configName.EndsWith(\"Config\"))");
+ sb.AppendLine(" configName = configName.Substring(0, configName.Length - 6);");
+ sb.AppendLine();
+ sb.AppendLine(" string[] texts = ResManager.Instance.LoadConfig(configName);");
+ sb.AppendLine(" if (texts != null)");
+ sb.AppendLine(" {");
+ sb.AppendLine(" string[] lines = texts;");
+ sb.AppendLine(" var methodInfo = configType.GetMethod(\"Init\", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.FlattenHierarchy);");
+ sb.AppendLine(" if (methodInfo != null)");
+ sb.AppendLine(" {");
+ sb.AppendLine(" methodInfo.Invoke(null, new object[] { lines });");
+ sb.AppendLine(" }");
+ sb.AppendLine(" }");
+ sb.AppendLine();
+ sb.AppendLine(" sw.Stop();");
+ sb.AppendLine();
+ sb.AppendLine(" if (sw.ElapsedMilliseconds >= 100)");
+ sb.AppendLine(" {");
+ sb.AppendLine(" Debug.LogError($\"[鑷] 鍔犺浇閰嶇疆 {configType.Name} 鑰楁椂杈冮暱: {sw.ElapsedMilliseconds} ms\");");
+ sb.AppendLine(" }");
+ sb.AppendLine(" else if (sw.ElapsedMilliseconds <= 5)");
+ sb.AppendLine(" {");
+ sb.AppendLine(" fastName.Add(configType.Name);");
+ sb.AppendLine(" }");
+ sb.AppendLine(" Debug.Log($\"[鑷] 鍔犺浇閰嶇疆: {configType.Name} 鐢ㄦ椂: {sw.ElapsedMilliseconds} ms\");");
+ sb.AppendLine(" }");
+ sb.AppendLine();
+ sb.AppendLine(" // 閲婃斁鎵�鏈夊凡鍔犺浇鐨勯厤缃�");
+ sb.AppendLine(" foreach (var className in checkClasses)");
+ sb.AppendLine(" {");
+ sb.AppendLine(" var configType = editorAsm.GetType(className) ?? Type.GetType(className);");
+ sb.AppendLine(" if (configType == null) continue;");
+ sb.AppendLine(" var methodInfo = configType.GetMethod(\"ForceRelease\", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.FlattenHierarchy);");
+ sb.AppendLine(" if (methodInfo != null)");
+ sb.AppendLine(" {");
+ sb.AppendLine(" methodInfo.Invoke(null, null);");
+ sb.AppendLine(" }");
+ sb.AppendLine(" }");
+ sb.AppendLine();
+ sb.AppendLine(" System.IO.File.WriteAllText(Application.dataPath + \"/fastConfig.txt\", string.Join(\"\\n\", fastName));");
+ sb.AppendLine(" Debug.Log($\"[鑷] fastConfig.txt 鐢熸垚瀹屾瘯锛屽揩閫熻〃鏈夛細{string.Join(\", \", fastName)}\");");
+ sb.AppendLine(" }");
+ sb.AppendLine("#endif");
sb.AppendLine("}");
return sb.ToString();
diff --git a/Assets/Launch/Launch.cs b/Assets/Launch/Launch.cs
index cd5c464..8b88680 100644
--- a/Assets/Launch/Launch.cs
+++ b/Assets/Launch/Launch.cs
@@ -14,6 +14,10 @@
public class Launch : MonoBehaviour
{
+#if UNITY_EDITOR
+ public bool isOpenConfigTesting = false;
+#endif
+
private static Assembly _hotUpdateAss;
--
Gitblit v1.8.0