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
1
Question by ZachAttack9280 · Jun 26, 2019 at 05:41 PM · objectserializationnamespacesaveloadproperties

Serializing Properties (JSON saving and loading system)

Hello, I have a few scripts: https://pastebin.com/5fVjYpWN and I'm trying to save and load some things (floats, ints, and something Called BigDouble which is made up of 2 properties, mantissa and exponent in the BreakInfinity namespace used in the scripts). Everything saves and loads except for BigDoubles since it's not serializable, I tried adding the attribute in the namespace but that wouldn't work either. Any ways to do this? Thanks.

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
2
Best Answer

Answer by Bunny83 · Jun 26, 2019 at 09:42 PM

Well you have several possibilities. Since you use Unity's JsonUtility you can do one of the following:

  • Edit your copy of the BigDouble struct and make it Serializable and replace the two auto properties with actual fields. That way it should serialize just fine.

  • Since editing a third-party library / class is not always a good idea you can also implement a wrapper struct that implements the ISerializationCallbackReceiver interface and use the callbacks to copy the values over to actual fields.


Another way would be to use a different serializer (not Unity's JsonUtility) however since you probably have most of it already setup it would make more sense to stick to the JsonUtility unless you stumble into more issues..


The wrapper could look like this:

 [System.Serializable]
 public struct BigDoubleSerializable : ISerializationCallbackReceiver
 {
     public double mantissa;
     public long exponent;
     public BigDouble value;
     public void OnBeforeSerialize()
     {
         mantissa = value.Mantissa;
         exponent = value.Exponent;
     }
     public void OnAfterDeserialize()
     {
         value.Mantissa = mantissa;
         value.Exponent = exponent;
     }
 }

In your PlayerData2 class you would replace all the BigDouble variables with BigDoubleSerializable. Just assign the actual BigDouble value to the "value" field.

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 ZachAttack9280 · Jun 27, 2019 at 12:12 AM 0
Share

I never thought converting the properties: public double $$anonymous$$antissa {get;} public long Exponent{get;} to public double mantissa; public long exponent; would be that easy! It works perfectly now!

BreakInfinity.cs: https://pastebin.com/F$$anonymous$$2W3VnZ

avatar image
0

Answer by Morhem · Aug 26, 2020 at 11:26 PM

Sorry for necroposting, but I was just solving the very same problem, and initially used answer from here, but later discovered, that you can just force unity to serialize private fields with [SerializeField], without having to have a wrapper.

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 · Aug 28, 2020 at 11:48 AM 0
Share

No, you can not. What you have missed is that the original BigDouble struct was not marked as Serializable and its fields were private (auto properties) which are also not serialized. That's why I suggested to edit the BigDouble struct. However as you can see here someone filed an issue at the same day and the developer added the Serializable attribute and changed the fields to serialized fields for newer versions of Unity. So the current version of the BigDouble struct is already serializable and you don't have to do anything.


Of course if your own variables are private you have to use SerializeField just the same way you have to do with any private variable, or make it public. Especially when it comes to saving to json using Unity's JsonUtility you usually want to make the fields of your data class public.

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

120 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

Related Questions

How do i change my save "Total Coin" everytime i get more coins. 0 Answers

Bool array to binaryformatter method 1 Answer

Serializing System.object or System.Type doesnt work. Data is lost. 1 Answer

Xml Serialization of "sub classes" 0 Answers

JSON Error : ArgumentException: JSON must represent an object type. 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