As for serializing objects of some interface type that aren’t UnityEngine.Object’s.
Well… Unity’s serialization engine just won’t support that at all. Since it resolves the type from the field declaration. This is also why if you have a field of some base type, and you reference some child type, it’ll serialize as the base type.
But .Net’s serialization engine bakes the type into the serialized data.
So if you use BinaryFormatter it works.
Or by creating a JsonFormatter:
I get something like this:
{
"@type" : "com.mansion.SaveGameToken",
"Scene" : "Threshold_A",
"Data" : [
"com.mansion.ScenarioPersistentData+ValuePair[]",
{
"@type" : "com.mansion.ScenarioPersistentData+ValuePair",
"Id" : "*Ep1*GameState*",
"Value" : {
"@type" : "com.mansion.Scenarios.Episode1.Episode1Controller+Token",
"ObjectivesToken" : [
"com.mansion.Objective[]"
],
"MobsKilled" : 0,
"MobsSpawned" : 0,
"DocumentsScore" : 1,
"Deaths" : 0,
"RoundsFired" : 0,
"TimesHealed" : 0,
"TakeDamage" : 0,
"Secrets" : 0,
"CurrentTime" : {
"@type" : "System.TimeSpan",
"_ticks" : 268100000
},
"Difficulty" : 2
}
},
{
"@type" : "com.mansion.ScenarioPersistentData+ValuePair",
"Id" : "08D52B5BC2171FCC",
"Value" : {
"@type" : "com.mansion.Scenarios.Episode1.Ep1_PlayerScenarioPersistentStateController+PlayerStateToken",
"Health" : 100,
"MaxHealth" : 100,
"AmmoToken" : [
"com.mansion.Entities.Weapons.AmmoPouch+ValuePair[]",
{
"@type" : "com.mansion.Entities.Weapons.AmmoPouch+ValuePair",
"Type" : 0,
"Quantity" : 18
}
],
"GunDamage" : 25,
"GunCritPercent" : 0.0199999995529652,
"GunAmmoInClip" : 9,
"ImmuneToGrapple" : false,
"Stat_DocumentsRead" : 1,
"Stat_Secrets" : 0,
"Stat_StepTracker" : false
}
}
]
}
So even if the field is an interface type, the serializer serializes the object it’s real type.