Read TextAsset from StreamingAssets

I am reading an excel file that is located in “Resources” folder of my Unity application. I now want to load the same file from different location, i.e. StreamingAssets. Now there is no StreamingAssets.Load(), so how do I load and convert the file to TextAsset?

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;
    }
}

and my LoadExcel is as :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LoadExcel : MonoBehaviour
{

    // Start is called before the first frame update
    void Start()
    {
        LoadItemData();
    }

    public void LoadItemData(){

        //Read csv
        List<Dictionary<string, object>> data = CSVReader.Read("MyExcel");

    }

}

I know the way to load file from StreamingAssets is this way:

IEnumerator Start () {

        string path = Path.Combine (Application.streamingAssetsPath, "MyExcel.csv");
        UnityWebRequest uwr = UnityWebRequest.Get(path);
        yield return uwr.SendWebRequest();

    }

But how do I now convert it?

uwr.downloadHandler will have the data, either in .data (as byte[ ]) or in .text (as string)

yes but how do I pass it to CSVReader.Read?
I am trying to do it this way:
I first change the CSVReader file as:

public static List<Dictionary<string, object>> Read(string data)
    {
        var list = new List<Dictionary<string, object>>();
        var lines = Regex.Split(data, LINE_SPLIT_RE);

....
....

and my LoadExcel file as:

string path = Path.Combine (Application.streamingAssetsPath, "MyExcel.csv");
        UnityWebRequest uwr = UnityWebRequest.Get(path);
        yield return uwr.SendWebRequest();

List<Dictionary<string, object>> data = CSVReader.Read(uwr.downloadHandler.text);

But it is not able to read. It says “ID” not found, which is the attribute in my CSV column.

I’m not sure what “LINE_SPLIT_RE” is, but you could try replacing that Regex.Split with String.Split(Environment.NewLine) or String.Split(“;”) (if it’s a CSV).

Have you tried looking at what is actually present in ‘lines’?

I wanted to give it a try with WebRequest, but instead I just did it with ReadAllText. You could also try with StreamReader I guess but I haven’t implemented it that way.

Also, if you guys could find a solution using WebRequest, please feel free to alter my LoadExcel script.

Here is the code for LoadExcel file:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LoadExcel : MonoBehaviour {

    // Start is called before the first frame update
    void Start () {
        LoadItemData();
    }


    public void LoadItemData(){

        string filePath = System.IO.Path.Combine (Application.streamingAssetsPath, "MyExcel.csv");
        string result;

        result = System.IO.File.ReadAllText (filePath);

        //Read csv
        List<Dictionary<string, object>> data = CSVReader.Read (result);

    }

}

and my CSVReader:

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 data)
    {
        var list = new List<Dictionary<string, object>>();
        //TextAsset data = Resources.Load(file) as TextAsset;
        var lines = Regex.Split(data, 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;
    }
}