c# - 无法从 android build 中的 Streaming Assets 访问 JSON 文件
问题描述
我对 Unity 比较陌生。当我为android构建它时遇到了以下问题。JSON 文件不会在构建时加载。但是,它会在编辑器中加载。
我尝试了多种技术,例如 Streaming Assets 文件夹、Resources、persistentDataPath。他们似乎都没有为构建工作。
public class setting_data
{
private Eng_WB_Question[] gamedata;
private string filename = "words.json";
public Eng_WB_Controller controller;
string json_data;
private void Start()
{
// LoadJSON();// this isn't necessary because I am calling the LoadJSON function from another class.
}
public void LoadJSON()
{
Debug.Log(json_data);
json_data = Resources.Load(filename).ToString();
Eng_WB_Question_Wrapper loaded_data = JsonUtility.FromJson<Eng_WB_Question_Wrapper>(json_data);
gamedata = loaded_data.questions;
controller.gamedata_loaded(gamedata);
}
}
解决方案
我正在使用 UnityWebRequest 从流资产文件夹中获取 json 文件。我的代码是:
void Awake()
{
filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "CountryCodes2.json");
StartCoroutine(Example());
}
IEnumerator Example()
{
if (filePath.Contains("://"))
{
UnityWebRequest www = new UnityWebRequest(filePath);
yield return www.SendWebRequest();
result = www.downloadHandler.text;
}
else
result = System.IO.File.ReadAllText(filePath);
}
你可以像这样读取你的 json 文件。
推荐阅读
- android - MyLocation 无法转换为 com.example.mycoordinate.TaskLoadedCallback
- ace-editor - 如何在 ace 编辑器中更改片段占位符变量的格式
- string - 字符串解压:降低时间和空间复杂度
- c# - 如何在 UWP 中运行带参数的 exe 文件
- javascript - 为什么我在 console.log 的第一个对象上得到 [Getter/Setter]?
- disqus - 如何让disqus授权api调用?
- asp.net - Word 16 文档以锁定模式打开
- excel - 如何修复我的代码以复制单元格值?
- azure-sql-database - .export 尝试在浏览器查询文本框中执行时返回错误“无法解析查询”
- flask - 无法使用 axios 向 Flask 应用程序发送 POST 请求