1:导入Excel的DLL文件,网上有很多
这些注册文件用于读取Excel文件
2:创建一个Excel文件,格式如下
3:把Excel文件放到SteamingAssets文件夹下
创建一个解析类用于解析Excel文件
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Data
{
/// <summary>
/// 存储数据类型的基类
/// </summary>
[Serializable]
public class Item
{
public string Name;
public string Class;
public string Sex;
public string Age;
}
/// <summary>
/// ScriptableObject是 Unity 提供的一个数据配置存储基类,它是一个可以用来保存大量数据的数据容器,我们可以将它保存为自定义的数据资源文件
/// ScriptableObject 类的实例会被保存成资源文件(.asset文件),和预制体,材质球,音频文件等类似,都是一种资源文件,存放在 Assets 文件夹下,创建出来的实例也是唯一存在的。
/// </summary>
public class ItemManager : ScriptableObject
{
public Item[] dataArray;
}
}
4:然后直接加载该Excel文件,主要是读取,然后生成相应内容,存储到数据类
using Excel;
using Data;
using System.IO;
using System.Data;
using UnityEngine;
using UnityEditor;
public class ExcelTool : MonoBehaviour
{
public class ExcelConfig
{
/// <summary>
/// 存放excel表格文件夹的路径
/// </summary>
public static readonly string excelFolderPath = Application.streamingAssetsPath + "/ExcelData/";
/// <summary>
/// 存放Excel转化为CS文件的文件夹路径
/// </summary>
public static readonly string assetPath = "Assets/Resources/DataAssets/";
}
/// <summary>
/// 读取表中的数据,生成数组
/// </summary>
public class Excel_Tool
{
static DataRowCollection ReadExcel(string filePath, ref int columnNum, ref int rowNum)
{
//FileStream是基于IO名称空间
FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
//IExcelDataReader是基于Excel名称空间
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
DataSet result = excelReader.AsDataSet();
//Table[0]表示的是文件中第一张表的数据
columnNum = result.Tables[0].Columns.Count;
rowNum = result.Tables[0].Rows.Count;
return result.Tables[0].Rows;
}
public static Item[] CreateItemArrayWithExcel(string filePath)
{
//获取表中的数据
int columnNum = 0, rowNum = 0;
DataRowCollection collection = ReadExcel(filePath, ref columnNum, ref rowNum);
Item[] array = new Item[rowNum - 1];
for (int i = 1; i < rowNum; i++)
{
Item item = new Item();
item.Name = collection[i][0].ToString();
item.Class = collection[i][1].ToString();
item.Sex = collection[i][2].ToString();
item.Age = collection[i][3].ToString();
array[i-1] = item;
}
return array;
}
}
public class ExcelBuild : Editor
{
[MenuItem("CustomEditor/CreateItemAsset")]
public static void CreateItemAsset()
{
ItemManager manager = ScriptableObject.CreateInstance<ItemManager>();
manager.dataArray = Excel_Tool.CreateItemArrayWithExcel(ExcelConfig.excelFolderPath + "Item.xlsx");
//确保路径中有这个文件夹,Directory
if (!Directory.Exists(ExcelConfig.assetPath))
{
Directory.CreateDirectory(ExcelConfig.assetPath);
}
//asset文件的路径 要以"Assets/..."开始,否则CreateAsset会报错
string assetPath = string.Format("{0}{1}.asset", ExcelConfig.assetPath, "Item");
//生成一个Asset文件
AssetDatabase.CreateAsset(manager, assetPath);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
}
}
5:最后在页面上搭好UI,等待读取就好了
using TMPro;
using UnityEngine.UI;
using UnityEngine;
using System.Collections.Generic;
using Data;
public class ExcelGenerate : MonoBehaviour
{
private TMP_Text myText;
List<GameObject> contents = new List<GameObject>();
public GameObject prefab;
public GameObject parent;
private void Start()
{
//读取生成好的Item文件内容
ItemManager man = Resources.Load<ItemManager>("DataAssets/Item");
for (int i = 0; i < 4; i++)
{
GameObject item = Instantiate(prefab, parent.transform);
//将预制体加入列表
contents.Add(item);
//数据赋值
contents[i].transform.GetChild(0).GetComponent<TMP_Text>().text = man.dataArray[i].Name;
contents[i].transform.GetChild(1).GetComponent<TMP_Text>().text = man.dataArray[i].Class;
contents[i].transform.GetChild(2).GetComponent<TMP_Text>().text = man.dataArray[i].Sex;
contents[i].transform.GetChild(3).GetComponent<TMP_Text>().text = man.dataArray[i].Age;
}
}
}
6:运行的结果如下: