- Home /
[Solved] Reading text file on Android
As a preface, I have already looked through all other mentionings of people with this problem, but none of their solutions seem to work.
In my project, I'm attempting to load levels stored as text files with the .map extension inside the StreamingAssets folder. On PC, this works flawlessly, no problems there, but no matter what I try I cannot get it to function on Android.
What I'm currently using (while faulty) to load files is:
public void LoadScene(){
if(Application.isMobilePlatform){
using(StreamReader sr = new StreamReader(Application.persistentDataPath + "/StreamingAssets/Levels/" + levelName)){
gridX = int.Parse(sr.ReadLine ());
gridY = int.Parse(sr.ReadLine ());
level = new int[gridX, gridY];
string[] yList = new string[gridY];
for(int i = 0; i < yList.Length; i++){
yList[i] = sr.ReadLine();
}
for(int y = 0; y < gridY; y++) {
for(int x = 0; x < gridX; x++){
string[] xList = yList[y].Split(',');
level[x, y] = int.Parse (xList[x]);
//print (level[x, y]);
}
}
}
}else{
if(Application.dataPath + "/StreamingAssets/Levels/" + levelName != null){
using(StreamReader sr = new StreamReader(Application.dataPath + "/StreamingAssets/Levels/" + levelName)){
gridX = int.Parse(sr.ReadLine());
gridY = int.Parse(sr.ReadLine());
level = new int[gridX, gridY];
string[] yList = new string[gridY];
for(int i = 0; i < yList.Length; i++) {
yList[i] = sr.ReadLine();
}
for(int y = 0; y<gridY; y++) {
for (int x = 0; x<gridX; x++) {
string[] xList = yList [y].Split(',');
level[x, y] = int.Parse(xList[x]);
//print (level[x, y]);
}
}
}
}else{
print ("Level not found");
}
}
The levels are stored in (on PC) streamingassets/Levels/
Answer by robertcigna · Oct 26, 2015 at 09:46 AM
The path you are looking for is Application.streamingAssetsPath, for all platforms.
It's really only coincidence that Application.dataPath + "/StreamingAssets" works on PC, because the streaming assets folder is directly inside the data directory. That doesn't work on Android because the data path points to the apk, whereas the streaming assets folder is several levels deep inside the apk.
Application.persistentDataPath is also an incorrect location to look for streaming assets on Android, because it points to an entirely different directory outside the apk where your app can write files (not possible inside the apk).
That said, there are other considerations for reading raw files from inside an Android apk, and I'm not sure off the top of my head if using StreamReader for a path inside the apk would work. If you don't have a particular reason for them to be in streaming assets, I would suggest putting them in the resources folder and use something like:
TextAsset level = Resources.Load<TextAsset>("Levels/" + levelName);
if(level != null)
{
using(StreamReader sr = new StreamReader(new MemoryStream(level.bytes)))
{
...
}
}
Blarg; completely passed my $$anonymous$$d that you could $$anonymous$$emoryStream.
Answer by Fragmental · Sep 19, 2018 at 12:13 PM
You can also use webrequest, in conjunction with Application.streamingAssetsPath, as illustrated here https://docs.unity3d.com/2017.4/Documentation/ScriptReference/Application-streamingAssetsPath.html
Your answer
Follow this Question
Related Questions
Make sphere shoot to Touch.position error 1 Answer
Character Movement Problem 0 Answers
Unexplainable NullReferenceException 2 Answers
Unity Editor & iOS - Download MP4 to Streaming Assets Folder 0 Answers
Distribute terrain in zones 3 Answers