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 jjey225 · Sep 26, 2019 at 03:18 PM · arrayclassjsonfileconversion

JSON missing arrays

I have this class:

 [System.Serializable]
     public class TerrainSaveInfo
     {
         public string name;
         public float[][] vertices;
         public int[] triangles;
         public float[][] uvs;
     }

  

which I am trying to convert to JSON and save to file. Unfortunately, the data that is visible is name and vertices - the last 2 arrays are not converted to JSON at all.
Why, what am I doing wrong?

Comment
Add comment · Show 1
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 TreyH · Sep 26, 2019 at 03:40 PM 0
Share

Are the other 2 fields missing or what, how do they appear in your file / json string?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Sep 26, 2019 at 03:49 PM

I think you should only see the "name" and the "triangles" array since the other two arrays can not be serialized with Unity's JsonUtility. Nested / jagged arrays or multidimensional arrays are not suppored by Unity's serializer and the JsonUtility has the same limitations as it's build on top of it. What you can do is use this:

 [System.Serializable]
 public class TerrainSaveInfo
 {
     public string name;
     public Vector3[] vertices;
     public int[] triangles;
     public Vector2[] uvs;
 }

Of course this will store each vertex / uv as an object with x,y,z components instead of a nested array. If you want to have this specific format, you could use my SimpleJSON framework and in addition the Unity extension. It does not directly serialize the data into your own custom class, but it provides easy access and easy creation of json text.

By default the "VectorContainerType" is set to "array", so it will create a JSONArray when you store a Vector3. With SimpleJSON you can do:

 JSONNode data = new JSONObject();
 data["name"] = yourName;
 
 var triangles = data["triangles"].AsArray();
 foreach(Vector3 v in yourVertices)
     triangles.Add(v);
 // the same for the other array
 
 // finally
 string text = data.ToString();

Comment
Add comment · Show 3 · 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 jjey225 · Sep 26, 2019 at 04:39 PM 0
Share

Thanks it helped! I don't have to play swapping float[][] for Vector3 now.
Now I have to think about how to reduce the file size because 1 file with my terrain (chunk) takes almost 300kb - it's a bit too much.

avatar image Bunny83 jjey225 · Sep 26, 2019 at 05:28 PM 1
Share

If you want to reduce the file size, using a text format isn't the best solution ^^. Json usually uses double precision floating point numbers. Those expressed as text easily take up 4-6 times the space a float in binary form would take. If you're just looking for a compact way to store a mesh, you might want to have a look at my generic $$anonymous$$eshSerializer. It stores the mesh data in a compact binary format. It only stores things that are present in the mesh.


If you want to keep the text format for some reason, you might just want to compress it. Though it would require some compression library.

avatar image jjey225 Bunny83 · Sep 26, 2019 at 05:48 PM 0
Share

Looking at your code, I have the impression that I'm just a beginner in program$$anonymous$$g. I will stay with the second option with compression.
After all, many thanks, you helped a lot :)

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

131 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 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

Cannot edit values of Class when created using .JSON file 1 Answer

Creating a class wide array (c#) 1 Answer

Adding scripted variables to an array 1 Answer

Losing array in class after Array.ToBuiltin? 1 Answer

Prepopulate Class Array 0 Answers


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