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
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?