安装后将文件从Resources/StreamingAssets复制到Application.persistentDataPath [英] Copy files from Resources/StreamingAssets to Application.persistentDataPath upon installation
问题描述
我有一个txt
文件,其中包含游戏中我的地图的数据.问题是该文件存储在Application.persistentDataPath
中,因此即使从我的android设备(创建的地图创建者),我也可以对其进行更改,因此如何将我在PC上创建的txt文件包含在基本地图中,并使其显示在persistentDataPath
中安装应用程序时我的Android设备是什么?
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;
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.
众所周知,资源"文件夹会增加加载时间.我建议您不要使用它,但这是一个值得了解的选项.
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 文件夹中,然后使用WWW
或UnityWebRequest
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
.
从StreamingAssets加载:
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屋!