From 379cecbb2903fdbda60d423beb87e31b7f1a108b Mon Sep 17 00:00:00 2001 From: zhangxl <1062808664@qq.com> Date: Mon, 22 Jul 2024 15:24:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E9=85=8D=E7=BD=AE=E8=A1=A8?= =?UTF-8?q?=E5=B8=B8=E9=87=8F=E5=B7=A5=E5=85=B7=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DemoGame/GameRes/ConfigConstTemplate.txt | 5 ++ .../GameRes/ConfigConstTemplate.txt.meta | 3 + .../GameScript/Editor/GenAssetConstEditor.cs | 4 +- .../GameScript/Editor/GenConfigCSEditor.cs | 74 +++++++++++-------- .../GameScript/Hotfix/Const/ConfigConst.cs | 18 +++++ .../Hotfix/Const/ConfigConst.cs.meta | 11 +++ 6 files changed, 81 insertions(+), 34 deletions(-) create mode 100644 Assets/DemoGame/GameRes/ConfigConstTemplate.txt create mode 100644 Assets/DemoGame/GameRes/ConfigConstTemplate.txt.meta create mode 100644 Assets/DemoGame/GameScript/Hotfix/Const/ConfigConst.cs create mode 100644 Assets/DemoGame/GameScript/Hotfix/Const/ConfigConst.cs.meta diff --git a/Assets/DemoGame/GameRes/ConfigConstTemplate.txt b/Assets/DemoGame/GameRes/ConfigConstTemplate.txt new file mode 100644 index 0000000..0747672 --- /dev/null +++ b/Assets/DemoGame/GameRes/ConfigConstTemplate.txt @@ -0,0 +1,5 @@ +using UnityEngine; +public static class #CLASSNAME# +{ + #CONSTCONTENT# +} \ No newline at end of file diff --git a/Assets/DemoGame/GameRes/ConfigConstTemplate.txt.meta b/Assets/DemoGame/GameRes/ConfigConstTemplate.txt.meta new file mode 100644 index 0000000..430bfa0 --- /dev/null +++ b/Assets/DemoGame/GameRes/ConfigConstTemplate.txt.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: f8aa6a7854744a2599fcb988dcba2871 +timeCreated: 1721631976 \ No newline at end of file diff --git a/Assets/DemoGame/GameScript/Editor/GenAssetConstEditor.cs b/Assets/DemoGame/GameScript/Editor/GenAssetConstEditor.cs index cf38396..9a10679 100644 --- a/Assets/DemoGame/GameScript/Editor/GenAssetConstEditor.cs +++ b/Assets/DemoGame/GameScript/Editor/GenAssetConstEditor.cs @@ -39,8 +39,8 @@ namespace ZEditor Bridging(ref sb, resPath); var readAllText = File.ReadAllText(template); - var content = readAllText.Replace("#CONSTCONTENT#", sb.ToString()).Replace("\\", "/").Replace("#CLASSNAME#", "AssetConst"); - var loaderContent = readAllText.Replace("#CONSTCONTENT#", sb.ToString()).Replace("\\", "/").Replace("#CLASSNAME#", "LoaderAssetConst"); + var content = readAllText.Replace("#CONSTCONTENT#", sb.ToString()).Replace("\\", "/").Replace("#CLASSNAME#", csName); + var loaderContent = readAllText.Replace("#CONSTCONTENT#", sb.ToString()).Replace("\\", "/").Replace("#CLASSNAME#", loaderCsName); if (!File.Exists(path)) { File.Create(path).Dispose(); diff --git a/Assets/DemoGame/GameScript/Editor/GenConfigCSEditor.cs b/Assets/DemoGame/GameScript/Editor/GenConfigCSEditor.cs index 052d8b2..64d0311 100644 --- a/Assets/DemoGame/GameScript/Editor/GenConfigCSEditor.cs +++ b/Assets/DemoGame/GameScript/Editor/GenConfigCSEditor.cs @@ -22,30 +22,6 @@ namespace ZEditor Run(); string folderPath = @"Assets/DemoGame/GameRes/Config/JSON"; - -// // 获取文件夹下所有文件的完整路径 -// string[] files = Directory.GetFiles(folderPath, "*.*", SearchOption.AllDirectories); -// -// Tables tables; -// // 输出文件路径 -// foreach (string file in files) -// { -// if (file.Contains(".meta")) -// { -// continue; -// } -// -// tables = new cfg.Tables(JX); -// } - -// string gameConfDir = @"Assets/DemoGame/GameRes/Config/JSON"; // 替换为gen.bat中outputDataDir指向的目录 -// var tables = new cfg.Tables(file => JSON.Parse(File.ReadAllText($"{gameConfDir}/{file}.json"))); -// StringBuilder sb = new StringBuilder(); -// foreach (var item in tables.TbItem.DataMap) -// { -// string name = $"{item.Key}_{item.Value.Name}"; -// sb.AppendLine($"public const int {name} = {item.Key};"); -// } } static void Run() @@ -78,6 +54,45 @@ namespace ZEditor { UnityEngine.Debug.LogError(errors); } + + Debug.Log("配置文件生成成功,开始生成常量脚本"); + // 自动生成脚本 + GenConst(); + } + + private const string fileName = "ConfigConst"; + private const string folderPath = "Assets/DemoGame/GameScript/Hotfix/Const"; + public static string template = "Assets/DemoGame/GameRes/ConfigConstTemplate.txt"; + +// [MenuItem("Tool/单独生成配置表常量脚本")] + static void GenConst() + { + string path = $"{folderPath}/{fileName}.cs"; + StringBuilder sb = new StringBuilder(); + + var tables = new cfg.Tables(JX); + // item_tbitem + foreach (var item in tables.TbItem.DataList) + { + sb.AppendLine($"public const int item_tbitem_{item.Id}_{item.Name}_{item.Desc} = {item.Id};"); + } + + // tbplayer + foreach (var playerData in tables.TBPlayer.DataList) + { + sb.AppendLine($"public const int tbplayer_{playerData.Id}_{playerData.Name}_{playerData.Desc} = {playerData.Id};"); + } + + var readAllText = File.ReadAllText(template); + var content = readAllText.Replace("#CONSTCONTENT#", sb.ToString()).Replace("\\", "/").Replace("#CLASSNAME#", fileName); + if (!File.Exists(path)) + { + File.Create(path).Dispose(); + } + + File.WriteAllText(path, content); + + Debug.Log("生成配置表常量脚本完成!"); } private static JSONNode JX(string arg) @@ -95,14 +110,9 @@ namespace ZEditor break; } - var textAsset = ResourcesLocalComponent.Instance.LoadAssetSync(path); - return JSON.Parse(textAsset.text); +// var textAsset = ResourcesLocalComponent.Instance.LoadAssetSync(path); + var textAsset = File.ReadAllText(path); + return JSON.Parse(textAsset); } - -// [MenuItem("Tool/运行场景")] -// public static void Run() -// { -// EditorApplication.isPlaying = true; -// } } } \ No newline at end of file diff --git a/Assets/DemoGame/GameScript/Hotfix/Const/ConfigConst.cs b/Assets/DemoGame/GameScript/Hotfix/Const/ConfigConst.cs new file mode 100644 index 0000000..480c17c --- /dev/null +++ b/Assets/DemoGame/GameScript/Hotfix/Const/ConfigConst.cs @@ -0,0 +1,18 @@ +using UnityEngine; +public static class ConfigConst +{ + public const int item_tbitem_10000_发型_初始发型 = 10000; +public const int item_tbitem_10001_外套_初始外套 = 10001; +public const int item_tbitem_10002_上衣_初始上衣 = 10002; +public const int item_tbitem_10003_裙子_初始下装 = 10003; +public const int item_tbitem_10004_袜子_初始袜子 = 10004; +public const int item_tbitem_10005_鞋子_初始鞋子 = 10005; +public const int item_tbitem_10006_发饰_初始发饰 = 10006; +public const int item_tbitem_10007_中秋节发饰_中秋节发饰 = 10007; +public const int item_tbitem_10008_中秋节鞋子_中秋节鞋子 = 10008; +public const int item_tbitem_10009_礼包_中秋节礼包 = 10009; +public const int tbplayer_10000_小明_这是小明啊 = 10000; +public const int tbplayer_10001_小兰_这是小兰啊 = 10001; +public const int tbplayer_10002_小花_这是小花啊 = 10002; + +} \ No newline at end of file diff --git a/Assets/DemoGame/GameScript/Hotfix/Const/ConfigConst.cs.meta b/Assets/DemoGame/GameScript/Hotfix/Const/ConfigConst.cs.meta new file mode 100644 index 0000000..ceeeafe --- /dev/null +++ b/Assets/DemoGame/GameScript/Hotfix/Const/ConfigConst.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 77c106f464c5948499e3b8e1784df08c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: