Objective
The main Objective of this blog post to learn about Unity Serialization and Game Data.
What is Serialization?
What is Serialized data?
How to Serialize your own data?
How Unity dose Serialization?
Do you have the same questions in your mind?
Then you are at the right place.
Let’s get one ride to it.
According to Unity API Documentation,
"Serialization is the process of converting the state of an object to a set of bytes in order to store the object into memory, a database or a file."
Here, the pictorial view of the Serialization process
serialization
The process of storing the state of the object as “Serialization”
and
The reverse process of building an object out of a file "Deserialization".
A pictorial view of the Deserialization process.
deserialization
The data that Unity derives when it’s trying to make copies or save copies of things that derive from UnityEngine. Object, Scene, in Prefabs.
These are most commonly saved on disk but used in memory.
Let’s see which types of data can be Serialized
Be public, or have [SerializeField] attribute
public float pi = 3.14f;
SerializeField] private int a = 3;
Not be static
public static int myStatic = 13;(will not get serialized...)
Not be const
public const int myConst=15;(will not get serialized...)
Not be readonly
public readonly int myReadonly = 20;(will not get serialized...)
Let’s take one look on which field types that we can serialize
Custom non abstract classes with [Serializable] attribute
[System.Serializable] public class SerializedDataCity { public string name; }
Custom structs with [Serializable] attribute.
References to objects that derive from UnityEngine.Object
public SerializedData serializedData= new SerializedData ();
Primitive data types
int,float,double,bool,string,etc
Array of a field type we can serialize
List<T> of a field type we can serialize
public List<SerializedDataCity> cities;
Let’s take an example,
Step 1)
Create an Empty gameObject name it as you like.
Step 2)
Create a C# script name it as you like.
Write the following code in to you script.