Deserialize TexturePacker JSON at run time

Hey all,

I’m trying to deserialize the texturepacker unity json at run time but I’m confused about how to get JsonUtility to handle the file properly.

Here’s an example of my json :

{"frames": {

"circle copy 2.png":
{
	"frame": {"x":132,"y":1,"w":128,"h":128},
	"rotated": false,
	"trimmed": false,
	"spriteSourceSize": {"x":0,"y":0,"w":128,"h":128},
	"sourceSize": {"w":128,"h":128}
},
"circle copy 3.png":
{
	"frame": {"x":262,"y":1,"w":128,"h":128},
	"rotated": false,
	"trimmed": false,
	"spriteSourceSize": {"x":0,"y":0,"w":128,"h":128},
	"sourceSize": {"w":128,"h":128}
}}

As you can see there’s a frames object and then a bunch of uniquely named objects that aren’t in an array. As far as I know with Unity’s JsonUtility I need to know the name of the object to parse it. My google-fu isn’t helping me figure out this issue.

Any direction would be appreciated.

Basically you’ll have to create a class that is matching the data you receive in the json.
Bellow you can find such an implementation, but I didn’t had the chance to test it (you can see logic behind it though)

public class SizeData {
	public int x;
	public int y;
	public int w; 
	public int h;
}

public class SourceSizeData {
	public int w;
	public int h;
}

public class ImageData {
	public SizeData frame;
	public bool rotated;
	public bool trimmed;
	public SizeData spriteSourceSize;
	publoc SourceSizeData sourceSize;
}

public class Frames {
	public List<ImageData> frames;
}

//decode
Frames decodedFrames = JsonUtility.FromJson<Frames>("yourJson");

Unity’s JsonUtility is just an object mapper so it can’t be used here. You have to use a general purpose Json parser like my SimpleJSON for example ^^.