Serialization – Saving Objects as Files

Serializing a custom Class

From now we will only deal with binary serialization. For more information about xml serialization check out the official documentation as well as this post on udemy.

Now that we can save a primitive struct like an integer we are going to serialize our own custom classes. For this example we will create a serializable Highscore class.
Lets start by creating this class.

public class Highscore
{
    public string Name;
    public int Score;
    public string Secret;
}

This is the Highscore class which has variables for a Name, a Score and a Secret. If we try to serialize it in it’s current form we would receive the following error: “SerializationException: Type Highscore is not marked as Serializable.” For a class to be serializable it must be marked with the [Serializable] attribute, that is located in the System namespace. Our Highscore class now looks like this:

using System;

[Serializable]
public class Highscore
{
    public string Name;
    public int Score;
    public string Secret;
}

Now we can serialize our Highscore without problem. It is important to note that every member of this class must also be marked as serializable. C# primitives like ints and strings are marked with the serializable attribute by default. But some third party classes are not marked and therefore can’t be saved. For example one has to save Unity’s Vector3 as 3 separate float values.
We can also exclude certain fields from being serialized with the [NonSerialized] attribute.

using System;

[Serializable]
public class Highscore
{
    public string Name;
    public int Score;
    [NonSerialized]
    public string Secret;
}

Now after deserialization the Secret variable will be it’s default value, which is null. The default value for reference types is null, for numeric value types is 0 and for bools is false. You can find more information about default values in the official documentation.

Creating the basis for a Save System

Now that we know how to create our own serializable objects and how to serialize them, we can create a system to save, load and delete our data. We are going to start by making an interface for all our cutstom save classes.

using System;

[Serializable]
public abstract class DataCotainer
{
}

The DataContainer class is the base class of all classes that will be saved. It is marked with the [Serializable] attribute which means it can be serialized. Furthermore it is a abstract class, so it can’t be initialized with the new keyword and has to be inherited. This class is the interface (don’t confuse that with a c# interface) we will use to store all kinds of data / classes inherited from DataContainer.

using System;

[Serializable]
public class PlayerDataCotainer : DataCotainer
{
    public string PlayerName = "Ryan";

    public int Score { get; set; }

    [NonSerialized]
    public string Secret = "Nope";
}

This PlayerDataContainer is an actual class that will be saved. This class is used to store information about the player. It is inherited from DataContainer and is also marked with the [Serializable] attribute.

Leave a comment

Your email address will not be published. Required fields are marked *