c# - how to read text files on android builds for Unity? -
i've been having lot of trouble trying read text files stored in streamingasset folder on android phone when build game .apk file.
i know android have use different path access files using
"jar:file://" + application.datapath + "!/assets" + filename;
and store www class. have tried many ways nothing seems work me totally lost right now. code looks this:
void awake(){ string filepath = "jar:file://" + application.datapath + "!/assets" + filename; // reads our text file , stores in array string[][] level = readfile (filepath); } // reads our level text file , stores information in jagged array, returns array string[][] readfile(string file){ www loadfile = new www (file); while (!loadfile.isdone) {} string text = system.io.file.readalltext(loadfile.text); string[] lines = regex.split(text, "\r\n"); int rows = lines.length; string[][] levelbase = new string[rows][]; (int = 0; < lines.length; i++) { string[] stringsofline = regex.split(lines[i], " "); levelbase[i] = stringsofline; } return levelbase; }
you can use streamreader this:
list<string> lines = new list<string>(); using (streamreader reader = new streamreader("file.txt")) { string line; while ((line = reader.readline()) != null) { lines.add(line); } }
Comments
Post a Comment