簡體   English   中英

如何在Unity的Android版本中讀取文本文件?

[英]how to read text files on android builds for Unity?

當我將游戲構建為.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;
}

你可以使用streamreader:

    List<string> lines = new List<string>();
    using (StreamReader reader = new StreamReader("file.txt"))
    {
        string line;
        while ((line = reader.ReadLine()) != null)
        {
        lines.Add(line); 
        }
    }

string text = System.IO.File.ReadAllText(loadFile.text); 是錯的:

  • loadFile.text已包含您嘗試使用System.IO.File.ReadAllText打開的文件的內容
  • 此外,您無法使用System.IO.File.ReadAllText方法從Android上的StreamingAssets訪問文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM