Hi!
I use Unity 2019.1.8f1.
My problem is that I can’t serialize a list of instances of objects that derive from an abstract class.
I wrote this code to make it work:
[Serializable]
public abstract class MyAction { }
[Serializable]
public class MyActionA : MyAction { }
[Serializable]
public class MyActionB : MyAction { }
[Serializable]
public class MyActionsDatabase : ScriptableObject, ISerializationCallbackReceiver
{
[SerializeField]
private List<MyAction> actions = new List<MyAction>();
[SerializeField]
byte[] actionsBuffer;
void ISerializationCallbackReceiver.OnAfterDeserialize()
{
this.actions = GenericSerializer.BinarySerializer<List<MyAction>>.Deserialize(this.actionsBuffer);
}
void ISerializationCallbackReceiver.OnBeforeSerialize()
{
this.actionsBuffer = GenericSerializer.BinarySerializer<List<MyAction>>.Serialize(this.actions);
}
}
PS: GenericSerializer.BinarySerializer is a wrapper for the “BinaryFormatter” of .NET
Even though this works for MonoBehaviours, it doesn’t for ScriptableObjects.
I simply Add my actions to the list from an editor script and it seems ok (the buffer is serialized), then i close and reopen unity (or just change scene and return to the previous) but my buffer is empty.
The question is: does “ISerializationCallbackReceiver” work for ScriptableObjects? What could I do in order to serialize my list?
Thank you.