Why is Serialization in Unity3d so important and what can I really do with it?

Why is Serialization in Unity3d so important and what can I really do with it?

As Unity describes serialization is basically happening everywhere in the game engine. When you save a scene in your game for instance Scene_1.unity - this file is a serialized representation of your scene and what Unity really does when you double click on your “.unity” file is then deserialize the file into an object representation that the Unity can understand, this file contains a lot of information such as attributes of your game objects, GUIDs, transformations, prefab references and a lot of different components to identify how to interpret this information about the saved scene. Keep in mind that if you like to be able to see the text representation of your scene files simply set the Unity Editor to force text serialization and you will be able to read the scene files in any common text editor.


Unity out of the box now comes with a lot of functionality to serialized your objects and also has a set of rules you have to follow adhere to be able to do so as described below:


  • Fields must be public, or have a SerializeField attribute.
  • Classes must no be static.
  • You can’t not serialize a constant.
  • Make sure that is not read only.
  • Must have a fieldtype that can be serialized such as: Non-Abstract classes with Serializable attribute, structs with Serializable attribute, objects that derived from UnityEngine.Object, primitive data types (int, float, double, bool, string, etc), Enum types, few Unity built-in types (Vector2, Vector3, Vector4, Rect, Quaternion, Matrix4x4, Color, Color32, LayerMask, AnimationCurve, Gradient, RectOffset, and GUIStyle), Array of fieldtype that can be serialized, and lastly a List of a fieldtype that can be serialized as well.


Few questions that come to mind that I personally had before learning about serialization:


Why would you need to serialize anything in your own games?  

There are many instances why you need to serialized information, for instance if you wanted to store in game data such as a checkpoint, you would need to save this information to the disk, you could decide to serialize the information in JSON, XML, or even in binary, then when getting the serialized information back out from the file system or cloud based architecture you will need to load it back (or deserialize it) so that you can consume in your own game.


What is this SerializedField attribute use for?  

Is not needed in instances where the field is set as public as indicated previously, to understand how it works, create a game object and assign a simple script to it, then just add a new public field called “name” and set its value to “John”. Example show below:

// Example
using UnityEngine;

public class Test : MonoBehaviour
    //by default public fields are serialized
    public string name = “John”;

    //you won’t see this in the inspector because is private 
    //so therefore is not serialized
    private string social_security = “123-23-2333”;

    //force serialization
    private string credit_card = “123-23-2334”;


As shown above we created a simple script and if you look at the inspector for the game object you attached the "Test" script then you will notice that "name" and "credit_card" are both fields displayed, name by definition is public so it is serializable, credit_card is private and even though is not serializable we forced it by adding the SerializeField attribute, lastly social_security is private and no attribute was added which therefore is not getting serialized by Unity.


Other topics that I would recommend and found interesting over the years and I also I think you should read are serialization with .NET libraries such as by using BinaryReader or BinaryWriter, or using NewtonSoft which is a JSON framework for .NET - it is also ported and available in the Unity Asset Store for Unity.


Well that’s it and I hope you enjoyed this article, I also want to add that I will create part II of this article as serialization is a very large and complex topic.


Thank you for your time and don't forget to subscribe to my newsletter, also be sure to find me at @dilmerv and stop by and say hi as I love to chat with everyone.

Pingbacks are closed.

No comments yet.