forked from Team-Tototototoro/UnityCSV2SO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSVReader.cs
More file actions
64 lines (48 loc) · 1.87 KB
/
CSVReader.cs
File metadata and controls
64 lines (48 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System.Collections;
using System.Collections.Generic;
using UnityCSV2SO.PrimitiveData;
using UnityEditor;
using UnityEngine;
public static class CSVReader
{
private static TextAsset _currentCSVTextAsset;
public static Dictionary<string, PrimitiveDataDictionary> _CSVDictionaries;
public static void SetFile(TextAsset textAsset)
{
_currentCSVTextAsset = textAsset;
ResetAndPopulateDictionary();
}
public static bool TryGetPrimitiveDictionary(string key, out PrimitiveDataDictionary dict)
{
return _CSVDictionaries.TryGetValue(key, out dict);
}
public static void ResetAndPopulateDictionary()
{
if(_currentCSVTextAsset == null) { return; }
_CSVDictionaries = new Dictionary<string, PrimitiveDataDictionary>();
string[] rows = _currentCSVTextAsset.text.Split('\n');
string[] keys = rows[0].Split(',');
for(int r = 1; r < rows.Length; r++)
{
PrimitiveDataDictionary primitiveDataDictionary = new PrimitiveDataDictionary();
string[] columns = rows[r].Split(',');
for(int c = 0; c < columns.Length; c++)
{
string key = keys[c].Trim();
string primitiveDataString = columns[c];
bool b;
int i;
float f;
string s;
b = primitiveDataString.ToLower().Equals("true");
int.TryParse(primitiveDataString, out i);
float.TryParse(primitiveDataString, out f);
s = primitiveDataString.Trim();
PrimitiveData primitiveData = new PrimitiveData(b, i, f, s);
Debug.Log("DAN" + key + "DAN");
primitiveDataDictionary.Add(key, primitiveData);
}
_CSVDictionaries.Add(columns[0], primitiveDataDictionary);
}
}
}