Read CSV file in Unity3d

Source: Internet
Author: User

Directly on the code

Part1:

1 usingUnityengine;2 usingSystem.IO;3 usingSystem.Collections.Generic;4 5  Public classCSV6 {7     StaticCSV csv;8      Publiclist<string[]>M_arraydata;9      Public StaticCSV getinstance ()Ten     { One         if(CSV = =NULL) A         { -CSV =NewCSV (); -         } the         returncsv; -     } -     PrivateCSV () {m_arraydata =Newlist<string[]>(); } -      Public stringGetString (intRowintCol) +     { -         returnM_arraydata[row][col]; +     } A      Public intGetInt (intRowintCol) at     { -         return int. Parse (M_arraydata[row][col]); -     } -      Public voidLoadFile (stringPathstringfileName) -     { - m_arraydata.clear (); inStreamReader sr =NULL; -         Try to         { +sr = File.OpenText (path +"//"+fileName); -Debug.Log ("file finded!"); the         } *         Catch $         {Panax NotoginsengDebug.Log ("file don ' t finded!"); -             return; the         } +         stringLine ; A          while(line = Sr. ReadLine ())! =NULL)  the         { +M_arraydata.add (line. Split (',')); -         } $ Sr. Close (); $ Sr. Dispose (); -     } -}

Part2:

usingUnityengine; Public classfilecontroller:monobehaviour{//Use this for initialization    voidStart () {CSV. GetInstance (). LoadFile (Application.datapath+"/res","Mytest.csv"); Debug.Log ("GetString:"+ CSV. GetInstance (). GetString (1,1)); Debug.Log ("GetInt:"+ CSV. GetInstance (). GetString (1,2)); }}

Add

There are 4 types of paths:

Application.datapath: This path points to the asset folder of our Unity Editor

Application.persistentdatapath: This path points to the sandbox path for iOS and Android

Application.streamingAssetsPath:streamingAsset folder path, on any platform can be read through this path to the contents of the folder

Application.temporarycachepath: Temporary data file path

Read CSV file in Unity3d

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.