Unity编译器 《策划表编表工具》 Excel转Json自动生成解析c#代码
1.表格数据结构解释
2.Excel文件路径
3.C#代码与Json自动生成路径
点击下载依赖Dll程序集
4.下载好的依赖程序集放到Plugins中
5.Unity中整体结构
6.Tools 工具代码
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
using Excel;
using LitJson;
public class Tools : EditorWindow
{
/// <summary>
/// Execlw文件夹路径
/// </summary>
public static string ExeclPath = Application.dataPath.Replace("Assets", "Excel");
/// <summary>
/// json路径
/// </summary>
public static string jsonPath = Application.dataPath + "/Resources/TxT/";
/// <summary>
/// 创建的解析脚本CSharp路径
/// </summary>
public static string CSharpPath = Application.dataPath + "/Script/Auto/TableAuto/";
/// <summary>
/// table表管理类路径
/// </summary>
public static string CSharptablePath = Application.dataPath + "/Script/Table/";
/// <summary>
/// Execl To Json入口
/// </summary>
[MenuItem("Tools / Execl To Json and C#")]
public static void StartExelToJson()
{
if (Directory.Exists(ExeclPath))
{
//文件路径
DirectoryInfo directoryInfo = new DirectoryInfo(ExeclPath);
//拿到所有的Exel路径
FileInfo[] FilesAll = directoryInfo.GetFiles("*.xlsx");
//循环序列化ExeclPath文件夹下所有表
for (int i = 0; i < FilesAll.Length; i++)
{
string ExcelTempPath = FilesAll[i].ToString();
Debug.Log(" Excel绝对路径: " + ExcelTempPath);
//构造Excel工具类
ExcelUtility excel = new ExcelUtility(ExcelTempPath);
excel.ConvertToJson(jsonPath, Encoding.GetEncoding("utf-8"), CSharpPath);
//刷新本地资源
AssetDatabase.Refresh();
}
//创建TableManager脚本
if (!Directory.Exists(CSharptablePath))
{
// 如果路径不存在,则创建文件夹
Directory.CreateDirectory(CSharptablePath);
Console.WriteLine("文件夹已创建!");
}
CreatTableManager(FilesAll);
//刷新本地资源
AssetDatabase.Refresh();
}
else
{
Debug.LogError("未找到路径: " + ExeclPath);
}
#region 创建Table管理类
/// <summary>
/// 创建Table管理类
/// </summary>
void CreatTableManager(FileInfo[] FilesAll)
{
StringBuilder scriptStr = new StringBuilder();
scriptStr.AppendLine("using System.Collections.Generic;");
scriptStr.AppendLine("using UnityEngine;");
scriptStr.AppendLine("public class TableManager");
scriptStr.AppendLine("{");
scriptStr.AppendLine(" private static TableManager _instance;");
scriptStr.AppendLine(" public static TableManager Instance");
scriptStr.AppendLine(" {");
scriptStr.AppendLine(" get");
scriptStr.AppendLine(" {");
scriptStr.AppendLine(" if (_instance == null)");
scriptStr.AppendLine(" {");
scriptStr.AppendLine(" _instance = new TableManager();");
scriptStr.AppendLine(" }");
scriptStr.AppendLine(" return _instance;");
scriptStr.AppendLine(" }");
scriptStr.AppendLine(" }");
foreach (var info in FilesAll)
{
string tampName= Path.GetFileNameWithoutExtension(info.Name);
scriptStr.AppendLine($" public {
tampName}InfoTable {
tampName}Info;");
}
scriptStr.AppendLine(