【发布时间】:2021-09-10 21:06:49
【问题描述】:
我有一个 JSON 文件,其中包含我在游戏中的本地化数据。该文件存储在 Application.persistentDataPath 中。我在文档中读到更新游戏时不会删除或覆盖该文件。目前我启动应用程序并且文件在那里,但是当我添加更多内容并发送更新以再次保存时,它只有原始内容。
但是当我更新游戏时,有没有办法更新这个文件? 如果将来我想向这个 JSON 文件添加更多内容,我必须将其删除并重新上传怎么办? 有什么方法可以更新和覆盖它?
private static void Request(string file)
{
var loadingRequest = UnityWebRequest.Get(Path.Combine(Application.streamingAssetsPath, file));
loadingRequest.SendWebRequest();
while (!loadingRequest.isDone)
{
if (loadingRequest.isNetworkError || loadingRequest.isHttpError)
{
break;
}
}
if (loadingRequest.isNetworkError || loadingRequest.isHttpError)
{
// Some error happened.
}
else
{
File.WriteAllBytes(Path.Combine(Application.persistentDataPath, file), loadingRequest.downloadHandler.data);
}
}
// Loads key from "StreamingAssets/language_data.json" file.
public static string LoadJson(string key)
{
Request("data.json");
var jsonPath = Path.Combine(Application.persistentDataPath, jsonFileName);
using (StreamReader r = new StreamReader(jsonPath))
{
var N = JSON.Parse(r.ReadToEnd());
string result = N[GetLanguage()][key];
return result;
}
}
【问题讨论】:
-
您能否向我们展示您尝试更新文件的代码?您如何确保内容实际更新?
-
是的,我刚刚上传了
-
// Loads key from "StreamingAssets/language_data.json" file.,不,它不是从persistentDataPath加载它......你的Request方法而是从streamingAssetsPath加载并写入peristentDataPath,这对我来说看起来不错...这些是从哪里调用的?以及按什么顺序? -
@derHugo 我调用 LoadJson() 并在 LoadJson 内部调用 Request()。在编辑器中它工作得很好(我刚刚测试过),但在 iOS 中,即使我允许任意加载,它也不会更新或添加到 persistentDataPath。 (这是我通常测试的地方)。
-
哦,是的,现在我明白了,对不起^^ 只是出于兴趣……如果请求失败怎么办?你没有抓住这种情况