Created
March 17, 2020 13:13
-
-
Save takumifukasawa/f4a4d73143e14ec66e13c992b2d0dd65 to your computer and use it in GitHub Desktop.
Unity: read CSV and parse to list of string array.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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