Unity实现读取Excel文件

我们都知道Unity有自带的类textAsset可以简单地读取Text文本的内容。但在实际的开发过程中,我们不可避免地会与excel 或者 json这些文件类型打交道,今天也是花了点时间,整理出来了如何简单地实现读取excel文档的功能。

github地址:github项目地址

本人个人博客:wyryyds.github.io

首先我们先导入三个拓展库。存放在文件夹Plugins(自建)下面。

链接: https://pan.baidu.com/s/1jRSOjiDvdoNyF0eSezz6Kw?pwd=twtn 提取码: twtn

我们先自定义两个类。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Data
{
	[System.Serializable]
	public class Item
	{
		public uint itemId;  //uint为无符号整型。
		public string itemName;
		public uint itemPrice;
	}
	public class ItemManager : ScriptableObject
	{
		public Item[] dataArray;
	}
}

让itemManager继承自Unity的ScriptableObject,方便我们在后面调用它的方法。

再新建一个脚本,处理我们的excel文件。

先定义一个类,来获取两个文件夹路径名。一个是我们的excel文件的路径,一个是我们要生成的item的路径名。方便我们在后续的操作中更直观。

public class ExcelConfig
	{
		public static readonly string excelsFolderPath = Application.dataPath + "/Excels/";

		public static readonly string assetPath = "Assets/Resources/DataAssets/";
	}

接着我们定义一个类,编写读取excel的函数,跟将数据转换到item的函数。

 public class Excel_Tool
    {
        static DataRowCollection ReadExcel(string filePath, ref int columnNum, ref int rowNum)//使用关键字ref引用传值
        {
            FileStream stream = File.Open(filePath, FileMode.
  • 4
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

武田晴海

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

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

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

打赏作者

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

抵扣说明:

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

余额充值