安装时将文件从 Resources/StreamingAssets 复制到 Application.persistentDataPath [英] Copy files from Resources/StreamingAssets to Application.persistentDataPath upon installation

查看:103
本文介绍了安装时将文件从 Resources/StreamingAssets 复制到 Application.persistentDataPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 txt 文件,其中包含我的游戏地图数据.问题是该文件存储在 Application.persistentDataPath 中,所以我什至可以从我的 android 设备(创建的地图创建者)更改它,所以我如何将我在 PC 上创建的 txt 文件包含在基本地图中并制作当我安装应用程序时,它出现在我的 Android 设备上的 persistentDataPath 中?

I have txt file which contains data for my maps in game. Problem is that file is stored in Application.persistentDataPath so I can change it even from my android device (created map creator) so how can I include txt file which I created on my PC with basic maps and make it appear in persistentDataPath on my android device when I install application?

推荐答案

您可以将文件放在 Editor 文件夹中的 Resources 文件夹中,然后使用 Resources API 读取.

You can put the file in the Resources folder from the Editor folder then read with the Resources API.

TextAsset txtAsset = (TextAsset)Resources.Load("textfile", typeof(TextAsset));
string tileFile = txtAsset.text;

您可以使用 这个.之后,您可以将加载的数据复制到Application.persistentDataPath 目录.

You can check if this is the first time the app is running with this. After that you can copy the loaded data to the Application.persistentDataPath directory.

众所周知,Resources 文件夹会增加加载时间.我建议您不要使用它,但这是一个值得了解的选项.

The Resources folder is known to increase loading times. I suggest you don't use it but it's an option that's worth knowing.

将文件放在 StreamingAssets 文件夹中,然后使用 WWWUnityWebRequest API 和 Application.streamingAssetsPath 读取它作为路径,然后将其复制到 Application.persistentDataPath.

Put the file in StreamingAssets folder then read it with the WWW or UnityWebRequest API and Application.streamingAssetsPath as the path then copy it to Application.persistentDataPath.

从流媒体资源加载:

IEnumerator ReadFromStreamingAssets()
{
    string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "MyFile");
    string result = "";
    if (filePath.Contains("://") || filePath.Contains(":///"))
    {
        UnityEngine.Networking.UnityWebRequest www = UnityEngine.Networking.UnityWebRequest.Get(filePath);
        yield return www.SendWebRequest();
        result = www.downloadHandler.text;
    }
    else
        result = System.IO.File.ReadAllText(filePath);
}

然后将其保存到persistentDataPath:

then save it to persistentDataPath:

File.WriteAllText(Application.persistentDataPath + "data/MyFile.txt", result);

这篇关于安装时将文件从 Resources/StreamingAssets 复制到 Application.persistentDataPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆