unity读取streamingassets路径下文件

2016-11-24  本文已影响2610人  UnityAsk

It’s always best to use Application.streamingAssetsPath
to get the location of the StreamingAssets folder, as it will always point to the correct location on the platform where the application is running.

On a desktop computer (Mac OS or Windows) the location of the files can be obtained with the following code:
path = Application.dataPath + "/StreamingAssets";
On iOS, use:
path = Application.dataPath + "/Raw";
On Android, use:
path = "jar:file://" + Application.dataPath + "!/assets/";

On Android, the files are contained within a compressed .jar file (which is essentially the same format as standard zip-compressed files). This means that if you do not use Unity’s WWW class to retrieve the file, you need to use additional software to see inside the .jar archive and obtain the file.

  public string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "MyFile");
  public string result = "";
  IEnumerator Example() {
    if (filePath.Contains("://")) {
        WWW www = new WWW(filePath);
        yield return www;
        result = www.text;
    } else
        result = System.IO.File.ReadAllText(filePath);
}
上一篇 下一篇

猜你喜欢

热点阅读