Threye
  • Games
  • Research
  • About
  • Careers

Subscribe to our newsletter

Serialization in Unity

2/10/2017

Comments

 

Introduction

This blog will cover the basics of serialization and the three most common methods of serialization in Unity: JSON, XML and Binary.

Serialization is the process of converting an object into a form that can stored or transported easily. 
Objects are instances of classes and they are converted to a stream of bytes. This stream can then be stored as a file, saved onto a database or sent over internet to a web service.
The reverse of this process, converting a stream which can be a file or a string into an object is called deserialization.
Picture

​It's easier to understand it using a simple example. In this example, the player details need to be saved, so that when the players replays the game, the details are already available to them. They don't need to provide it on every game session.

When the player inputs his details into PlayerData object the state of the object is serialized and stored as a file. In the next play session the saved file is deserialized  to recreate or clone the same state of our object. JSON and XML are two common standard serialization formats and both of them are shown in the image below. These standards have their own pros and cons and will be discussed later.
Picture

Serialization is used extensively by the unity game engine itself for things like saving data of MonoBehaviours and scriptable objects, editor scripting, prefabs, instantiation, saving and loading your scenes etc. This post will only focus on the use of serialization that you may encounter in a game.

Uses of Serialization


  • Saving Game Data
          Saving game related data like save files, entities data and game settings
  • Web Based Services
          Using web based services like leaderboards, social features, facebook, twitter or google APIs
  • Separation of Data from Code​
          Data can be separated from logic and it's easier for the designers to edit data files
  • Creating Levels​
          Level Layouts, worlds and game scenarios can be saved in a compact form
  • Modding​
          It is easier for modders to edit game entities or even add new entities or gameplay to your game

JSON Serialization


JSON is the most preferred data exchange format for the internet. Web browsers and servers continuously use JSON to communicat between each other.
  • JSON stands for JavaScript Object Notation
  • Syntax for storing and exchanging data
  • JSON is text
  • Language independent
  • Lightweight data interchange format
  • Human readable and easy to understand
PlayerData Class

JSON Serialization and Deserialization
​
​
JSON Output
​

​

XML Serialization


  • XML stands for Extensible Markup Language
  • used primarily for storing or transferring structured data
  • The structure of XML is very similar to HTML
  • Human Readable

XML Serialization and Deserialization
​

XML Output
​

​

JSON vs XML


  • XML is considered more verbose than JSON, so the generated data file is bigger. That's why JSON is preferred for web based services
  • XML can handle complex data structures and relations
  • Serialization and Deserialization operation is more costly for XML compared to JSON
  • More APIs use JSON. Ex - facebook graph API, twitter API and google maps API
  • XML mostly preferred for desktop application, JSON preferred for mobile and web applications

Binary Serialization


​Binary serialization converts and object into a binary format, which is a byte stream.
  • Serialization operation is faster
  • The data file generated is compact and smaller is size when compared to JSON or XML
  • The data file is Non Human readable
  • Platform dependent, only works on .Net platform

Binary Serialization and Deserialization
​
Comments

    Follow updates

    RSS Feed

    Categories

    All
    3D Printing
    Aerospace & Defence
    Augmented Reality
    Author AnuragRana
    Author-AnuragRana
    Game Development
    Robotics

    Archives

    February 2017
    January 2017
    March 2016
    August 2015
    November 2013


​
​SIGN UP FOR OUR NEWSLETTER

We send a few emails per month about updates on games, blogs and jobs. Please consider joining.
​ We value your email and do not spam. Thank You.  

GET OUR APPS

CONNECT WITH US

All rights reserved. Threye Interactive Pvt Ltd. 2021
  • Games
  • Research
  • About
  • Careers