Unity 读取CSV与Excel

  前几天看到我们在游戏中需要动态加载某些角色的游戏策划值,关于这个问题怎么解决呢?其实办法很多种,归根到底,就是数据的读取。我们可以想到的存储数据的载体有很多。例如:txt,xml,csv,excel。甚至可以使用Sqlite,Mysql,Sqlserver等!这都不是问题!那么我们今天学习下CSV文件和Excel的读取。废话不多说了,开始了!

1.建个空的项目!

2 建议test.csv的文件并录入数据。

录入数据:

3,读取csv文件。

方法一:将CSV文件强制转换为txt格式,在Unity中使用TextAsset直接读取文本信息。

using UnityEngine;
using System.Collections;
using System.IO;

public class read : MonoBehaviour
{
    public TextAsset txtCSV;
    public GUIText guitext;
    void Start()
    {
        guitext.text = txtCSV.text;
        print(txtCSV.text);

    }
}

运行结果:

第一种方式很简单,那么第二种方式读取,添加脚本 test.cs

using UnityEngine;
using System.Collections;
using System.IO;

public class read : MonoBehaviour
{public GUIText guitext;
    void Start()
    {
        readCSV();
    }
    /// <summary>
    /// 读取CSV文件
    /// </summary>
    void readCSV()
    {
        //读取csv二进制文件
        TextAsset binAsset = Resources.Load("csv", typeof(TextAsset)) as TextAsset;
        //显示在GUITexture中
        guitext.text = binAsset.text;

        string[] data = binAsset.text.Split("|"[0]);
        foreach (var dat in data)
        {
            Debug.Log(dat);
        }

        ////读取每一行的内容
        string[] lineArray = binAsset.text.Split("\r"[0]);
        ////按‘|’进行拆分
        string[] lineArray1 = binAsset.text.Split("|"[0]);

        //创建二维数组
        string[][] Array = new string[lineArray.Length][];

        //把csv中的数据储存在二位数组中
        for (int i = 0; i < lineArray.Length; i++)
        {
            Array[i] = lineArray[i].Split("\r"[0]);
            Debug.Log(Array[i][0].ToString());

        }
    }
}

运行结果:

OK 文本方式读取就结束了。下面进行Excel的读取。

需要插件: 稍后共享!

 1 using UnityEngine;
 2 using System.Collections;
 3 using System;
 4 using System.Collections.Generic;
 5 using System.Linq;
 6 using System.Text;
 7 using System.Text.RegularExpressions;
 8 using System.IO;
 9 using Excel;
10 using System.Data;
11 using UnityEngine.UI;
12
13 public class NewBehaviourScript : MonoBehaviour
14 {
15     public Text readData;
16     void Start ()
17     {
18         XLSX();
19     }
20
21     void XLSX()
22     {
23         FileStream stream = File.Open(Application.dataPath + "/UserLevel.xlsx", FileMode.Open, FileAccess.Read);
24         IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
25
26         DataSet result = excelReader.AsDataSet();
27
28         int columns = result.Tables[0].Columns.Count;
29         int rows = result.Tables[0].Rows.Count;
30
31
32         for(int i = 0;  i< rows; i++)
33         {
34             for (int j = 0; j < columns; j++)
35             {
36                 string nvalue = result.Tables[0].Rows[i][j].ToString();
37                 Debug.Log(nvalue);
38                 if (i > 0)
39                 {
40                     readData.text += "\t\t" + nvalue;
41                 }
42                 else
43                 {
44                     readData.text +="   \t" + nvalue;
45                 }
46             }
47             readData.text += "\n";
48         }
49     }
50
51 }

搞定收工!

PS:可以以数据集的形式存储读取到的二维表格,然后可直接以二维数组的形式获取各个元组的信息!

作为数据集进行存储

DataSet result = excelReader.AsDataSet();

取得数据集中第一张表格的行的数目
int rows = result.Tables[0].Rows.Count;

取得数据集中第一张表格的列的数目

int columns = result.Tables[0].Columns.Count;

直接对行列操作:

result.Tables[0].Rows[i][j].

百度网盘:http://pan.baidu.com/s/1kTGIGS3

时间: 2024-08-29 15:42:29

Unity 读取CSV与Excel的相关文章

unity读取csv表格进字典

using System; using UnityEngine; using System.Collections; using System.Collections.Generic; public class MessageText { private static MessageText instance; /// <summary> /// 单利模式 /// </summary> /// <returns></returns> public stati

python读取csv,txt,excel类似文件出现UnicodeDecodeError错误

错误如下:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0简单粗暴,用Windows的记事本打开源文件,编码格式改为utf-8,再用pandas,xlrd等库文件读文件就解决了.原因是因为python读文件的时候默认使用utf-8编码,而存下来的文件鱼龙混杂,很大一部分是ANSI编码. 原文地址:https://blog.51cto.com/healer/2357632

Unity读取Excel文件(附源代码)

今天想弄个Unity读取Excel的功能的,发现网上有许多方法,采用其中一种方法:加入库文件 Excel.dll 和ICSharpCode.SharpZipLib.dll库文件,(还有System.Data.dll也要拷贝进来,在Unity安装路径C:\Program Files\Unity\Editor\Data\Mono\lib\mono\unity中),代码下载链接在最后. 使用时要注意1997-2003和2007版本的脚本不一样: 然后编写脚本DoExcel.cs: using Syst

unity中CSV的读取

unity中读取数据的格式有很多种,现在为大家介绍的是读取CSV, static CSV csv; public List<string[]> m_ArratData; public static CSV Getinstance() { if (csv == null) { csv = new CSV(); } return csv; } public string getstring(int row, int col) { return m_ArratData[row][col]; } pu

python读取txt、csv和excel文件

一.python读取txt文件:(思路:先打开文件,读取文件,最后用for循环输出内容) fp = open('test.txt','r') lines = fp.readlines() fp.close() for line in lines: username = line.split(',')[0] password = line.split(',')[1] 注:第一句是以只读方式打开文本文件:第二个是读取所有行的数据(read:读取整个文件:readline:读取一行数据):最后一定要关

cocos2d-x 读取CSV文件,读取本地Excel配置表的方法

//CSVReader.h #define MAP_LINE std::map<std::string, std::string> //key为首行字符串, value为此列字符串 #define MAP_CONTENT std::map<int, MAP_LINE> //key为code, value为一行map #define VEC_MAP std::vector<std::pair<std::string, int>> //csv文件读取器 clas

Unity3d读取CSV中的文本到Rich Text中换行符不识别的问题

项目中要用到符文本做一些信息显示,需要有颜色.字体样式.换行之类,Unity的 Rich Text就可以用来做这类需求. 关于Rich Text 的使用: http://docs.unity3d.com/Manual/StyledText.html 虽然里面没有提到 " \n " 这个换行符,但是其实是支持的. 文章转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn 今天在读取CSV中的内容到 Rich Text中显示的时

Python 读取csv的某行

站长用Python写了一个可以提取csv任一列的代码,欢迎使用.Github链接 csv是Comma-Separated Values的缩写,是用文本文件形式储存的表格数据,比如如下的表格: 就可以存储为csv文件,文件内容是:No.,Name,Age,Score1,Apple,12,982,Ben,13,973,Celia,14,964,Dave,15,95假设上述csv文件保存为"A.csv",如何用Python像操作Excel一样提取其中的一行,也就是一条记录,利用Python自

python读取和生成excel文件

今天来看一下如何使用python处理excel文件,处理excel文件是在工作中经常用到的,python为我们考虑到了这一点,python中本身就自带csv模块. 1.用python读取csv文件: csv是逗号分隔符格式 一般我们用的execl生成的格式是xls和xlsx  直接重命名为csv的话会报错: Error: line contains NULL byte insun解决方案:出错原因是直接是把后缀为xls的execl文件重命名为csv的 正常的要是另存为csv文件 就不会报错了 譬