2

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? 
Programmer
  • 121,791
  • 22
  • 236
  • 328
Nic
  • 163
  • 3
  • 19

1 Answers1

3

You could use the TextAsset

TextAsset txt = (TextAsset)Resources.Load("position", typeof(TextAsset));  

and then split the text into lines

List<string> lines = new List<string>(txt.text.Split('\n'));

you could also replace the '\n' with System.Environment.NewLine if you want to be cross platform compatible

Sebastian Kilb
  • 286
  • 1
  • 9
  • I tried to use TextAsset too and they works till i 'm in develop mode.After compilation i cannot see the file and i get no right behaviour – Nic Jun 06 '18 at 09:45
  • Your answer was right but i forgot to mention that i was building the application like a WebGL one. So i cannot access local files, i have to use WWW. Silly mistake from a newbie – Nic Jun 06 '18 at 13:15
  • well if you are using webgl than no, you cannot access local files – zambari Jun 06 '18 at 14:03