Pages

19/04/2017

Reading CSV files in Unity3D


Here’s a little snippet of code for reading text files in Unity using C#.

The read file is displayed on a supplied Text canvas element.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class ReadingCSV: MonoBehaviour
{
   public TextAsset csvFile;
   public Text contentArea;
   private char lineSeparater = '\n';

   private List<string> wordContainer;

   //Read data from CSV file
   // you can alter this to read files that have multiple fields per
   // line using a nested loop and a field deliminator

   private void readData()
   {
     string[] records = csv.text.Split(lineSeparater);
     foreach(string record in records)
     {
        contentArea.text += record + "\t";
        wordContainer.Add(record);
     }
   }
}

No comments:

Post a Comment