Is there a hook when Serialization occurs?

OnEnable is called when an ScriptableObject has been deserialized.

Is there any hook just before it gets serialized?

My concrete problem is, that I have an scriptable object that has a very tuned and specialized structure for holding its data, which would give me a headache to try to turn it into an serializable structure directly. So my idea was, that I convert the data into some serializable format (string, byte or whatever) just before serialization kicks in and convert it back on OnEnable.

Sure, I could do the serialization changes every time I change anything on the data, but that seems rather cumbersome to me.

Isn’t there a simple “OnSerializable” or EditorApplication.onSerializableCallback in the specs that I may have overlooked? :smiley:

Since 4.5, there is a hook: ISerializationCallbackReceiver

Every time the scripts are recompiled, every object is disabled and enabled back again. You have a chance to serialize stuff on OnDisable and serialize data back on OnEnable (be careful in the latter case as you can override previous deserialization by the engine). Some time ago I coded a [Preference] attribute (valid on fields) and some calls made at OnEnable and OnDisable to serialize/deserialize to EditorPrefs, so as to keep values on my custom EditorWindow that were holding configurations (e.g. FTP connection data). Hope it helps.