- Home /
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 ?
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.
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!
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.
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
Your answer
Follow this Question
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