Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Naotagrey · Sep 20, 2020 at 04:47 PM · serializationinheritancedeserializationinherited-members

Help with using JsonUtility.FromJsonOverwrite

I have a GameSettings class that hold all modifiable settings with a singleton instance reference. Following this architecture : (simplified)


 [System.Serializable]
 public class GameSettings{
         public static GameSettings Instance = new GameSettings();
 
         public FloatSetting colorR = new FloatSetting { Name = "Red Color", DefaultValue = 0f, Value = 0f};
         public FloatSetting colorG = new FloatSetting { Name = "Green Color", DefaultValue = 0f, Value = 0f};
         public FloatSetting colorB = new FloatSetting { Name = "Blue Color", DefaultValue = 0f, Value = 0f};
 }

 [System.Serializable]
 public class FloatSetting : SimulationSetting<float> {

     [System.NonSerialized]
     public float maxValue;
     [System.NonSerialized]
     public float minValue;
 }

 [System.Serializable]  
 public abstract class SimulationSetting<ValueType> {
     [System.NonSerialized]
     public string Name;

     public ValueType DefaultValue { get; set; }
     
     public ValueType Value { get; set; }
 }



I use serialization to save only the values (also default value, somehow I can't find how to prevent it from not serializing) and get the following JSON :

{"colorR":{"Value":0.338345945}, "colorG":{"Value":0.0}, "colorB":{"Value":0.0}}

However when it comes to deserializing I tried using JsonUtility.FromJsonOverwrite(str, GameSettings.Instance); But the ColorR Value does not change.

Weirdly when I do GameSettings GS = JsonConvert.DeserializeObject<GameSettings>(str); the values are correctly assigned.

I can't just Gamesettings.Instance = GS; because other references are lost (like reference to UIHandles on screen, etc.), and manual assigning for every setting would be long and not a good practice imo.

Any ideas on the problem here ?

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

1 Reply

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

Answer by Bunny83 · Sep 20, 2020 at 05:16 PM

None of your classes is marked as Serializable. Also Unity does not serialize properties at all, only fields. So your class doesn't have a field called "Value". Since you use an auto property the backing field name is unknown. Also the backing field is also not marked with SerializeField and is private, so not serialized either. In essence nothing in your class(es) is actually serialized.


I just copied your code and once I mark all classes as Serializable and turn the property into a field it works fine.

Comment
Add comment · Show 4 · 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 Naotagrey · Sep 20, 2020 at 05:26 PM 0
Share

Thanks for your answer! However like I said I don't have any problems serializing, I get the intended JSON, my only problem is when deserializing using JsonUtility.FromJsonOverwrite.

I'm not aware of the differences between Properties and field, can you share your fixed code so I can try it out ?

Thanks again!

avatar image Bunny83 Naotagrey · Sep 20, 2020 at 06:02 PM 1
Share

First of all for reference here's the documentation for Unity's JsonUtility. As mentioned in the documentation pretty much the same rules and limitations apply that also apply to script serialization.

These are my changes:

 [System.Serializable]
 public class GameSettings
 {
     public static GameSettings Instance = new GameSettings();
     public FloatSetting colorR = new FloatSetting { Name = "Red Color", DefaultValue = 0f, Value = 0f };
     public FloatSetting colorG = new FloatSetting { Name = "Green Color", DefaultValue = 0f, Value = 0f };
     public FloatSetting colorB = new FloatSetting { Name = "Blue Color", DefaultValue = 0f, Value = 0f };
 }
 [System.Serializable]
 public class FloatSetting : SimulationSetting<float>
 {
     [System.NonSerialized]
     public float maxValue;
     [System.NonSerialized]
     public float $$anonymous$$Value;
 }

 [System.Serializable]
 public abstract class SimulationSetting<ValueType>
 {
     [System.NonSerialized]
     public string Name;
     public ValueType DefaultValue;
     public ValueType Value;
 }



Note that the json text you posted in your question is not valid as "colorB" is missing the opening curly bracket.

avatar image Naotagrey Bunny83 · Sep 20, 2020 at 06:18 PM 0
Share

oh yeah sorry, I made an error retyping, I already had the [Serializable] tags, I just didn't put them in the sample code to try and be as concise as possible. What about turning the properties into fields like you said ? I don't see any code for that and it might possibly be the issue

Show more comments

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

145 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 avatar image avatar image 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

.asset file containing ScriptableObject is empty on openingUnity 1 Answer

An OS design issue: File types associated with their appropriate programs 1 Answer

Copy reference from one component to another derived component., 2 Answers

Serialization and Inheritance: Great-GrandChild Class appears unserialized 0 Answers

Is the class derived/inherited from generic list serializable? 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