0 前言
Excel数据处理是游戏开发中很常见的一个问题,这篇文章实现了:
1.解析Excel生成对应CSharp类
2.Excel数据转Json数据
3.运行时读取Json数据存入字典
Excel为什么要转Json(我自己的理解):
1)方便运行时的数据读取(Json转自定义类型有插件方法可以用,很方便)
2)出于性能考虑
需要插件:
可以在网上找找,应该都有
1 效果预览
使用
Excel配置
使用方法
右键Excel文件或者在右键文件夹(转换文件夹下的所有Excel文件)
结果
CSharp类
JsonData
打印
2 代码
ExcelConfig.cs
这个脚本主要就是配置一些路径信息。
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class ExcelConfig
{
private static string excelDataPath = "/Excel";
public static string ExcelDataPath {
get {
var path = Application.dataPath + excelDataPath;
if (!Directory.Exists(path)) {
Directory.CreateDirectory(path);
}
return excelDataPath;
}
}
private static string jsonDataPath = "/Scripts/Excel/JsonData";
public static string JsonDataPath {
get {
var path = Application.dataPath + jsonDataPath;
if (!Directory.Exists(path)) {
Directory.CreateDirectory(path);
}
return jsonDataPath;
}
}
private static string cSharpPath = "/Scripts/Excel/CSharp";
public static string CSharpPath {
get {
var path = Application.dataPath + cSharpPath;
if (!Directory.Exists(path)) {
Directory.CreateDirectory(path);
}
return cSharpPath;
}
}
}
ExcelToCSharpClass.cs
这个脚本实现了转换。
using Excel;
using Newtonsoft.Json;
using OfficeOpenXml;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
public class ExcelToCSharpClass
{
[MenuItem("Assets/ExcelTool/ExcelToCSharpClass")]
public static void ExcelToCSharpClassMethod() {
var selectedObject = Selection.activeObject;
if (selectedObject == null) {
Debug.LogError