Unity3d 将序列化对象转换为Json并转换回Unity
我有两门课:Unity3d 将序列化对象转换为Json并转换回Unity,unity3d,Unity3d,我有两门课: public class MyParent { public string a; public MySub[] b; } public class MySub { public int sub; } 我对它们进行如下初始化: myPC = new MyParent(); myPC.a = "test"; myPC.b = new MySub[2]; myPC.b[0] = new MySub(); myPC.b[1] = new MySub();
public class MyParent
{
public string a;
public MySub[] b;
}
public class MySub
{
public int sub;
}
我对它们进行如下初始化:
myPC = new MyParent();
myPC.a = "test";
myPC.b = new MySub[2];
myPC.b[0] = new MySub();
myPC.b[1] = new MySub();
myPC.b[0].sub = 1;
myPC.b[2].sub = 2;
但是当我序列化对象时,结果只是{“a”:“test”}
如何正确地将JSON转换回对象?在内部,
JsonUtility
使用Unity序列化程序;因此,序列化程序必须支持您传入的对象:它必须是一个单行为、ScriptableObject或应用了Serializable属性的普通类/结构
为了确保自定义类可以序列化,它需要Serializable属性
并且必须遵守这些规则:
[System.Serializable]
public class MyParent
{
public string a;
public MySub[] b;
}
[System.Serializable]
public class MySub
{
public int sub;
}
有关脚本序列化的更多详细信息,请查看官方文档:什么是
@SerializeField
它出现错误名称空间不能直接包含字段或方法等成员
您使用的是C#还是UnityScript?看起来像C#,在这种情况下,它的[System.Serializable]
哇,谢谢,它工作完美:)@DeLe不客气,很高兴我能帮上忙。
[System.Serializable]
public class MyParent
{
public string a;
public MySub[] b;
}
[System.Serializable]
public class MySub
{
public int sub;
}