I am creating a custom editor where you can create your own types to be displayed in a GUI editor. These types inherit from a base class which defines some variables that are not serialisable by Unity, but I have written my own serialisation for them. The user-defined types must contain only serialisable members.
For example:
// Base is custom serialised, the rest are serialisable by default
public class Example : Base
{
// A Unity Object, so serialised by default
public UnityEngine.Object obj;
// This is default serialisable by Unity
public SerialisedCommand serialised;
}
My issue is this - how do I have Unity correctly serialise the user-defined class? Will simply adding the System.Serialisable attribute to the class work, or is there some way I can have the Unity API serialise part of it?
If you implement Unity - Scripting API: ISerializationCallbackReceiver, you can have your base class perform particular serialization tasks, just before and after serialization by unity. This allows you to put your data into (or extract your data from) a form that unity CAN serialize. Depending on how you implement your class hierarchy, you can always have classes derived from Base override, and redefine those callback functions to do something special/specific.
Ah… may have misunderstood the question- As long as the derived classes field are public or specifically attributed as serializable (and of a type that is actually serializable by unity), those fields WILL be serialized by unity automatically. However, this DOES make the assumption that the base class IS, serialized by Unity (which I mention because you said you do your own.) If this is NOT the case, you may need to store those derived class as their own assets, and just create/reference them from your base class, when doing its serialization.