Unity 从零开始的框架搭建1-6 读Excel配置表自动生成json和数据结构类

ops/2025/3/18 22:06:30/

        本人水平有限 如有不足还请斧正,本文仅作学习交流使用不做任何商业用途

目录

效果

Excel结构

需要插件 

读表算法 

代码


效果

Unity Excel一键读表转json和数据结构类

Excel结构

需要插件 

Epplus的dll即可 可以网上搜索 或者用Vs工具下载 另外我已上传资源

 

读表算法 

        看上面这个表的结构

        1.先找到工作簿下某一工作表的索引 默认为1

        2.从第二行获取类型到List

        从第三行获取id以及名称到另一个List

        3.获取工作表名做裁剪

        4.核心算法 

行号 | 列1(类型) | 列2(类型) | 列3(类型) | 列4(类型)
--- | --- | --- | --- | ---
2    | int        | string      | float       | int[]
3    | id         | 角色名      | 生命值      | 技能等级数组
4    | 1001       | 战士        | 80.2        | 1,2,3
5    | 1002       | 刺客        | 60          | 2,3,4

是横着读的,声明一个字典

for (int row = 4; row <= worksheet.Dimension.Rows; row++)
{// 处理当前行(如第4行、第5行...)var dataItem = new Dictionary<string, object>();// 内层循环遍历当前行的所有列for (int col = 1; col <= worksheet.Dimension.Columns; col++){// 获取当前列的值(如第4行第1列的值是"1001")string value = worksheet.Cells[row, col].Text;// 根据列索引获取预读取的类型和字段名string type = variableTypes[col - 1]; // 第1列对应variableTypes[0] = "int"string name = variableNames[col - 1]; // 第1列对应variableNames[0] = "id"// 转换值并存储到字典dataItem[name] = ConvertValue(type, value);}dataList.Add(dataItem); // 将当前行数据存入总列表
}

从4,1开始 匹配之前的两个List表[col -1] 然后做类型转换 最后存入字典

然后内层循环是4,2---------4,3-------4,4

之后外层循环跳跃到5,1-----5,2-----5,3......以此类推

               var worksheet = package.Workbook.Worksheets[1];if (worksheet.Dimension.Rows < 3){Debug.LogError("配置表格式错误:至少需要3行配置行(类型行和名称行)");return;}var variableTypes = new List<string>();var variableNames = new List<string>();// 读取类型和字段名for (int col = 1; col <= worksheet.Dimension.Columns; col++){variableTypes.Add(worksheet.Cells[2, col].Text.Trim());variableNames.Add(worksheet.Cells[3, col].Text.Trim());}// 获取工作表名称string sheetName = worksheet.Name;int commentIndex = sheetName.IndexOf('#');string className;if (commentIndex > 0){className = sheetName.Substring(0, commentIndex).Trim();}else{className = Path.GetFileNameWithoutExtension(excelFilePath).Replace(" ", "");}var dataList = new List<Dictionary<string, object>>();// 读取数据行for (int row = 4; row <= worksheet.Dimension.Rows; row++){var dataItem = new Dictionary<string, object>();for (int col = 1; col <= worksheet.Dimension.Columns; col++){string value = worksheet.Cells[row, col].Text;dataItem[variableNames[col - 1]] = ConvertValue(variableTypes[col - 1], value);}dataList.Add(dataItem);}

全部代码

using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using Newtonsoft.Json;
using OfficeOpenXml;public class ConfigTableTool : EditorWindow
{private string configTablePath = "D:/unitygames/GreyBox/Tools/Excel";private string exportJsonPath = "D:/unitygames/GreyBox/Assets/Model/Json";private string generateClassPath = "D:/unitygames/GreyBox/Assets/Scripts/Data/SaveJsonClass";private string[] excelFiles;private int selectedIndex = 0;private Texture2D excelIcon;private Vector2 scrollPos;private const string ConfigTablePathKey = "ConfigTableTool_ConfigTablePath";private const string ExportJsonPathKey = "ConfigTableTool_ExportJsonPath";private const string GenerateClassPathKey = "ConfigTableTool_GenerateClassPath";[MenuItem("Tools/配置表工具")]public static void ShowWindow(){GetWindow<ConfigTableTool>("配置表工具");}private void OnEnable(){configTablePath = EditorPrefs.GetString(ConfigTablePathKey, "");exportJsonPath = EditorPrefs.GetString(ExportJsonPathKey, "");generateClassPath = EditorPrefs.GetString(GenerateClassPathKey, "");// 加载Excel图标(确保图标文件存在,且设置为EditorGUI类型)excelIcon = AssetDatabase.LoadAssetAtPath<Texture2D>("Assets/JKFrame/Editor/ConfigeTableTool/icon.png");if (excelIcon == null){Debug.LogWarning("Excel图标未找到,使用默认图标");}if (!string.IsNullOrEmpty(configTablePath)){LoadExcelFiles();}}private void OnDisable(){EditorPrefs.SetString(ConfigTablePathKey, configTablePath);EditorPrefs.SetString(ExportJsonPathKey, exportJsonPath);EditorPrefs.SetString(GenerateClassPathKey, generateClassPath);}private void OnGUI(){// 路径配置区域DrawPathSetting();// Excel文件列表显示DrawExcelList();// 打开选中Excel按钮DrawOpenButton();// 底部操作按钮DrawActionButtons();EditorGUILayout.LabelField("版本: dev0.2", EditorStyles.miniLabel);}void DrawPathSetting(){configTablePath = EditorGUILayout.TextField("配置表路径", configTablePath);if (GUILayout.Button("选择配置表文件夹", GUILayout.Width(150))){var path = EditorUtility.OpenFolderPanel("选择配置表文件夹", configTablePath, "");if (!string.IsNullOrEmpty(path)){configTablePath = path;LoadExcelFiles();}Repaint();}EditorGUILayout.Space(10);exportJsonPath = EditorGUILayout.TextField("JSON导出路径", exportJsonPath);if (GUILayout.Button("选择JSON导出文件夹", GUILayout.Width(150))){var path = EditorUtility.OpenFolderPanel("选择JSON导出文件夹", exportJsonPath, "");if (!string.IsNullOrEmpty(path)){exportJsonPath = path;}Repaint();}EditorGUILayout.Space(10);generateClassPath = EditorGUILayout.TextField("类文件路径", generateClassPath);if (GUILayout.Button("选择类文件文件夹", GUILayout.Width(150))){var path = EditorUtility.OpenFolderPanel("选择类文件文件夹", generateClassPath, "");if (!string.IsNullOrEmpty(path)){generateClassPath = path;}Repaint();}EditorGUILayout.Space(20);}void DrawExcelList(){if (!Directory.Exists(configTablePath)) return;EditorGUILayout.LabelField("Excel文件列表", EditorStyles.boldLabel);using (var scrollScope = new EditorGUILayout.ScrollViewScope(scrollPos)){scrollPos = scrollScope.scrollPosition;int itemsPerRow = Mathf.FloorToInt(EditorGUIUtility.currentViewWidth / 110);int rowCount = 0;for (int i = 0; i < excelFiles.Length; i++){if (i % itemsPerRow == 0){EditorGUILayout.BeginHorizontal();rowCount++;}DrawExcelItem(i);if ((i + 1) % itemsPerRow == 0 || i == excelFiles.Length - 1){EditorGUILayout.EndHorizontal();}}}}void DrawExcelItem(int index){bool isSelected = index == selectedIndex;string fileName = Path.GetFileNameWithoutExtension(excelFiles[index]);EditorGUILayout.BeginVertical(GUI.skin.box, GUILayout.Width(100), GUILayout.Height(90));// 绘制选中背景if (isSelected){GUI.color = new Color(0.5f, 0.8f, 1f, 0.3f);GUI.DrawTexture(GUILayoutUtility.GetRect(100, 90), Texture2D.whiteTexture);GUI.color = Color.white;}// 绘制图标if (excelIcon != null && GUILayout.Button(excelIcon, GUILayout.Width(50), GUILayout.Height(50))){selectedIndex = index;}// 绘制文件名GUIStyle labelStyle = new GUIStyle(EditorStyles.miniLabel){alignment = TextAnchor.MiddleCenter,wordWrap = true};EditorGUILayout.LabelField(fileName, labelStyle);EditorGUILayout.EndVertical();}void DrawOpenButton(){EditorGUILayout.BeginHorizontal();if (GUILayout.Button("打开选中Excel", GUILayout.Height(30))){if (excelFiles != null && excelFiles.Length > 0 && selectedIndex >= 0 && selectedIndex < excelFiles.Length){OpenExcelFile(excelFiles[selectedIndex]);}}EditorGUILayout.EndHorizontal();}void DrawActionButtons(){EditorGUILayout.BeginHorizontal();if (GUILayout.Button("导出选中配置表", GUILayout.Height(30))){if (excelFiles != null && excelFiles.Length > 0){ExportSingleExcel(excelFiles[selectedIndex]);}}if (GUILayout.Button("导出全部配置表", GUILayout.Height(30))){if (excelFiles != null && excelFiles.Length > 0){foreach (var file in excelFiles){ExportSingleExcel(file);}}}EditorGUILayout.EndHorizontal();}void LoadExcelFiles(){if (!string.IsNullOrEmpty(configTablePath)){excelFiles = Directory.GetFiles(configTablePath, "*.xlsx");selectedIndex = 0;}}void ExportSingleExcel(string excelFilePath){try{if (!File.Exists(excelFilePath)){Debug.LogError($"配置表文件不存在: {excelFilePath}");return;}using (var package = new ExcelPackage(new FileInfo(excelFilePath))){var worksheet = package.Workbook.Worksheets[1];if (worksheet.Dimension.Rows < 3){Debug.LogError("配置表格式错误:至少需要3行配置行(类型行和名称行)");return;}var variableTypes = new List<string>();var variableNames = new List<string>();// 读取类型和字段名for (int col = 1; col <= worksheet.Dimension.Columns; col++){variableTypes.Add(worksheet.Cells[2, col].Text.Trim());variableNames.Add(worksheet.Cells[3, col].Text.Trim());}// 获取工作表名称string sheetName = worksheet.Name;int commentIndex = sheetName.IndexOf('#');string className;if (commentIndex > 0){className = sheetName.Substring(0, commentIndex).Trim();}else{className = Path.GetFileNameWithoutExtension(excelFilePath).Replace(" ", "");}var dataList = new List<Dictionary<string, object>>();// 读取数据行for (int row = 4; row <= worksheet.Dimension.Rows; row++){var dataItem = new Dictionary<string, object>();for (int col = 1; col <= worksheet.Dimension.Columns; col++){string value = worksheet.Cells[row, col].Text;dataItem[variableNames[col - 1]] = ConvertValue(variableTypes[col - 1], value);}dataList.Add(dataItem);}// 生成JSONstring jsonFileName = Path.GetFileNameWithoutExtension(excelFilePath) + ".json";string jsonPath = Path.Combine(exportJsonPath, jsonFileName);// 添加类名元数据var jsonWithMetadata = new Dictionary<string, object>{{ "className", className },{ "data", dataList }};File.WriteAllText(jsonPath, JsonConvert.SerializeObject(jsonWithMetadata, Formatting.Indented));Debug.Log($"✅ JSON 导出成功: {jsonPath}");// 生成C#类if (!string.IsNullOrEmpty(generateClassPath)){string classPath = Path.Combine(generateClassPath, className + ".cs");string classCode = GenerateClassCode(className, variableTypes, variableNames);File.WriteAllText(classPath, classCode);Debug.Log($"✅ C#类生成成功: {classPath}");}}AssetDatabase.Refresh();}catch (Exception e){Debug.LogError($"❌ 导出失败: {excelFilePath}\n{e.Message}");}}string GenerateClassCode(string className, List<string> types, List<string> names){string code = $"// Auto Generated\n[System.Serializable]\npublic class {className} : ConfigBase\n{{\n";for (int i = 0; i < names.Count; i++){code += $"    public {ConvertTypeName(types[i])} {names[i]};\n";}code += "}";return code;}string ConvertTypeName(string typeName){return typeName switch{"float[]" => "float[]","int[]" => "int[]","string[]" => "string[]",_ => typeName};}object ConvertValue(string type, string value){if (string.IsNullOrWhiteSpace(value)) return null;try{return type switch{"int" => Convert.ToInt32(value),"float" => Convert.ToSingle(value),"bool" => Convert.ToBoolean(value),"double" => Convert.ToDouble(value),"string" => value,"int[]" => Array.ConvertAll(value.Split(','), int.Parse),"float[]" => Array.ConvertAll(value.Split(','), float.Parse),"string[]" => value.Split(','),_ => value};}catch{Debug.LogError($"类型转换失败:{type} <- {value}");return null;}}void OpenExcelFile(string filePath){try{UnityEngine.Debug.Log($"尝试打开文件: {filePath}");System.Diagnostics.Process.Start(filePath);}catch (Exception ex){UnityEngine.Debug.LogError($"无法打开Excel文件: {filePath} - {ex.Message}");}}
}


http://www.ppmy.cn/ops/166878.html

相关文章

深度学习有哪些算法?

深度学习包含多种算法和模型&#xff0c;广泛应用于图像处理、自然语言处理、语音识别等领域。以下是主要分类及代表性算法&#xff1a; 一、基础神经网络 多层感知机&#xff08;MLP&#xff09; 最简单的深度学习模型&#xff0c;由多个全连接层组成&#xff0c;用于分类和回…

印刷店常用的PDF批量页码统计软件

参考原文&#xff1a;印刷店常用的PDF批量页码统计软件 支持在不打开文件的情况下批量统计出PDF文档的页码数&#xff0c;还可以协助计算出打印费。 针对统计出错的文档&#xff0c;还可以筛选出来。 需要支持的使用环境&#xff01; 支持Windows10以上环境可以直接运行&…

DeepSeek大模型在政务服务领域的应用

DeepSeek大模型作为国产人工智能技术的代表&#xff0c;近年来在政务服务领域的应用呈现多点开花的态势。通过多地实践&#xff0c;该技术不仅显著提升了政务服务的效率与智能化水平&#xff0c;还推动了政府治理模式的创新。以下从技术应用场景、典型案例及发展趋势三个维度进…

QAI AppBuilder 快速上手(7):目标检测应用实例

YOLOv8_det是YOLO 系列目标检测模型&#xff0c;专为高效、准确地检测图像中的物体而设计。该模型通过引入新的功能和改进点&#xff0c;如因式分解卷积&#xff08;factorized convolutions&#xff09;和批量归一化&#xff08;batch normalization&#xff09;&#xff0c;在…

python爬虫笔记(一)

文章目录 html基础标签和下划线无序列表和有序列表表格加边框 html的属性a标签&#xff08;网站&#xff09;target属性换行线和水平分割线 图片设置宽高width&#xff0c;height html区块——块元素与行内元素块元素与行内元素块元素举例行内元素举例 表单from标签type属性pla…

ImGui 学习笔记(五) —— 字体文件加载问题

ImGui 加载字体文件的函数似乎存在编码问题&#xff0c;这一点可能跟源文件的编码也有关系&#xff0c;我目前源文件编码是 UTF-16。 当参数中包含中文字符时&#xff0c;ImGui 内部将字符转换为宽字符字符集时候&#xff0c;采用的 MultiByteToWideChar API 参数不太对&#…

Java 的 CopyOnWriteArrayList 和 Collections.synchronizedList 有什么区别?分别有什么优缺点?

CopyOnWriteArrayList 问题:什么是 CopyOnWriteArrayList? 解答: CopyOnWriteArrayList 是 Java 并发包 (java.util.concurrent) 提供的一种 线程安全 的 List 实现,它的核心特性是 写时复制(Copy-On-Write)。即,每当执行修改操作(如 add()、set()、remove())时,都…

CSS中z-index使用详情

定位层级 1.定位元素的显示层级比普通元素高,无论什么定位,显示层级都是一样的; 2.如果位置发生重叠,默认情况是:后面的元素,会显示在前面元素之上; 3.可以通过CSS属性z-index调整元素的显示层级; 4.z-index的属性值是数字,没有单位,值越大显示层级越高; 5.只有定位的元素…