Unity版本 2019.4.9f1
准备工作
  1. 安装Excel的拓展库,百度网盘提取码1234
  2. 新建一个Plugins文件夹,将下载的拓展库放进去。
  3. 创建资源文件夹
开始扣代码
1. 创建基本数据类型

ExItem可以根据自己后期需要定义多个类型使用

using System.Collections.Generic;
using UnityEngine;namespace Data
{[System.Serializable]public class ExItem{public uint itemId;  // uint为无符号整型// 可以自定义所需数据的类型// public int itemID;// public string itemName;public List<string> itemData;}public class ItemManager : ScriptableObject{public ExItem[] dataArray;}public class ExCell{public string name;public string info;}
}
2. 创建地址类型
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class ExcelConfig
{/// <summary>/// 存放excel表文件夹的的路径/// </summary>public static readonly string excelPath = Application.dataPath + "/Resources/Config/Excel/";/// <summary>/// 存放Excel转化的Assest文件的文件夹路径/// </summary>public static readonly string assetPath = "Assets/Resources/Config/AssetsFile/";/// <summary>/// 存放Excel转化的xml文件的文件夹路径/// </summary>public static readonly string xmlPath = "Assets/Resources/Config/Xml/";
}
3.工具代码
using System.Collections.Generic;
using System.Data;
using System.IO;
using Excel;
using Data;
using System;public class ExcelTool
{static DataRowCollection ReadExcel(string filePath, ref int columnNum, ref int rowNum){FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);DataSet result = excelReader.AsDataSet();//Tables[0] 下标0表示excel文件中第一张表的数据columnNum = result.Tables[0].Columns.Count;rowNum = result.Tables[0].Rows.Count;return result.Tables[0].Rows;}/// <summary>/// 读取表数据,生成对应的数组/// </summary>/// <param name="filePath">excel文件全路径</param>/// <returns>Item数组</returns>public static ExItem[] CreateItemArrayWithExcel(string filePath){//获得表数据int columnNum = 0, rowNum = 0;DataRowCollection excelData = ReadExcel(filePath, ref columnNum, ref rowNum);//根据excel的定义,第二行开始才是数据ExItem[] array = new ExItem[rowNum - 1];for (int i = 1; i < rowNum; i++){ExItem item = new ExItem();//解析每列的数据item.itemId = uint.Parse(excelData[i][0].ToString());item.itemData = new List<string>();for (int j = 0; j < columnNum; j++){item.itemData.Add(Convert.ToString(excelData[i][j]));}array[i - 1] = item;}return array;}public static List<List<ExCell>> ReadExcelInfo(string filePath){// 获取表格情况 columnNum竖列,rowNum横排int columnNum = 0, rowNum = 0;DataRowCollection excelData = ReadExcel(filePath, ref columnNum, ref rowNum);// Debug.Log("表格情况:" + columnNum + "/" + rowNum);List<List<ExCell>> list = new List<List<ExCell>>();for (int i = 1; i < rowNum; i++){List<ExCell> item = new List<ExCell>();for (int j = 0; j < columnNum; j++){ExCell excell = new ExCell();excell.name = Convert.ToString(excelData[0][j]);excell.info = Convert.ToString(excelData[i][j]);// Debug.Log("ID:" + itemInfo.name + "/" + itemInfo.info);item.Add(excell);}list.Add(item);}return list;}
}
4.编辑器代码

新建Editor文件夹,代码需新建在Editor文件夹里

using Data;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using UnityEditor;public class ExcelBuild : Editor
{[MenuItem("ExcelBuild/ExcelToAsset")]public static void Excel2Asset(){// 检测文件夹是否存在if (Directory.Exists(ExcelConfig.excelPath)){//List<>// 获取文件夹信息DirectoryInfo direction = new DirectoryInfo(ExcelConfig.excelPath);FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories);for (int i = 0; i < files.Length; i++){if (files[i].Name.EndsWith(".xlsx")){ExportExcelToAsset(ExcelConfig.excelPath + files[i].Name, files[i].Name.Substring(0, files[i].Name.IndexOf('.')));}}}else{// 创建文件夹Directory.CreateDirectory(ExcelConfig.excelPath);// 给点提示// throw new System.Exception("Excel文件夹为空");}// 刷新视图AssetDatabase.Refresh();}public static void ExportExcelToAsset(string path, string name){ItemManager manager = CreateInstance<ItemManager>();// 赋值manager.dataArray = ExcelTool.CreateItemArrayWithExcel(path);// 确保文件是新的if (File.Exists(ExcelConfig.assetPath + name + ".asset")){File.Delete(ExcelConfig.assetPath + name + ".asset");}//asset文件的路径 要以"Assets/..."开始,否则会报错string assetPath = string.Format("{0}{1}.asset", ExcelConfig.assetPath, name);// Debug.Log("转化的数据:" + manager.dataArray.Length);// 生成Asset文件AssetDatabase.CreateAsset(manager, assetPath);AssetDatabase.SaveAssets();AssetDatabase.Refresh();}[MenuItem("ExcelBuild/ExcelToXml")]public static void Excel2Xml(){// 检测文件夹是否存在if (Directory.Exists(ExcelConfig.excelPath)){//List<>// 获取文件夹信息DirectoryInfo direction = new DirectoryInfo(ExcelConfig.excelPath);FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories);for (int i = 0; i < files.Length; i++){if (files[i].Name.EndsWith(".xlsx")){ExportExcel2Xml(ExcelConfig.excelPath + files[i].Name, files[i].Name.Substring(0, files[i].Name.IndexOf('.')));}}}else{// 创建文件夹Directory.CreateDirectory(ExcelConfig.excelPath);}// 刷新视图AssetDatabase.Refresh();}public static void ExportExcel2Xml(string path, string name){List<List<ExCell>> list = ExcelTool.ReadExcelInfo(path);// Debug.Log("表格长度" + list.Count);string xmlPath = ExcelConfig.xmlPath + "/" + name + ".xml";// 文件夹不存在?if (!File.Exists(ExcelConfig.xmlPath)){Directory.CreateDirectory(ExcelConfig.xmlPath);// throw new System.Exception("Xml文件夹为空");}// 文件存在?if (File.Exists(xmlPath)){File.Delete(xmlPath);}// 创建xml文件XmlDocument xml = new XmlDocument();// 创建最上一层的根节点XmlElement root = xml.CreateElement("elements");for (int i = 0; i < list.Count; i++){// 创建每一行的子节点信息XmlElement element = xml.CreateElement("element");for (int j = 0; j < list[i].Count; j++){// Debug.Log("cell数量" + list[i][j].name);// 设置子节点的内容element.SetAttribute(list[i][j].name, list[i][j].info);}// 添加到最上层节点下root.AppendChild(element);}// 将根节点添加到xml里xml.AppendChild(root);// 保存xml文件xml.Save(xmlPath);}}
工具栏效果

点击工具栏的ExcelBuild->ExcelToAsset
即可生成所需的Asset文件

点击工具栏的ExcelBuild->ExcelToXml
即可生成所需的Xml文件

Unity编辑器—Excel转Xml和Asset数据相关推荐

  1. Unity 中配置文件Excel 转xml ;josn;序列化ScriptableObject及加载(最详细)。

    游戏中对策划的配置数据导入处理常用分为1.转xml或josn.2.序列化为ScriptableObject类.第一种方法游戏加载耗时一些,第二种避免了第一种方法加载缺点但内存要占用大一些.不过推荐第二 ...

  2. unity中 Excel序列化转换为Asset遇到的一些坑

    转载自:烟雨迷离半世殇 Unity实战篇:读取Excel数据并转换成Asset 本文链接:https://blog.csdn.net/qq_15020543/article/details/83098 ...

  3. Unity 之 Excel表格转换为Unity用的文件格式 -- ScriptableObject,Json,XML 全部搞定

    Unity 之 Excel表格转换为Unity用的文件格式 -- ScriptableObject,Json,XML 全部搞定 前言 一,准备工作 1.1 确认表格表头 1.2 读取Excel 1.3 ...

  4. 用unity读取excel中的数据并绘制表格

    首先要感谢王王王渣渣这位大佬,把插件和制作步骤写的很清楚,原文链接请参考 https://blog.csdn.net/wangjiangrong/article/details/79980447 在此 ...

  5. Java 使用poi导入excel,结合xml文件进行数据验证的例子(增加了jar包)

    ava 使用poi导入excel,结合xml文件进行数据验证的例子(增加了jar包) 假设现在要做一个通用的导入方法: 要求: 1.xml的只定义数据库表中的column字段,字段类型,是否非空等条件 ...

  6. python 读取excel格式xml,读取xml格式的xls文件、解析其中数据

    1.python 读取excel格式xml,解析其中数据 当excel文件的格式是xml的时候,window系统是可以正常打开的,但是使用pandas直接读取则会报错,原因就是现在已经是xml文件了, ...

  7. unity向服务器发送xml文件格式,Unity读取Excel文件转换XML格式文件的方法

    Unity读取Excel文件转换XML格式文件的方法 发布时间:2020-06-23 09:34:33 来源:亿速云 阅读:107 作者:清晨 不懂Unity读取Excel文件转换XML格式文件的方法 ...

  8. unity读取excel表格数据

    unity读取excel表格需要引用excel.dll文件,下载地址:https://download.csdn.net/download/ThreePointsHeat/12859664 打包的时候 ...

  9. unity读取excel数据并绘制曲线

    一.读取数据 1.导入EPPlus类库:EPPlus.dll 2.创建script脚本 3.创建空物体,挂载脚本 using System.Collections; using System.Coll ...

最新文章

  1. 让你的网站支持 Emoji
  2. Apache Spark学习:利用Scala语言开发Spark应用程序
  3. Linux服务-Samba文件服务器部署
  4. @echo off是什么意思_为什么执行自己的程序要在前面加./
  5. css样式图片、渐变、相关小知识
  6. vaps 程序直接通信
  7. R实例:根据经纬度坐标批量返回行政区域信息
  8. Bzoj 3226: [Sdoi2008]校门外的区间
  9. 从零开始搭二维激光SLAM --- 基于gtsam的后端优化的代码实现
  10. PowerAI DDL
  11. 针对口令的暴力破解攻击方式
  12. 算法第四版 课后习题答案
  13. 浅谈各种常见的芯片封装技术DIP/SOP/QFP/PGA/BGA
  14. String常用方法汇总
  15. Vue+Element-ui实现考试检查答题(判断正确答题,错答,漏答)
  16. ValueError: `generator` yielded an element of shape (2,) where an element of shape (?, ?) was expect
  17. NOI 2017 滚粗记
  18. 编写python代码实现打开并登录网页、对网页进行点击、输入信息等操作
  19. vb.net 获取系统图标_「快捷指令」桌面图标任意摆放
  20. C# 25. 获取windows串口号对应的串口(设备)名称

热门文章

  1. 13种图片后期美化处理LR预设
  2. 【踩坑】cat3.x服务端部署, springboot客户端接入
  3. 翻译ESSumm: Extractive Speech Summarization from Untranscribed Meeting
  4. 解决Vue中的生命周期beforeDestory不触发的问题(用了keep-alive)
  5. python数据分析之卡方检验、T检验、方差分析
  6. 打开共享计算机很慢,局域网中,查看一台电脑的共享文件夹,打开很慢(电脑达人进)...
  7. android xml apk jar反编译工具包
  8. 【LEDE】x86软路由之路-03-挂载移动硬盘?
  9. 德睿多媒体信息发布系统
  10. NetBIOS主机名扫描工具nbtscan