Serialize DataObjects
All DataObjects which have been added to the runtime DataLibrary can be serialized. They will not be serialized automatically as you will have to define the fields that should be serialized and added to the save file.
Make sure your DataObject has been added to the runtime DataLibrary:
Use the [DatabrainSerialize] attribute on all serializable fields. Like this:
public class EnemyData : DataObject
{
public GameObject prefab; //not serializable
// This will serialize health
[DatabrainSerialize]
public int health;
// This will serialize strength
[DatabrainSerialize]
public float strength;
}That's it! Databrain does now all the hard work for you.
Serializing by using the serializable class
It is also possible to create a serializable class which only contains the serializable fields. This class can then be constructed and passed on via the SetSerializedData and GetSerializedData overridable methods. This can be useful when implementing custom serialization for non-serializable data types like Texture2D to bytes, for example.
Let’s assume we have this DataObject Class:
We now have to create an additional runtime class derived from SerializableDataObject.
As you can see, the runtime class looks very similar to the EnemyData object class. We're just using its constructor to pass the original data to the runtime data.
As a last step, we now have to override two methods in the DataObject to set and get the data back. Modify the script in the following way:
Your DataObject is now ready to be serialized by Databrain.
Last updated