- Home /
Unity - Read text file from external source
I'm trying to read a file in Unity from external source with no luck. To be more clear i can easily read and write on file with StreamWriter and StreamReader while i'm developing the game. The problem is that after build game don't want to read the file anymore. The problem is that in the build i have no more the Resource folder neither the position.txt file. So i tried to put the file under c: calling the function in this way
> ReadFromFile(@"C:\test\position.txt");
It still works under developement but not after build
This is the code i'm using during development and it works
string path = Application.dataPath+"/Resources/position.txt";
ReadFromFile(path);
public bool ReadFromFile(string fileName)
{
try
{
string line;
StreamReader theReader = new StreamReader(fileName, Encoding.Default);
using (theReader)
{
do
{
line = theReader.ReadLine();
if (line != null)
{
string[] entries = line.Split(',');
switch (entries[0])
{
case "1":
Console.WriteLine("Case 1");
cube1 = GameObject.Find("Cube1");
cube1.GetComponent<Renderer>().material.color = Color.red;
StartCoroutine(RotateCoroutine(new Vector3(0, 90, 0), 2.0f, cube1));
break;
case "2":
Console.WriteLine("Case 2");
cube2 = GameObject.Find("Cube2");
cube2.GetComponent<Renderer>().material.color = Color.red;
StartCoroutine(RotateCoroutine(new Vector3(0, 90, 0), 2.0f,cube2));
break;
case "3":
Console.WriteLine("Case 3");
cube3 = GameObject.Find("Cube3");
cube3.GetComponent<Renderer>().material.color = Color.red;
StartCoroutine(RotateCoroutine(new Vector3(0, 90, 0), 2.0f, cube3));
break;
default:
Console.WriteLine("Default case");
break;
}
}
}
while (line != null);
theReader.Close();
return true;
}
}
catch (Exception e)
{
Console.WriteLine("{0}\n", e.Message);
return false;
}
}
Can someone give me some hints to make it work?
Why don't you simply copy-paste the Resources
folder into your _Data
folder after the game is built?
I'm a newbie of unity. After build i have only 2 folders Build and TemplateData. Wich folderare you refferring to? And what function i have to call to understand the true path the builded game? I know that there are those but i still not understand how to use them
Debug.Log("Enviroment " + Environment.GetFolderPath(Environment.SpecialFolder.Personal)); Debug.Log("Application Data Path " + Application.dataPath); Debug.Log("Persistent Data Path " + Application.persistentDataPath);
Answer by sisus_co · Jun 06, 2018 at 12:08 PM
You can use Application.streamingAssetsPath instead of the Resources directory if you want to read the text file using StreamReader at runtime.
Alternatively you can use Resources.Load to access files from the Resources directories at runtime, like so:
var textAsset = Resources.Load<TextAsset>("position");
var text = textAsset.text;´
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Mac OS X Build Execute File? 1 Answer
Manipulating and using .Json files at runtime. 0 Answers
Possible to make build output clean? 1 Answer
Error building OSX only. 0 Answers