How to read files in Build Porjects?

I have a script in Unity to read from txt files for the project like this:

var readerNAME = new StreamReader(File.OpenRead("Assets/Resources/Data/_names"));
        while (!readerNAME.EndOfStream)
            string name = readerNAME.ReadLine();

and I use those data to load prefabs:

sphere[Count] = (GameObject)Instantiate(Resources.Load(name, typeof(GameObject)));

It works pretty well in Unity but when I build the project it’s not working.

I think it is due to unity didn’t package my resource files so I put it into Resource folder under Assets. But still not working.

Please help.


In Unity if you want to read file you need to use TextAsset variables.

Exemple :

TextAsset data = Resources.Load("Assets/Resources/Data/_names") as TextAsset;

If you want all the data in string array do this :

Exemple :

string[] lines = data.text.Split('


I hope my answer will help you. :wink: