Unity’s Serialization and Game Data

30/09/2020|Written by - Team Hashbyte|Game Development

Unity is the best cross-platform game engine from Unity Technologies. The game engine has been extended to supports more than twenty platforms and it supports augmented reality, virtual reality, 2D, 3D games, and other simulations. Apart from the gaming development India market, many other industries have adopted this platform like engineering, automotive, construction, architecture, and films.

The automatic process of transforming the object states and data structures in Unity is called Serialization. Few in-built features like the instantiation, loading and saving, Prefabs, Inspector window in Unity using Serialization. 

Features of the Serialization system


When instantiation() is called on a game object or prefab that exists in the scene i.e things that are derived from UnityEngine. An object can be serialized so we first serialize an object, and then create a new object, again we deserialize the data into a new object. 


The serialization system is used in the in-editorial yalm loading and as the runtime loading of scenes and assets. The asset bundle uses the serialization system as well. 


When you use a text editor to open the .unity scene file and have the set unity to force text serialization, then the serializer runs with yaml backend.  


This is a serialized data stream of game components and objects. It is a list of modifications that has to be made on the serialized data for this instance. The modifications of prefab get baked into the normal serialization stream when Unity makes a build and gets instantiated. The instantiated gameobject has no idea that it was a prefab when it lived in the editor ( the concept prefab exists only at editor time).

Inspector Window

The inspector window does not speak to the C# API to find out the values of the properties of whatever it is inspecting. It simply asks the object to serialize itself and displays the serialized data.

The serialization system is written in C++ and it is used for the internal object type like AnimationClip, texture, camera, etc. the serialization takes place at the UnityEngine.Object-level where all the UnityEngine. The object is serialized as a whole. They can contain a reference to other UnityEngine.Object and those objects serialized properly. 


Fieldtypes that can be serialized

  • We can custom non-generic classes, non-abstract classes with serializable attributes 
  • Custom the struct serializable attribute
  • References to objects that derive from UnityEngine.Object
  • Can serialize primitive data types like float, int, string, double, bool, etc
  • It can serialize built-in types like AnimationCurve, GUIStyle, RectOffset, Gradient, Quaternion, Matrix4x4, etc. 
  • We can serialize an array of fieldtype and List<T> of a fieldtype

Rules of Serialization 

The serialization in Unity runs in the real-time game environment and it has a significant role in the performance. Unity’s serialization behaves differently than the serialization in other programming languages. 

Tips to use Serialization in Unity

  • When you want to make sure that a field in the script is serialized, ensure that it 
  • Is not static or const or read only
  • If it has field type that can be serialized
  • If it has a SerializeField attribute or is public

Custom Serialization

Serialization is an automatic process of transforming object states or data structures into a format in which the Unity can store and reconstruct it later. Sometimes the users might wish to serialize something that is not supported by Unity and in that case, the best approach is to use serialization callbacks. 

ISerializationCallbackReciever: Unity’s serializer is able to serialize most of the datatypes but still left with few. In this case, there are two callbacks available for the users to manually process these datatypes so that Unity can serialize and deserialize them. Care has to be taken while within these callbacks because Unity’s serializer runs on a different thread in many of Unity API and it is advisable to only process the manipulation of datatypes while keeping the processing burden as low as possible. 

Serialization can occur during all kinds of operation, for eg: when using instantiate() to clone an object, Unity serializes and deserializes the original object in order to find an internal reference to the original object so that it can replace them with the clone object. The serialization callbacks allow you to be notified before the serializer reads the data from your fields and after it has finished writing to them. The users can make use of the serialization callbacks to give the hard to serialize data a different representation at runtime to the representation when the user actually serialize. 

Optimize Serialization usage

The users can organize their data to make sure that they get optimal use from Unity’s serialization.


  • While referring to other classes, the users have to avoid recursive and nested structures. The layout of the serialized structure always has to be the same, it is independent of the data and is dependent on the things that are exposed in the script. The only way to reference other objects in this is through the classes derived from UnityEngine.Object. These classes are entirely separate and they only reference each other and don’t embed the contents.


  • The users have to organize the data to never have Unity Serialization cache the data or duplicate the data. Doing so might cause significant troubles in backward compatibility and it carries a high risk in error because it is easy for the data to get out of sync. 


  • The users can organize the data by aiming to have serialization to the smallest possible set of data. The main purpose of doing so is not to save hard drive space of the computer, but to ensure that it can handle the backward compatibility with the earlier version of the project. Because the backward compatibility becomes more difficult later on in the development phase when it has a large set of serialized data. 

Leave a Reply

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

contact us

    We'd Love To Collaborate!

    Let Us Know What You're After.

    Tell Us A Little More About Yourself!

    We’ll Get The Ball Rolling.

    Visit Us

    USA: 5830 E. 2nd Street,
    Casper, WY 82609


    India: F 28 and 29, East Court, Phoenix Market City,
    Viman Nagar, Pune, MH 411014

    Call Us

    +91 9041477333