【发布时间】:2022-01-31 21:53:28
【问题描述】:
我对 Unity 和 C# 还很陌生,我正在尝试将 json 文件解析为 Unity 中的 Experiment 对象,但我不断从 Debug.Log 行收到此错误消息;
Argument 2: cannot convert from 'System.Collections.Generic.List<Experiment>' to 'UnityEngine.Object'
这是我的加载 json 代码,使用 JSON .NET for Unity 资产。我的 json 文件结构为一个对象数组。
public void LoadJson()
{
using (StreamReader r = new StreamReader("exMercury.json"))
{
string json = r.ReadToEnd();
List<Experiment> items = JsonConvert.DeserializeObject<List<Experiment>>(json);
Debug.Log("Experimenter ", items);
}
}
json文件:
{
"experiments": [
{
"Title": "A title",
"Ingredients": "list of ingredients",
"Description": "description",
"Points": 100,
"Completed": false,
"Steps": [
"step 1",
"step 2",
"step 3",
],
"Hypothesis": "",
"Explanation": "",
"Progress": 0.0
},
]
}
实验结构;
using System.Collections;
using System.Collections.Generic;
using Firebase.Firestore;
[FirestoreData]
public struct Experiment
{
[FirestoreProperty]
public string Title { get; set; }
[FirestoreProperty]
public string Description { get; set; }
[FirestoreProperty]
public List<string> Ingredients { get; set; }
[FirestoreProperty]
public List<string> Steps { get; set; }
[FirestoreProperty]
public int Points { get; set; }
[FirestoreProperty]
public bool Completed { get; set; }
[FirestoreProperty]
public string Hypothesis { get; set; }
[FirestoreProperty]
public string Explanation { get; set; }
[FirestoreProperty]
public float Progress { get; set; }
}
任何帮助将不胜感激:)
【问题讨论】:
-
我在您这里显示的代码中看不到任何会引发类似异常的地方?
-
除非是
Debug.Log行,否则我对 Unity 的了解不够深入,无法知道过载应该做什么。 -
调试日志是唯一可以显示的地方,是需要 UnityEngine.Object 的日志
-
糟糕,我的错,错误在 Debug.Log 行
-
所以您遇到的错误是因为您错误地使用了
Debug.Log方法。尽管@madreflection 也是正确的,但这不是您在这里要问的。