Serialize a class that inherits MonoBehaviour
I have a problem with serialization.
I have the following classes (the arrows indicate inheritance):
Vehicle
(abstract) <- Car
(abstract) <- Sedan
and
Vehicle
(abstract) <- MotorBike
The vehicle class inherits from MonoBehavior
.
I have some gameobjects (prefabs), that I attached the classed to. I only use the non abstract classes (i.e. MotorBike
and Sedan
).
My problem is this:
I want to serialize the data stored within these classes, to be able to save/load. (I use BinaryFormatter
for this.) The problem is, MonoBehavior
isn't serializable.
All I want is to serialize the data within the vehicle classes, and (upon loading the data) pass that to my Vehicle Factory class, to re-instantiate the prefabs, with the deserialized data.
What I've tried so far:
Using a separate Data class, but this causes all kinds of problems and I would need to create a child data class for each of my Vehicle classes.
Also, I tried to make a SerializationSurrogate for MonoBehavior, but this didn't work out. And since I'm not trying to serialize the gameobjects, but only to store all relevant data to re-instantiate them, this seemed to be too complex to try.
So, my question is: Does anybody know a way to handle this?