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