我們都知道Unity有自帶的類textAsset可以簡(jiǎn)單地讀取Text文本的內(nèi)容。但在實(shí)際的開發(fā)過(guò)程中,我們不可避免地會(huì)與excel 或者 json這些文件類型打交道,今天也是花了點(diǎn)時(shí)間,整理出來(lái)了如何簡(jiǎn)單地實(shí)現(xiàn)讀取excel文檔的功能。
github地址:github項(xiàng)目地址
本人個(gè)人博客:wyryyds.github.io
首先我們先導(dǎo)入三個(gè)拓展庫(kù)。存放在文件夾Plugins(自建)下面。
鏈接: https://pan.baidu.com/s/1jRSOjiDvdoNyF0eSezz6Kw?pwd=twtn 提取碼: twtn
我們先自定義兩個(gè)類。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Data
{
[System.Serializable]
public class Item
{
public uint itemId; //uint為無(wú)符號(hào)整型。
public string itemName;
public uint itemPrice;
}
public class ItemManager : ScriptableObject
{
public Item[] dataArray;
}
}
讓itemManager繼承自Unity的ScriptableObject,方便我們?cè)诤竺嬲{(diào)用它的方法。
再新建一個(gè)腳本,處理我們的excel文件。
先定義一個(gè)類,來(lái)獲取兩個(gè)文件夾路徑名。一個(gè)是我們的excel文件的路徑,一個(gè)是我們要生成的item的路徑名。方便我們?cè)诤罄m(xù)的操作中更直觀。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-507057.html
public class ExcelConfig
{
public static readonly string excelsFolderPath = Application.dataPath + "/Excels/";
public static readonly string assetPath = "Assets/Resources/DataAssets/";
}
接著我們定義一個(gè)類,編寫讀取excel的函數(shù),跟將數(shù)據(jù)轉(zhuǎn)換到item的函數(shù)。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-507057.html
public class Excel_Tool
{
static DataRowCollection ReadExcel(string filePath, ref int columnNum, ref int rowNum)//使用關(guān)鍵字ref引用傳值
{
FileStream stream = File.Open(filePath, FileMode.
到了這里,關(guān)于Unity實(shí)現(xiàn)讀取Excel文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!