can you use resources.load to load a text file into streamreader

I need to load a text file from the resource folder that holds an objects properties. Im thinking that I need to use resources.load since the assets in the resource folder are protected upon compile, or else id just use a relative path on stream reader.

so can i use resources.load to find a text file and then pass that text file into stream reader?

2 Answers

2

If you haven't done so already, look into TextAssets:

http://unity3d.com/support/documentation/ScriptReference/TextAsset.html

There's an example at the bottom of this page that loads a TextAsset from the Resources folder:

http://unity3d.com/support/documentation/Components/class-TextAsset.html

Since the URLs of the accepted answer are now dead, I’ll leave here new links that I found trying to solve the problem.

You don’t have to use StreamReader when using TextAssets, and it is actually better because on webgl builds (and other particular cases), StreamReader won’t be able to find your files. Here is the TextAsset version to read every line of your file:

private List<string> TextAssetToList(TextAsset ta)
{
    var listToReturn = new List<string>();
    var arrayString = ta.text.Split('

');
foreach (var line in arrayString)
{
listToReturn.Add(line);
}
return listToReturn;
}

//Code by levoTNTO, Jun 28, 2016

This is where I took the code from:
https://forum.unity.com/threads/read-from-textasset-line-by-line.327422/

The original link of the accepted answer: