In this post, I will talk about Unity SeralizeField.
What is serialization?
In computing, serialization is the process of translating a data structure or object’s state into a format that the computer can store or transmit and reconstruct later. In other words, you take an object in ram and make a disk representation of the object to recreate it at any point in the future.
When the consequential series of bits is reread according to the serialization format, users can use it to create an identical clone of the original object. This process is often not as straightforward as it may sound for many complex objects.
Serializing an object is also called marshaling an object in some situations. The opposite operation, extracting a data structure from a series of bytes, is deserialization.
When you apply the SerializeField attribute to a field, it commands Unity to save or restore its state. Unity SerializeField forces Unity to serialize a private field.
Note that when Unity serializes your scripts, it will serialize your public fields only. If you want to serialize your private fields, you should add the SerializeField attribute to those fields.
Unity serializes the script components, reloads the new assemblies, and recreates the script components from their serialized counterparts. This process is done in the internal Unity serialization system, not with .NET’s serialization functionality.
The serialization system can do the following:
- Serialize public non-static fields (of serializable types)
- Serialize nonpublic non-static fields marked with the SerializeField attribute.
The serialization system cannot do the following:
- Serialize static fields.
- Serialize properties.
Unity can serialize fields of the following types:
- All classes from UnityEngine.Object
- All primary data types, including int, string, float, and bool.
- Some built-in types, including Vector2, Vector3, Vector4, Quaternion, Matrix4x4, Color, Rect, and LayerMask.
- Arrays and lists of a serializable type
To learn more about serializable types, you can check the Script Serialization on Unity Documentation.
Unity SeralizeField dictionary
If this is the question that lured you into this article, let me tell you that: Unity won’t serialize Dictionary. Still, you have options: Store a List<> for keys and a List<> for values and mash them in a non serialized dictionary on Awake(). You won’t be able to modify the Dictionary and have it saved back, but it might help you with many other cases.
For your other options, you can visit this discussion.
Unity SerializeField vs public
To understand when you should use SerializeField or public variables, you should know the difference between public and private variables:
Public variables can be accessed by any other code in the same assembly or another assembly that references it. The accessibility level of public members of a type differs.
Private variables can only be accessed by code in the same class or struct.
So, other classes can reference public variables, while private variables cannot. In Unity, public fields are displayed in the Inspector.
Public fields are serializable by default, just like the areas you mark with SerializeField.
You might prefer SerializeField rather than public because you may want to see your variable in the Inspector but keep it non-accessible.
There are four different statuses that your variables can have.
- They might be public, showing up in Inspector, and also accessible by other scripts,
- They might be [SerializeField] private, showing up in Inspector, but not accessible by other scripts,
- They might be [HideInInspector] public, doesn’t show in Inspector but are accessible by other scripts
- They might be private, neither shown in Inspector nor accessible by other scripts.
If you wish to see SerializeField in action, I suggest this piece from Dev Community and the video above.