DEV Community

Cover image for [System.Serializable] in Unity
Ben
Ben

Posted on

[System.Serializable] in Unity

[System.Serializable] is a C# attribute that can be applied to a class, struct, or field to indicate that it can be serialized by Unity's serialization system. By doing which will allow developer to save data locally (or remotely via API) , and reload later. Here's a simple example of how to use the [System.Serializable] attribute:

[System.Serializable]
public class PlayerData
{
    public string name;
    public int score;
    public float health;
}
Enter fullscreen mode Exit fullscreen mode

The reason of using the [System.Serializable] is it can make a custom class Serializable, meaning store and reload from local device in text format, such as Json or xml.

What kind of data can be serialized

This includes:

  • Basic data types such as int, float, and string
  • Arrays of basic data types or other serializable objects
  • Classes that implement the ISerializable interface
  • Lists, dictionaries, and other collection types that contain serializable objects

However, some other types such as Transform can't be serialized, or custom data type that user define themselves. So how to serialize those kinds of data types?

Serialize advanced data types

To serialize advanced data types such as Transform, we can create a wrapper class to hold the values of a transform that we want to serialize. Here is an example:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TransformSerialize : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("transform: " + gameObject.transform);
        SerializableTransform st = new SerializableTransform(gameObject.transform);
        string json = JsonUtility.ToJson(st);
        Debug.Log("transform json: " + json);
        Transform tf = JsonUtility.FromJson<SerializableTransform>(json).DeserializableTransform()
        Debug.Log("transform json to object: " + tf);
    }

    // Update is called once per frame
    void Update()
    {

    }
}


[System.Serializable]
public class SerializableTransform
{
    public Vector3 position;
    public Quaternion rotation;
    public Vector3 scale;

    public SerializableTransform(Transform transform)
    {
        position = transform.position;
        rotation = transform.rotation;
        scale = transform.localScale;
    }

    public Transform DeserializableTransform()
    {
        GameObject tempGamObject = new GameObject();
        Transform newTransform = tempGamObject.transform; 
        newTransform.position = position;
        newTransform.rotation = rotation;
        newTransform.localScale = scale;
        return newTransform;
    }
}
Enter fullscreen mode Exit fullscreen mode

In above script, we have a SerializableTransform which can convert a Transform data Serializable. Also we can have a method which can revert it back DeserializableTransform.

πŸ‘‰ If you want received more stories like this, please subscribe my channel to get the latest update in time.

**Ref: **https://hackingwithunity.com/system-serializable-in-unity/

Top comments (0)