下载 Json 文件,构建游戏后无法运行
Downloaded Json File, not Working after Building the Game
美好的一天社区,
几周以来我一直在研究一个愚蠢的饮酒游戏。一切都很好,花花公子。努力完成我所有的想法并完成它。我为自己感到骄傲。现在这是踢球者。游戏使用 Unity 中的 www 从我的 Nextcloud 下载 Json 文件。 Json 包含所有 Game_Cards。它在 Unity 中运行得非常好,但是在我构建游戏之后,它什么都不做。我很确定我在这里遗漏了一些东西。但我不知道如何调试它。坦率地说,我认为我对这个有工作盲点。我希望这是一个简单的修复。
我觉得是怎么回事可能:
- 下载工作正常,但 Json 文件保存到错误或无法访问的路径
- 根本无法下载。
卡片路径:
cardPath = Application.dataPath + "/Saves/cards.json";
下载方法
IEnumerator Cards()
{
UnityWebRequest www = new UnityWebRequest("https://my.link.wich/nobody/should/know");
www.downloadHandler = new DownloadHandlerBuffer();
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.LogError(www.error);
}
else
{
string downloadedText = www.downloadHandler.text;
File.WriteAllText(cardPath, downloadedText);
ReadCards();
}
}
正在阅读来自Json
的卡片
public void ReadCards()
{
string cardFile = File.ReadAllText(cardPath);
cardDatabase = JsonUtility.FromJson<CardListObject>(cardFile);
}
第一次使用 Whosebug 并实际发布了一些东西。对我好点:D
更新
成功了!
我必须先创建文件夹。
cardPath = Application.persistentDataPath + "/Saves/cards.json";
if (!System.IO.Directory.Exists(Application.persistentDataPath + "/Saves"))
{
System.IO.Directory.CreateDirectory(Application.persistentDataPath + "/Saves");
}
在构建中,您需要使用 Application.persistentDataPath
而不是 Application.dataPath
UPD:我的错,你只有下载才能阅读。所以你可以忽略下一部分。
如果文件不存在,您需要在 ReadCards
方法中执行一些操作,因为您可能会遇到下载错误并且不会创建文件。
例如:
public void ReadCards()
{
if (!System.IO.File.Exists(cardPath) return;
string cardFile = File.ReadAllText(cardPath);
cardDatabase = JsonUtility.FromJson<CardListObject>(cardFile);
}
然后你的 cardDatabase
将保持默认值,你将不会有 FileNotFoundException
。
美好的一天社区,
几周以来我一直在研究一个愚蠢的饮酒游戏。一切都很好,花花公子。努力完成我所有的想法并完成它。我为自己感到骄傲。现在这是踢球者。游戏使用 Unity 中的 www 从我的 Nextcloud 下载 Json 文件。 Json 包含所有 Game_Cards。它在 Unity 中运行得非常好,但是在我构建游戏之后,它什么都不做。我很确定我在这里遗漏了一些东西。但我不知道如何调试它。坦率地说,我认为我对这个有工作盲点。我希望这是一个简单的修复。
我觉得是怎么回事可能:
- 下载工作正常,但 Json 文件保存到错误或无法访问的路径
- 根本无法下载。
卡片路径:
cardPath = Application.dataPath + "/Saves/cards.json";
下载方法
IEnumerator Cards()
{
UnityWebRequest www = new UnityWebRequest("https://my.link.wich/nobody/should/know");
www.downloadHandler = new DownloadHandlerBuffer();
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.LogError(www.error);
}
else
{
string downloadedText = www.downloadHandler.text;
File.WriteAllText(cardPath, downloadedText);
ReadCards();
}
}
正在阅读来自Json
的卡片public void ReadCards()
{
string cardFile = File.ReadAllText(cardPath);
cardDatabase = JsonUtility.FromJson<CardListObject>(cardFile);
}
第一次使用 Whosebug 并实际发布了一些东西。对我好点:D
更新
成功了! 我必须先创建文件夹。
cardPath = Application.persistentDataPath + "/Saves/cards.json";
if (!System.IO.Directory.Exists(Application.persistentDataPath + "/Saves"))
{
System.IO.Directory.CreateDirectory(Application.persistentDataPath + "/Saves");
}
在构建中,您需要使用 Application.persistentDataPath
而不是 Application.dataPath
UPD:我的错,你只有下载才能阅读。所以你可以忽略下一部分。
如果文件不存在,您需要在 ReadCards
方法中执行一些操作,因为您可能会遇到下载错误并且不会创建文件。
例如:
public void ReadCards()
{
if (!System.IO.File.Exists(cardPath) return;
string cardFile = File.ReadAllText(cardPath);
cardDatabase = JsonUtility.FromJson<CardListObject>(cardFile);
}
然后你的 cardDatabase
将保持默认值,你将不会有 FileNotFoundException
。