From e54d4b6cdbd61bd21029df67225d50ca461f58cf Mon Sep 17 00:00:00 2001 From: zc <1062808664@qq.com> Date: Wed, 30 Oct 2024 21:32:36 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=E5=AE=8C=E5=96=84excel=EF=BC=8C?= =?UTF-8?q?=E7=8E=B0=E5=9C=A8=E7=9A=84=E4=B8=8D=E9=9C=80=E8=A6=81=E6=89=8B?= =?UTF-8?q?=E5=8A=A8=E5=88=9B=E5=BB=BA=E6=96=87=E4=BB=B6=E5=A4=B9=EF=BC=8C?= =?UTF-8?q?=E8=80=8C=E4=B8=94=E5=B0=B1=E7=AE=97=E8=AF=AF=E5=88=A0=E5=86=85?= =?UTF-8?q?=E5=AE=B9=E4=B9=9F=E5=8F=AF=E4=BB=A5=E9=87=8D=E6=96=B0=E7=94=9F?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DemoGame/GameRes/ExcelConstTemplate.txt | 7 ++ .../GameRes/ExcelConstTemplate.txt.meta | 3 + .../DemoGame/GameScript/Editor/ExcelEditor.cs | 101 ++++++++++++++---- 3 files changed, 90 insertions(+), 21 deletions(-) create mode 100644 Assets/DemoGame/GameRes/ExcelConstTemplate.txt create mode 100644 Assets/DemoGame/GameRes/ExcelConstTemplate.txt.meta diff --git a/Assets/DemoGame/GameRes/ExcelConstTemplate.txt b/Assets/DemoGame/GameRes/ExcelConstTemplate.txt new file mode 100644 index 0000000..4965e74 --- /dev/null +++ b/Assets/DemoGame/GameRes/ExcelConstTemplate.txt @@ -0,0 +1,7 @@ +using UnityEngine; +using System.Collections.Generic; + +public static class #CLASSNAME# +{ + #CONSTCONTENT# +} \ No newline at end of file diff --git a/Assets/DemoGame/GameRes/ExcelConstTemplate.txt.meta b/Assets/DemoGame/GameRes/ExcelConstTemplate.txt.meta new file mode 100644 index 0000000..812eaf8 --- /dev/null +++ b/Assets/DemoGame/GameRes/ExcelConstTemplate.txt.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 2d7d2225392a4369834946f18709f9db +timeCreated: 1730292580 \ No newline at end of file diff --git a/Assets/DemoGame/GameScript/Editor/ExcelEditor.cs b/Assets/DemoGame/GameScript/Editor/ExcelEditor.cs index 87b72ca..d3b489b 100644 --- a/Assets/DemoGame/GameScript/Editor/ExcelEditor.cs +++ b/Assets/DemoGame/GameScript/Editor/ExcelEditor.cs @@ -19,24 +19,28 @@ namespace ZEditor { private static string folderPath = $"{Application.streamingAssetsPath}/Excel/"; - private static string genPath = $"{Application.streamingAssetsPath}/ExcelData/"; + private static string genDataPath = $"{Application.streamingAssetsPath}/ExcelData/"; private static string genCSPath = $"{Application.dataPath}/DemoGame/GameScript/Hotfix/Generate/Excel2CS/"; - private static string templatePath = $"{Application.dataPath}/DemoGame/GameRes/ExcelTemplate.txt"; - [MenuItem("Tool/加载表格数据")] - static void Load() - { - string path = $"{Application.streamingAssetsPath}/ExcelData/Test.json"; - var allText = File.ReadAllText(path); - var testData = JsonConvert.DeserializeObject(allText); - Debug.Log($"{testData.TestList[0].Id} , {testData.TestList[0].Subject} , {testData.TestList[0].JX} "); - } + private static string templatePath = $"{Application.dataPath}/DemoGame/GameRes/ExcelTemplate.txt"; + private static string constTemplatePath = $"{Application.dataPath}/DemoGame/GameRes/ExcelConstTemplate.txt"; + + // [MenuItem("Tool/加载表格数据")] + // static void Load() + // { + // string path = $"{Application.streamingAssetsPath}/ExcelData/Test.json"; + // var allText = File.ReadAllText(path); + // var testData = JsonConvert.DeserializeObject(allText); + // Debug.Log($"{testData.TestList[0].Id} , {testData.TestList[0].Subject} , {testData.TestList[0].JX} "); + // } [MenuItem("Tool/表格数据生成")] static void Gen() { + DeleteAndCreateFolder(); + DirectoryInfo info = new DirectoryInfo(folderPath); var fileInfos = info.GetFiles(); foreach (var fileInfo in fileInfos) @@ -48,6 +52,71 @@ namespace ZEditor LoadExcelAndCreateCS(fileInfo.FullName, fileName); LoadExcelAndCreateData(fileInfo.FullName, fileName); } + + LoadExcelAndGenData(fileInfos); + } + + static void DeleteAndCreateFolder() + { + DirectoryInfo info = new DirectoryInfo(genCSPath); + var fileInfos = info.GetFiles(); + foreach (var fileInfo in fileInfos) + { + fileInfo.Delete(); + } + + info = new DirectoryInfo(genDataPath); + fileInfos = info.GetFiles(); + foreach (var fileInfo in fileInfos) + { + fileInfo.Delete(); + } + + if (!Directory.Exists(genDataPath)) + Directory.CreateDirectory(genDataPath); + if (!Directory.Exists(genCSPath)) + Directory.CreateDirectory(genCSPath); + } + + static void LoadExcelAndGenData(FileInfo[] fileInfos) + { + StringBuilder sb = new StringBuilder(); + foreach (var fileInfo in fileInfos) + { + if (fileInfo.Name.Contains(".meta")) + continue; + string fileName = fileInfo.Name.Replace(".xlsx", ""); + + //查找整个文档 + Workbook book = new Workbook(fileInfo.FullName); + //获取一个工作表 + Worksheet sheet = book.Worksheets[0]; + + for (var i = 2; i < sheet.Cells.Rows.Count - 2; i++) + { + var cellsColumn = sheet.Cells[i + 3, 2].Value; + if (cellsColumn == null) + { + continue; + } + + string str = cellsColumn.ToString(); + var id = int.Parse(str); + sb.AppendLine($"public const int {fileName}_{str} = {id};"); + } + } + + string text = File.ReadAllText(constTemplatePath); + text = text.Replace("#CONSTCONTENT#", sb.ToString()); + text = text.Replace("#CLASSNAME#", "ExcelConstData"); + + var csPath = $"{genCSPath}/ExcelConstData.cs"; + if (!File.Exists(csPath)) + { + File.Create(csPath).Dispose(); + } + + File.WriteAllText(csPath, text); } /// @@ -117,8 +186,6 @@ namespace ZEditor text = text.Replace("#LISTNAME#", $"{fileName}List"); - Debug.Log(text); - string filePath = genCSPath + "/" + newName + ".cs"; if (!File.Exists(filePath)) { @@ -135,14 +202,7 @@ namespace ZEditor //获取一个工作表 Worksheet sheet = book.Worksheets[0]; - Debug.Log($"{sheet.Cells.Rows.Count}行 , {sheet.Cells.Columns.Count}列"); - - // List zhuShi = new List(); - // List name = new List(); - // List type = new List(); - DataTable table = new DataTable(); - // table.Columns for (var i = 0; i < sheet.Cells.Rows.Count + 2; i++) { @@ -154,7 +214,6 @@ namespace ZEditor string name = sheet.Cells[3, j + 2].Value.ToString(); var typeName = sheet.Cells[4, j + 2].Value.ToString(); Type type = GetType(typeName); - // Debug.Log($"{name} {typeName} {type}"); table.Columns.Add(name, type); } else if (i > 4) @@ -256,7 +315,7 @@ namespace ZEditor var json = JsonConvert.SerializeObject(table); string newStr = "{" + $"\"{fileName}List\":" + json + "}"; Debug.Log(newStr); - string filePath = genPath + "/" + fileName + ".json"; + string filePath = genDataPath + "/" + fileName + ".json"; if (!File.Exists(filePath)) { File.Create(filePath).Dispose();