Nested class deserialization
I have some system where settings is nested class:
public class SomeDependencyForGlobalSystem : ISomeDependencyForGlobalSystem
{
public float ParamA { get; private set; }
public float ParamB { get; private set; }
[Serializable]
public class Settings : ModuleSettings
{
[Range(1,200)]
public float ParamA;
public float ParamB;
}
[Inject]
public void Inject(Settings settings)
{
ParamA = settings.ParamA;
ParamB = settings.ParamB;
}
}
And project editor for settings:
All settings data stored in scriptable object:
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 8c520adfd27db7a4ebf9cec63f713fa8, type: 3}
m_Name: SettingsDatabase
m_EditorClassIdentifier:
Items:
- {fileID: 114720727467815220}
--- !u!114 &114574360097912698
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 0}
m_Name:
m_EditorClassIdentifier: Assembly-CSharp::SomeDependencyForGlobalSystem/Settings
Id: 1
SettingsName: SomeDependencyForGlobalSystem
Version: v1.0
ParamA: 1
ParamB: 1
Problem: after editor restart unity can't read correctly data from scriptable object. Reason - nested setting class.
Question: how to make Unity deserialize correctly nested classes?
Answer by MstislavPavlov · Oct 30, 2017 at 04:53 PM
Ok, i'm understend, unity support only one level deserialization :|
Answer by YoloJero · Mar 11, 2019 at 04:29 AM
I know this is late but the current answer simply wrong ;)
The correct one would be: Unity can only serialize fields not properties! You can't have only
public float ParamA { get; private set; }
public float ParamB { get; private set; }
but should either remove the {get, private set}
or add proper serialized backing fields like e.g.
public float ParamA { get{ return _paramA;} }
public float ParamB { get{ return _paramB;} }
[SerializeField] private float _paramA;
[SerializeField] private float _paramB;
If you look at yaml, u see that data is serialized and problem in DEserialization date from $$anonymous$$oduleSettings(data in fields) :)
The issue is that simply only fields are serialized and deserialized ... not properties.