Unity——利用GUUI制作通过外部数据自动生成表格

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:运行的结果如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Laker404

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值