Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by bardic · Jan 19, 2018 at 02:19 PM · jsondeserialization

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Neamtzu · Jan 19, 2018 at 03:13 PM

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");
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bardic · Jan 19, 2018 at 04:51 PM 0
Share

Thanks for the reply. That is almost have I have already but I keep getting this error : Unexpected node type. UnityEngine.JsonUtility:FromJson(String)

I thought it may have any something to do with the way the children objects are named.

avatar image
0

Answer by Bunny83 · Jan 19, 2018 at 05:13 PM

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 ^^.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jan 19, 2018 at 05:24 PM 0
Share

ps: There's also a Unity specific extension file for SimpleJSON. It adds some conversion helpers for most built-in Unity types. Unfortunately the serialization format i've choosen for Rect is {"x":0, "y":0, "width":128, "height":128 }. However you can of course alter your copy anyway you like ^^ (Specifically line 276 as well as line 292 / 293).

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

76 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Deserialize Json into list.,how to deserialize a list via .FromJson 1 Answer

Deserializing dictionary with 'Json.Net' for Unity returns a null object. 0 Answers

JSON deserialization 0 Answers

Json.net deserialization problem 1 Answer

Deserializing Json problem in Hololens app 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges