在我将游戏打包为.apk文件后,在Android手机上读取StreamingAsset文件夹中存储的文本文件一直很麻烦。
我知道在Android系统中,要访问这些文件需要使用不同的路径:
"jar:file://" + Application.dataPath + "!/assets" + fileName;
并将其存储到WWW类中。我尝试了很多方法,但是似乎没有什么作用,因为我现在完全迷失了方向。我的代码如下:
void Awake(){
string filePath = "jar:file://" + Application.dataPath + "!/assets" + fileName;
// Reads our text file and stores it in the array
string[][] Level = readFile (filePath);
}
// Reads our level text file and stores the information in a jagged array, then returns that 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][];
for (int i = 0; i < lines.Length; i++) {
string[] stringsOfLine = Regex.Split(lines[i], " ");
levelBase[i] = stringsOfLine;
}
return levelBase;
}