There are 4 known possible reasons why you may get empty Json in Unity.
1.Not including [Serializable]
. You get empty json if you don't include this.
2.Using property (get/set) as your variable. JsonUtility does not support this.
3.Trying to serializing a collection other than List
.
4.Your json is multi array which JsonUtility
does not support and needs a wrapper to work.
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;
}
and
[Serializable]
public class SpriteDataCollection
{
public SpriteData[] sprites;
}
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;
}
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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…