- Home /
The question is answered, right answer was accepted
Parsing a TexturePacker JSON file
When I use texture packer to publish a texture atlas of for "Unity3d", it creates a txt file with the following JSON format:
{
"frames": {
"frame-abc.png":
{
"frame": {"x":41,"y":2,"w":256,"h":256},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":256,"h":256},
"sourceSize": {"w":256,"h":256}
},
"someframe":
{
"frame": {"x":2,"y":2,"w":37,"h":382},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":37,"h":382},
"sourceSize": {"w":37,"h":382}
}
}
}
My question is, how can I parse this to create a simple class so I can easily access the uv coordinates for each frame:
public class FrameAtlas{
Frame[] frames;
}
public class Frame {
private string name;
private Rect rect;
}
Did you ever get this sorted tbkn? I am working on exactly the same thing at the moment and I am struggling a little!
I ended up using $$anonymous$$iniJSON. The atlas packer JSON is simple enough to pack using this without any smart magic deserializer... Simply iterate the JSON objects and create a rect from the data within each.
Eventually I didn't use the atlas, so I don't have any code to share anymore... sorry. But as far as I understand, unity now has a built in atlas packer (maybe only pro?)
Answer by TonyLi · Jun 01, 2013 at 12:56 AM
Use LitJSON: http://lbv.github.io/litjson/
Or use JSONObject: http://wiki.unity3d.com/index.php/JSONObject
Or, since you're providing the JSON, there aren't any security issues with just using eval(), but it's much slower than LitJSON.
Or, if you can get Texture Packer to export to XML instead, you can use XmlSerializer to load it.
Answer by Andreas Loew · Jan 08, 2015 at 11:21 AM
If you really need access to the UV coordinates one of the JSON readers is the best solution. See above.
However if you intend to create sprites from the data the way easier solution is to use Unity's Texture2D / Sprite classes with a sprite sheet. This already handles the complete drawing and also allows you to directly use your sprites in the editor.
There's a unity plugin that can directly parse sprite sheet data.
Fair enough posting on newer questions with this solution but this is beco$$anonymous$$g spam/advertising on older questions. Please stop
Follow this Question
Related Questions
How can I use atlas sprite as texture for a material? 2 Answers
SpriteAtlas clone memory problem 0 Answers
loading sprite from atlas in script 4 Answers
NGUI Texture atlas help ! 1 Answer
How to get textures/sprites from Atlas and use it ? 1 Answer