Unity编辑Excel一键数据转换,转json或bytes

本文介绍了一款用于Unity项目的Excel数据导入工具,该工具能够将Excel文件转换为Unity可读的JSON或字节数据,并自动生成对应的C#脚本来解析这些数据。此工具通过使用Excel.dll和ICSharpCode.SharpZipLib.dll库实现,适用于游戏开发和数据管理。

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

需要用到Excel.dll和ICSharpCode.SharpZipLib.dll

表格示例:
shili

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Text;
using Excel;
using System.Reflection;
using System.Reflection.Emit;
using System;

public class ExcelTool : Editor
{
   
   
    public class ExcelClassData
    {
   
   
        public string className; //类名
        public string[] infos;    //注释
        public string[] types;    //类型
        public string[] propertyName;    //属性名
        public List<string[]> datas; //数据
    }
    //excel文件夹
    const string EXCEL_PATH = "ExcelData/Excel";
    
    [MenuItem("ExcelTool/生成数据")]
    static void ExcelToData()
    {
   
   
        //所有xlsx表格
        string[] files = Directory.GetFiles(Application.dataPath + "/" + EXCEL_PATH, "*.xlsx");
        //Debug.Log(files.Length);

        //所有数据信息
        List<ExcelClassData> allClassDatas = new List<ExcelClassData>();

        for (int i = 0; i < files.Length; i++)
        {
   
   
            string file = files[i];
            FileStream fileStream = File.Open(file, FileMode.Open, FileAccess.Read);
            IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(fileStream);
            if (!excelReader.IsValid)
            {
   
   
                Debug.Log("读取excel失败" + file);
                continue;
            }

            //构建数据
            ExcelClassData exdata = new ExcelClassData();
            exdata.className = excelReader.Name;
            exdata.datas = new List<string[]>();

            int line = 1;
            while (excelReader.Read())
            {
   
   
                //一行数据
                string[] strLineDatas = new string[excelReader.FieldCount];
                for (int j = 0; j < strLineDatas.Length; j++)
                {
   
   
                    strLineDatas[j] = excelReader.GetString(j);
                }

                ////空行处理
                //if (string.IsNullOrEmpty(strLineDatas[0]))
                //{
   
   
                //    continue;
                //}

                //注释行
                if (line == 2)
                {
   
   
                    exdata.infos = strLineDatas;
                }
                //类型行
                if (line == 3)
                {
   
   
                    exdata.types = strLineDatas;
                }
                //属性名行
                if (line == 4)
                {
   
   
                    exdata.propertyName = strLineDatas;
                }
                //数据行
                if(line > 4)
                {
   
   
                    exdata.datas.Add(strLineDatas);
                }

                line++;

            }

            allClassDatas.Add(exdata);

        }

        //写出数据和脚本操作
        Writer(allClassDatas);

        Debug.Log("----->excel datas trans finish!");

        AssetDatabase.Refresh();
    }


    static void Writer(List<ExcelClassData> exDataList)
    {
   
   
        #region//---json---
        //AssemblyName assemblyName = new AssemblyName("dynamicAssembly");
        //AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
        //ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyName.Name);

        //for (int i = 0; i < exDataList.Count; i++)
        //{
   
   
        //    ExcelClassData exData = exDataList[i];
        //    //定义类型
        //    TypeBuilder typeBuilder = moduleBuilder.DefineType(exData.className, TypeAttributes.Public);
        //    //定义属性
        //    for (int j = 0; j < exData.types.Length; j++)
        //    {
   
   
        //        typeBuilder.DefineField(exData.propertyName[j], GetType(exData.types[j]), FieldAttributes.Public);
        //    }
        //    //t
        //    Type t = typeBuilder.CreateType();

        //    List<object> allObjList = new List<object>();
        //    for (int j = 0; j < exData.datas.Count; j++)
        //    {
   
   
        //        //一行数据
        //        string[] strDatas = exData.datas[j];
        //        //反射实例
        //        object obj = Activator.CreateInstance(t);
        //        for (int k = 0; k < exData.types.Length; k++)
        //        {
   
   
        //            //设置属性值
        //            FieldInfo fieldInfo = t.GetField(exData.propertyName[k]);
        //            object value = GetValue(exData.types[k], strDatas[k]);
        //            fieldInfo.SetValue(obj, value);
        //        }
        //        allObjList.Add(obj);
        //    }
        //    string jsonData = Newtonsoft.Json.JsonConvert.SerializeObject(allObjList, Newtonsoft.Json.F

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值