Unity’s often used built in form of serialization are the PlayerPrefs. As Lars Kokemohr, the Head of Programming at the School4Games, points out in this Facebook post: The are a lot of drawbacks to saving data using Unity’s PlayerPrefs class. Furthermore he advises that
A cleaner way of doing it would be to create a new file in the path that is stored in Application.persistentDataPath. […] This […] could then be stored using Mono’s BinaryFormatter class.
So in this post I am going to show you how serialization allows you to do just that. The scripts I am using for that is a modified version of what can be found in this blogpost by Ryan Nielson.
Saving the State of an Object = Serialization
In object-oriented programming data is stored in objects, like a class or a struct. You could have a Highscore class for example that saves a player name (e.g. Kevin) and a highscore (e.g. 9001). When this highscore is saved as a file the highscore object (An instance of the Highscore class) and it’s state (i.e. the player name being Kevin and the highscore being 9001) a taken out of memory and converted into a format that can be saved. This process is known as serialization. The process of reading a file and reverting it into the original Highscore object with it’s original state is known as deserialization. C# allows for two basic kinds of serialization: XMLSerialization and binary serialization. XML serializes objects into human readable files. The xml serialized file of the Highscore class could be edited with a basic text editor, which means that everyone could easily change the highscore value with notepad. Binary serialization on the other hand results in a binary file which can not be read and manipulated easily by humans. For the purpose of a Highscore class binary serialization should be used. Another important difference is that xml files can be merged with version control software, whereas binary files can not. For a super deep comparision of the two kinds of serialization (as well as some others) you can read this post. If you want a quick comparision
|Type / Feature||Binary Serializer||Xml Serializer|
|Despription||Serializes and object in an internal binary format that the .NET Framework understands.||Serializes and object in universal XML format.|
|Requeriments||The type needs to be marked with the [Serializable] atribute ||Type must be public (public class person)
Must implement a parameterless constructor (in order to deserialize the object)
If you are serializing a non generic collection of items, you must pass the types that are stored in the collection as a parameter in the constructor of the XmlSerializer.
|Pros||The output byte stream generated is compact|
The serialization process is faster than using the other formatters.
Serializes public and private members (deep serialization)
|Class doesn't need to be decorated with [Serializable] attribute.
Developer has a deep control about how each field is going to be serialized by using attributes
|Cons||Format not readable by other techonolgies (just .NET Framework)||Only public members will be serialize! (shallow serialization)|