Databrain supports all common Unity types + custom classes
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:
publicclassEnemyData:DataObject{publicGameObject prefab; //not serializable // This will serialize health [DatabrainSerialize]publicint health; // This will serialize strength [DatabrainSerialize]publicfloat 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:
public classEnemyData: DataObject{ public GameObject prefab; //not serializable public int health; public float strength;}
We now have to create an additional runtime class derived from SerializableDataObject.
public classEnemyData: DataObject{ public GameObject prefab; //not serializable public int health; public float strength;}// Runtime Dataobject classpublic classRuntimeEnemyData: SerializedDataObject{ public int health; public float strength; // For serialization we need to add a parameterless default constructor public RuntimeEnemyData(){} // Use the constructor to only assign serializable data public RuntimeEnemyData (EnemyData _data) {this.health =_data.health;this.strength =_data.strength; }}
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:
publicclassEnemyData:DataObject{publicGameObject prefab; //not serializablepublicint health;publicfloat strength; // Return the serializable data objectpublicoverrideSerializableDataObjectGetSerializedData() {var _runtimeEnemyData =newRuntimeEnemyData(this);return _runtimeEnemyData; } // Called after loading of json file. // Set back the data.publicoverridevoidSetSerializedData(object data) { health = (data asRuntimeEnemyData).health; strength = (data asRuntimeEnemyData).strength; }}// Runtime Dataobject classpublicclassRuntimeEnemyData:SerializableDataObject{publicint health;publicfloat strength; // For serialization we need to add a parameterless default constructorpublicRuntimeEnemyData(){} // Use the constructor to assign the datapublicRuntimeEnemyData (EnemyData _data) {this.health=_data.health;this.strength=_data.strength; }}
Your DataObject is now ready to be serialized by Databrain.
Please note: For serialization, a parameterless default constructor is required for the runtime SerializableDataObject