Created
January 28, 2015 04:51
-
-
Save derme302/ce7dc7eb5252a9d7c3f4 to your computer and use it in GitHub Desktop.
Allows a CSV to be imported into Unity
This file contains hidden or 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
// Built on http://bravenewmethod.com/2014/09/13/lightweight-csv-reader-for-unity/ | |
using UnityEngine; | |
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Text.RegularExpressions; | |
public class CSVReader | |
{ | |
static string SPLIT_RE = @",(?=(?:[^""]*""[^""]*"")*(?![^""]*""))"; | |
static string LINE_SPLIT_RE = @"\r\n|\n\r|\n|\r"; | |
static char[] TRIM_CHARS = { '\"' }; | |
public static List<Dictionary<string, object>> Read(string file) | |
{ | |
var list = new List<Dictionary<string, object>>(); | |
TextAsset data = Resources.Load (file) as TextAsset; | |
var lines = Regex.Split (data.text, LINE_SPLIT_RE); | |
if(lines.Length <= 1) return list; | |
var header = Regex.Split(lines[0], SPLIT_RE); | |
for(var i=1; i < lines.Length; i++) { | |
var values = Regex.Split(lines[i], SPLIT_RE); | |
if(values.Length == 0 ||values[0] == "") continue; | |
var entry = new Dictionary<string, object>(); | |
for(var j=0; j < header.Length && j < values.Length; j++ ) { | |
string value = values[j]; | |
value = value.TrimStart(TRIM_CHARS).TrimEnd(TRIM_CHARS).Replace("\\", ""); | |
object finalvalue = value; | |
int n; | |
float f; | |
if(int.TryParse(value, out n)) { | |
finalvalue = n; | |
} else if (float.TryParse(value, out f)) { | |
finalvalue = f; | |
} | |
entry[header[j]] = finalvalue; | |
} | |
list.Add (entry); | |
} | |
return list; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment