Choosing what is saved
Only certain fields are saved by default (see Supported Types). To save other fields or properties, you can use attributes or ES3Types.
Attributes
The [ES3Serializable] attribute is used to tell Easy Save to serialize a field or property which is not automatically serialised.
1 2 |
[ES3Serializable] private float myPrivateFloat; |
You can use the [ES3NonSerializable] to prevent a field or property from being serialized.
1 2 |
[ES3NonSerializable] public float myPublicFloat; |
Controlling serialization using ES3Types
In some cases it’s not possible to add attributes, so it’s necessary to create an ES3Type.
An ES3Type script tells Easy Save what fields and properties of a type to save and how to save them. This can be created automatically, and modified if necessary.
Once the script is created, Easy Save will automatically find it and use it to serialize and de-serialize your type. You do not need to create an instance of the ES3Type.
Creating an ES3Type
An ES3Type can be created by going to the Window > Easy Save 3, and selecting the Types tab.
From here you can select your Type from the list and choose the fields and properties you wish to save.
Saving a field or property which is not explicitly serializable can have undesired effects, so you should ensure it’s safe to serialize before selecting it.
Manually modifying an ES3Type
In some cases you will need to modify the ES3Type. For example:
- When the type does not have a parameterless constructor.
- When you need to call a method to access the variable you’re saving/loading.
You can do this by modifying the Write, Read and ReadInto methods of the ES3Type script generated in the Types pane. The ES3Type script can be found in /Assets/Easy Save 3/Types/.
Note that once you manually modify the ES3Type file, any changes made in the Types panel will overwrite your manual modifications.
Modifying the Write method
You can change what properties are written by modifying or adding reader.WriteProperty calls in the Write method.
Note that UnityEngine.Object types such as MonoBehaviours and ScriptableObjects will be written by reference.
1 2 3 4 5 6 7 8 |
protected override void WriteComponent(object obj, ES3Writer writer) { var instance = (MyScript)obj; writer.WriteProperty<int>("points", instance.points); writer.WriteProperty<float>("speed", instance.speed); writer.WriteProperty<Transform>("partner", instance.partner); } |
Modifying the Read Method
The Read method uses the reader.Properties enumerator to get the name of each property, and a switch statement with reader.Read calls to read the value of that property.
By changing or adding cases in the switch statement you can change which properties are serialized.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
protected override void ReadComponent<T>(ES3Reader reader, object obj) { var instance = (MyScript)obj; foreach(string propertyName in reader.Properties) { switch(propertyName) { case "points": instance.points = reader.Read<int>(); break; case "speed": instance.speed = reader.Read<float>(); break; case "partner": instance.partner = reader.Read<Transform>(); break; default: reader.Skip(); break; } } } |
If the class you’re saving does not have a parameterless constructor, you will also need to modify the ReadObject<T>(ES3Reader reader) method to create an instance with appropriate default values. For example:
1 2 3 4 5 6 |
protected override object ReadObject(ES3Reader reader) { var instance = new MyClass(1, 2, 3); ReadObject(reader, instance); return instance; } |