Tz2/Assets/Editor/ExcelBuild.cs

162 lines
6.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Data;
using System.IO;
using System.Text;
using Excel;
using UnityEditor;
using Data;
using UnityEngine.UI;
using System;
public class ExcelBuild : Editor
{
public 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);
//Debug.Log(stream);
DataSet result = excelReader.AsDataSet();
//Debug.Log(result);
// 获取第一张表的数据
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 Item[] CreateItemArrayWithExcel(string filePath)
{
int columnNum = 0, rowNum = 0;
DataRowCollection collect = ReadExcel(filePath, ref columnNum, ref rowNum);
// 根据excel的定义第二行开始才是数据因此这里需要从1开始
//Item[] array = new Item[rowNum - 1]; // 减去第一行(标题行)
//for (int i = 1; i < rowNum; i++) // 从1开始跳过标题行
Item[] array = new Item[rowNum]; // 减去第一行(标题行)
for (int i = 0; i < rowNum; i++) // 从1开始跳过标题行
{
Item item = new Item();
item.itemData = new List<ItemData>();
// 解析每列的数据
try
{
for (int j = 0; j < columnNum; j++)
{
//Debug.Log("i:"+i+" j:"+j+" value:"+collect[i][j].ToString());
ItemData itemData = new ItemData();
// 这里可以根据实际情况解析每列的数据类型比如int、float、string等
// 这里假设每列都是string类型
itemData.item = collect[i][j].ToString();
item.itemData.Add(itemData);
}
}
catch (FormatException e)
{
// 如果解析失败,输出错误信息
Debug.LogError("解析Excel数据时发生格式错误: " + e.Message);
// 并且返回null数组或者你可以选择跳过错误行继续解析
return null;
}
array[i] = item; // 将解析好的item放入数组中注意索引从i-1开始
}
return array;
}
[MenuItem("CustomEditor/CreateItemAsset")]
public static void CreateItemAsset()
{
ItemManager manager = ScriptableObject.CreateInstance<ItemManager>();
// 赋值
Item[] items = CreateItemArrayWithExcel(Application.dataPath + "/Excels/" + "biao01.xlsx");
if (items != null)
{
manager.dataArray = items;
}
else
{
Debug.LogError("无法创建Item Asset因为CreateItemArrayWithExcel返回了null");
return;
}
// 确保文件夹存在
if (!Directory.Exists("Assets/Resources/DataAssets/"))
{
Directory.CreateDirectory("Assets/Resources/DataAssets/");
}
// asset文件的路径 要以"Assets/..."开始否则CreateAsset会报错
string assetPath = string.Format("{0}{1}.asset", "Assets/Resources/DataAssets/", "Item");
// 生成一个Asset文件
AssetDatabase.CreateAsset(manager, assetPath);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
[MenuItem("GameObject/MyMenu/SaveExcelDataToUI")]
public static void SaveExcelDataToUI()
{
int columnNum = 0, rowNum = 0;
FileStream stream = File.Open(Application.dataPath + "/Excels/" + "biao01.xlsx", FileMode.Open, FileAccess.Read, FileShare.Read);
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
DataSet result = excelReader.AsDataSet();
// 获取第一张表的数据
columnNum = result.Tables[0].Columns.Count;
rowNum = result.Tables[0].Rows.Count;
DataRowCollection collect = result.Tables[0].Rows;
if (Selection.activeGameObject)
{
GameObject inputObj = null;
GameObject rowObj = null;
// 查找子对象中的rowObj和inputObj
foreach (var item in Selection.activeGameObject.transform.GetComponentsInChildren<Transform>(true))
{
if (item.name == "rowObj")
{
rowObj = item.gameObject;
}
else if (item.name == "inputObj")
{
inputObj = item.gameObject;
}
}
Transform rowParent = Selection.activeGameObject.transform;
Transform columnParent = null;
Transform columnItem = null;
string content;
for (int i = 0; i < rowNum; i++) // 从1开始跳过标题行
{
columnParent = (Instantiate(rowObj) as GameObject).transform;
columnParent.name = (i).ToString();
columnParent.gameObject.SetActive(true);
columnParent.parent = rowParent;
columnParent.localScale = Vector3.one;
columnParent.localPosition = new Vector3(0, -29 * (i - 1), 0); // 调整位置计算,以适应跳过的标题行
columnParent.localEulerAngles = Vector3.zero;
for (int j = 0; j < columnNum; j++)
{
columnItem = ((Instantiate(inputObj)) as GameObject).transform;
columnItem.gameObject.SetActive(true);
columnItem.parent = columnParent;
columnItem.localScale = Vector3.one;
content = collect[i][j].ToString();
columnItem.GetComponent<InputField>().text = content;
}
}
rowParent.GetComponent<RectTransform>().sizeDelta = new Vector2(columnParent.GetComponent<RectTransform>().sizeDelta.x, (28 * (rowNum)));
}
}
}