我好生气😤,Python,JS里面一两行代码能够搞定的Json读取,在Unity中使用C#读取Json文件超多坑,爬出来一个又来一个。
主要是JsonUtility.FromJson太不给力了。
最好的方式是,使用 https://github.com/jilleJr/Newtonsoft.Json-for-Unity 这个第三方库。详情见下。
在UnityEditor中运行程序时,如何读取Json
- Step 1: 先把对应的Json File放到 Assets/Resources 里面,假设该Json File的名字为:“my_file.json”
- Step 2: 对应着Json Object的格式,声明一个对应的类,例如:
[System.Serializable]
public class CustomerInfo
{
public string application_id;
public string assigned_staff;
public float arrive_time;
public float starting_process_time;
public float finish_time;
}
[System.Serializable]
public class CustomerInfoList
{
public List<CustomerInfo> customerApplications = new List<CustomerInfo>();
}
这里是第一个坑,[System.Serializable] 必不可少,否则无法JsonUtility无法解析。见文档说明:
Internally, this method uses the Unity serializer; therefore the type you are creating must be supported by the serializer. It must be a plain class/struct marked with the Serializable attribute. Fields of the object must have types supported by the serializer. Fields that have unsu

文章讲述了在Unity中使用C#读取Json文件遇到的问题,主要批评了JsonUtility的不足,推荐使用Newtonsoft.Json第三方库。详细步骤包括将Json文件放入Resources,创建对应的C#类,使用Resources.Load读取文件,以及如何处理JsonArray和JsonObject的转换。此外,还提到在不同平台运行时的读取策略。
最低0.47元/天 解锁文章
8985

被折叠的 条评论
为什么被折叠?



