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();