Class CSVReader
Provides methods for reading and parsing CSV files into dictionaries with typed values.
public static class CSVReader
Inheritance
Methods
Read<T>(TextAsset)
Reads and parses CSV data from a TextAsset into a list of dictionaries with typed values.
public static List<Dictionary<string, T>> Read<T>(TextAsset textAsset) where T : IConvertible
Parameters
| Name | Type | Description |
|---|---|---|
textAsset | TextAsset | The TextAsset containing the CSV data. |
Returns
List<Dictionary<string, T>> — A list of dictionaries representing the CSV rows.
Type Parameters
| Name | Type | Description |
|---|---|---|
T | The type to convert each value to. Must implement IConvertible. |
Examples
var data = CSVReader.Read(myTextAsset);
Debug.Log(data.Count);
Read<T>(string)
Reads and parses CSV data from a raw CSV string into a list of dictionaries with typed values.
public static List<Dictionary<string, T>> Read<T>(string rawText) where T : IConvertible
Parameters
| Name | Type | Description |
|---|---|---|
rawText | string | The raw CSV data as a string. |
Returns
List<Dictionary<string, T>> — A list of dictionaries representing the CSV rows.
Type Parameters
| Name | Type | Description |
|---|---|---|
T | The type to convert each value to. Must implement IConvertible. |
Examples
string rawCSV = "Name,Score\nAlice,95\nBob,88";
var data = CSVReader.Read(rawCSV);
Debug.Log(data[1]["Name"]); // Outputs: Bob
ReadFromFilePath<T>(string)
Reads CSV data from a file path and parses it into a list of dictionaries with typed values.
public static List<Dictionary<string, T>> ReadFromFilePath<T>(string filePath) where T : IConvertible
Parameters
| Name | Type | Description |
|---|---|---|
filePath | string | The path to the CSV file. |
Returns
List<Dictionary<string, T>> — A list of dictionaries representing the CSV rows.
Type Parameters
| Name | Type | Description |
|---|---|---|
T | The type to convert each value to. Must implement IConvertible. |
Examples
var data = CSVReader.ReadFromFilePath("Assets/Data/myData.csv");
Debug.Log(data[0]["Name"]);
ReadHeaders(TextAsset)
Reads and parses the CSV headers from a TextAsset.
public static string[] ReadHeaders(TextAsset textAsset)
Parameters
| Name | Type | Description |
|---|---|---|
textAsset | TextAsset | The TextAsset containing CSV data. |
Returns
string[] — An array of header strings.
Examples
var headers = CSVReader.ReadHeaders(myTextAsset);
Debug.Log(string.Join(", ", headers));
ReadHeaders(string)
Reads and parses the CSV headers from a raw CSV string.
public static string[] ReadHeaders(string rawText)
Parameters
| Name | Type | Description |
|---|---|---|
rawText | string | The raw CSV data as a string. |
Returns
string[] — An array of header strings.
Examples
string rawCSV = "Name,Age\nJohn,25";
var headers = CSVReader.ReadHeaders(rawCSV);