Skip to content

Dictionary

The serializable dictionary allows you to have the whole fully fledged functionality of System.Collections.Generic.Dictionary but also allows you to automatically serialize its values.

To create a serializable dictionary, simply inherit from TSerializableDictionary<TKey, TValue>. For example, to create a dictionary that uses string as its key and GameObject as its value:

[Serializable]
public class MyDictionary : TSerializableDictionary<string, GameObject>
{ }

You can now create a dictionary that automatically serializes its values and use it as any normal dictionary:

public class MyComponent : MonoBehaviour
{
    public MyDictionary dictionary = new MyDictionary();

    private void Awake()
    {
        // Add element to dictionary:
        this.dictionary.Add("Hello World", this.gameObject);

        // Print element added
        Debug.Log(this.dictionary["Hello World"].name);
    }
}