unity读取streamingassets路径下文件

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);
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容