Unity C# JsonUtility 未序列化列表 [英] Unity C# JsonUtility is not serializing a list

查看:51
本文介绍了Unity C# JsonUtility 未序列化列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些数据需要序列化/反序列化,但 JsonUtility 没有做它应该做的事情.这是我正在使用的对象:

I've got some data I need to serialize/deserialize, but JsonUtility is just not doing what it's supposed to. Here's the objects I'm working with:

public class SpriteData {
    public string sprite_name;
    public Vector2 sprite_size;
    public List<Vector2> subimage;
}

public class SpriteDataCollection
{
    public SpriteData[] sprites;
}

如果我创建一个 SpriteDataCollection,并尝试使用 JsonUtility 对其进行序列化,我只会得到一个空对象 {}.这是它的构建方式:

If I create a SpriteDataCollection, and attempt to serialize it with JsonUtility, I just get an empty object {}. Here's how it's being built:

            SpriteData data = new SpriteData();
            data.sprite_name = "idle";
            data.sprite_size = new Vector2(64.0f, 64.0f);
            data.subimage = new List<Vector2> { new Vector2(0.0f, 0.0f) };

            SpriteDataCollection col = new SpriteDataCollection();
            col.sprites = new SpriteData[] { data };

            Debug.Log(JsonUtility.ToJson(col));

调试日志只打印{}".为什么它不序列化任何东西?我已经对其进行了测试,并且序列化单个 SpriteData 确实可以完成它应该做的事情,但它在 SpriteDataCollection 中不起作用.

The debug log only prints "{}". Why isn't it serializing anything? I've tested it out, and serializing a single SpriteData does exactly what it's supposed to do, but it won't work in the SpriteDataCollection.

推荐答案

有 4 个已知的可能原因导致您在 Unity 中得到空的 Json.

There are 4 known possible reasons why you may get empty Json in Unity.

1.不包括[Serializable].如果你不包含这个,你会得到空的 json.

1.Not including [Serializable]. You get empty json if you don't include this.

2.使用属性(获取/设置)作为您的变量.JsonUtility 不支持这个.

2.Using property (get/set) as your variable. JsonUtility does not support this.

3. 尝试序列化 List 以外的集合.

3.Trying to serializing a collection other than List.

4.您的 json 是多数组,JsonUtility 不支持并且需要 包装器 工作.

4.Your json is multi array which JsonUtility does not support and needs a wrapper to work.

问题看起来像#1.您在类中缺少 [Serializable] .您必须添加 using System; 才能使用它.

The problem looks like #1. You are missing [Serializable] on the the classes. You must add using System; in order to use that.

[Serializable]
public class SpriteData {
    public string sprite_name;
    public Vector2 sprite_size;
    public List<Vector2> subimage;
}

[Serializable]
public class SpriteDataCollection
{
    public SpriteData[] sprites;
}

5.像上面在SpriteData类中给出的例子一样,变量必须是一个公共变量.如果是私有变量,则在其顶部添加[SerializeField].

5.Like the example, given above in the SpriteData class, the variable must be a public variable. If it is a private variable, add [SerializeField] at the top of it.

[Serializable]
public class SpriteDataCollection
{
    [SerializeField]
    private SpriteData[] sprites;
}

<小时>

如果仍然无法正常工作,那么您的 json 可能无效.从in-unity">在 Unity 中序列化和反序列化 Json 和 Json 数组" 帖子.这应该能让你了解如何解决这个问题.


If still not working then your json is probably invalid. Read "4.TROUBLESHOOTING JsonUtility" from the answer in the "Serialize and Deserialize Json and Json Array in Unity" post. That should give you inside on how to fix this.

这篇关于Unity C# JsonUtility 未序列化列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆