- Home /
how put txt file into android package
I have programme that have to read txt file, on coputer it work but on android don't, I don't know how to locate my txt file's that they I coud read the with path. I was tried to locate txt file in Resources file, first I have using System.IO;
and path:
string path_first_level= @"Resources/first_level.txt";
the I use ReadAllLines:
first_level = System.IO.File.ReadAllLines(path_first_level);
ReadAllLines is best for me becaus it return a table when every line is an object in table so I navigate in txt with this, on computer it works, what should I change with path to use it on android
Answer by troien · Aug 07, 2015 at 12:25 PM
Since you did place your text files in the resources folder, I assume you did read something about this.
However, the way you try to access these files is not the Unity way. In fact, this probably won't work if you would make a build for windows/mac either (unless you manually place the files at the given path after you build). Only in the editor this should work.
By reading the Description of the Resources page you can see how you should acces your data inside the Resources folder.
In your case, sinse you are trying to read a text file. Loading a TextAsset is probably what you want to do. Note that the path parameter is not the full path. In your case "first_level.txt" will probably do the trick (not sure if the .txt is needed, but I'm sure you'll be able to figure that out :p)
TextAsset asset = Resources.Load<TextAsset>("YourPathHere");
Debug.Log(asset.text);
As for why you ca't access your files using System.IO. This is because your files don't exist. Or at least, not in the same way as they do inside your project. For more information on this, this wiki article covers this. (the "Resources" part)
Your answer
Follow this Question
Related Questions
How to post process Unity-built dll before il2cpp kicks in? 1 Answer
Android App crashing while downloading data from firebase 0 Answers
I am having trouble building the project for android (i can't build) 0 Answers
Export android with compute shader doesn't work on unity 2018 1 Answer
Unity Android Build Dosn't Work On The Device But Works In The Editor. 0 Answers