Unity reads CSV and Excel

Source: Internet
Author: User

 a few days ago saw us in the game need to dynamically load some characters game planning value, about this problem how to solve it? In fact, many kinds of methods, in the final analysis, is the data read. We can think of a number of carriers that store data. For example: Txt,xml,csv,excel. You can even use sqlite,mysql,sqlserver and so on! This is not a problem! So today we're going to learn the CSV file and Excel read. don't say much nonsense, start!

1. Build an empty Project!

2 suggest test.csv file and input data.

Input data:

3. Read the CSV file.

Method One: Cast the CSV file to TXT format and use Textasset to read text information directly in unity.

using Unityengine; using System.Collections; using System.IO;  Public class read:monobehaviour{    public  textasset txtcsv;      Public guitext guitext;     void Start ()    {        = txtcsv.text;        Print (Txtcsv.text);}    }

Operation Result:

The first way is simple, then the second way to read, add script Test.cs
usingUnityengine;usingSystem.Collections;usingSystem.IO; Public classread:monobehaviour{ PublicGuitext Guitext; voidStart () {readcsv (); }    /// <summary>    ///Read CSV file/// </summary>    voidreadcsv () {//reading a CSV binary fileTextasset Binasset = Resources.load (the CSV",typeof(Textasset)) asTextasset; //displayed in the GuitextureGuitext.text =Binasset.text; string[] data = BinAsset.text.Split ("|"[0]); foreach(varDatinchdata)        {Debug.Log (DAT); }        ////Read the contents of each row        string[] Linearray = BinAsset.text.Split ("\ r"[0]); ////Press ' | ' To split        string[] LineArray1 = BinAsset.text.Split ("|"[0]); //creating a two-dimensional array        string[] Array =New string[linearray.length][]; //storing data in a CSV in a two-bit array         for(inti =0; i < linearray.length; i++) {Array[i]= Linearray[i]. Split ("\ r"[0]); Debug.Log (array[i][0].        ToString ()); }    }}

Operation Result:

The OK text method reads the end. Read the following for Excel.

Plugin required: Share later!

1 usingUnityengine;2 usingSystem.Collections;3 usingSystem;4 usingSystem.Collections.Generic;5 usingSystem.Linq;6 usingSystem.Text;7 usingSystem.Text.RegularExpressions;8 usingSystem.IO;9 usingExcel;Ten usingSystem.Data; One usingUnityengine.ui; A  -  Public classNewbehaviourscript:monobehaviour - { the      PublicText ReadData; -     voidStart () -     {         - XLSX (); +     } -      +     voidXLSX () A     { atFileStream stream = File.Open (Application.datapath +"/userlevel.xlsx", FileMode.Open, FileAccess.Read); -Iexceldatareader Excelreader =Excelreaderfactory.createopenxmlreader (stream); -      -DataSet result =Excelreader.asdataset (); -      -         intColumns = result. tables[0]. Columns.count; in         introws = result. tables[0]. Rows.Count; -          to          +          for(inti =0; i< rows; i++) -         { the              for(intj =0; J < columns; J + +) *             { $                 stringNvalue = result. tables[0]. ROWS[I][J]. ToString ();Panax Notoginseng Debug.Log (nvalue); -                 if(I >0) the                 { +Readdata.text + ="\t\t"+Nvalue; A                 } the                 Else +                 { -Readdata.text + ="\ t"+Nvalue;  $                 } $             } -Readdata.text + ="\ n"; -         }     the     } - Wuyi}
View Code

Get it done!

PS: The two-dimensional table can be stored as a dataset, and then the information of each tuple can be obtained directly in the form of a two-dimensional array!

Storage as a data set

DataSet result = Excelreader.asdataset ();

Gets the number of rows in the first table in the data set
int rows = result. Tables[0]. Rows.Count;

Gets the number of columns in the first table in the data set

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

To work directly on rows and columns:

Result. Tables[0]. ROWS[I][J].

Baidu Network disk: http://pan.baidu.com/s/1kTGIGS3

Unity reads CSV and Excel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.