Skip to content

Instantly share code, notes, and snippets.

@takumifukasawa
Created March 17, 2020 13:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takumifukasawa/f4a4d73143e14ec66e13c992b2d0dd65 to your computer and use it in GitHub Desktop.
Save takumifukasawa/f4a4d73143e14ec66e13c992b2d0dd65 to your computer and use it in GitHub Desktop.
Unity: read CSV and parse to list of string array.
using System;
using System.IO;
using System.Collections.Generic;
using UnityEngine;
// ex1. CSVReader.getData(csvFile)
// ex2. CSVReader.getData(csvFile, ",")
public class CSVReader
{
public static List<string[]> getData(string path, string splitStr = ", ")
{
if (path == "")
{
throw new Exception("should be pass csv path.");
}
List<string[]> data = new List<string[]>();
TextAsset csv = Resources.Load(path) as TextAsset;
StringReader reader = new StringReader(csv.text);
while (reader.Peek() != -1)
{
string line = reader.ReadLine();
string[] items = line.Split(splitStr.ToCharArray(), System.StringSplitOptions.RemoveEmptyEntries);
data.Add(items);
}
return data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment