UGTK cat mascotUGTKengine within an engine Request access

UGTK / Toolkit / Data Systems / Dictionary System Completed

Data Systems — UGTK

Dictionary System

Summary

The Dictionary System within the Unity Games Toolkit (UGTK) provides a set of static extension methods for enhancing the functionality of a serializable dictionaries implemented using SO_Dictionary<T, T1> by scriptable object or the struct S_Dictionary . These methods facilitate operations such as retrieving keys and values, adding, removing, and checking for elements within the dictionary.


Content


Modules Dependencies

The Dictionary System module does not have external dependencies within the UGTK. It can be used independently.


Setup

You can choose to use either the ScriptableObject or the serializable struct of a dictionary.

For the ScriptableObject:

  1. Create a new class derived from SO_Dictionary<T, T1> defining T and T1:

```csharp

using UGTK.Data.DictionarySystem;

CreateAssetMenu(menuName = "UGTK/Dictionary/ExampleDictionary", fileName = "ExampleDictionary")]

public class SO_ExampleDictionary : SO_Dictionary

{

   // Your class implementation here

}

```

  1. Create you Scriptable Object instance:

  1. Add Elements:

  1. Use the dictionary in your scriptable Object:

```csharp

using UGTK.Data.DictionarySystem;

public class MyScript: MonoBehaviour

{

   [SerializeField] public SO_ExampleDictionary SO_PlayerScores;

   private S_Dictionary<string, int> playerScores;



   public void Start()

   {

      playerScores = SO_PlayerScores.dictionary;

   }

}

```

For the Serializable struct:

  1. Add in you code the struct and define the data types T and T1 that will be stored in the dictionary:

```csharp

[SerializeField] public S_Dictionary PlayerScores;

```
  1. Add Elements:


How To Use

To enhance the functionality of dictionaries implemented using S_Dictionary<T, T1>, substitute T and T1 with other types when you copy the code and use the following extension methods for manipulate the dictionary (If you're using any SO_Dictionary<T, T1> istead of the struct type, remember of use myDictionary.dictionary instead of myDictionary):

  1. Retrieve All Keys:



     List<T> keys = myDictionary.Keys();



  1. Retrieve All Value:



     List<T1> keys = myDictionary.Keys();



  1. Add a New Key-Value Pair:



     T elementKey;

     T1 elementValue;



     myDictionary.Add(elementKey, elementValue);



  1. Add a New Key-Value Pair By BiType Element:



     T elementKey;

     T1 elementValue;



     myDictionary.Add(new BiType<T,T1>(elementKey, elementValue));



  1. Get Value by Key:



    T key;

    T1 value = myDictionary.GetValue(key);



  1. Get Key by Value:



    T1 value;

    T key = myDictionary.dictionary.GetValue(value);



  1. Remove Key-Value Pair by Key:



    T key;

    myDictionary.RemoveKey(key);



  1. Remove Key-Value Pair by Value:



    T1 value;

    myDictionary.RemoveValue(value);



  1. Remove a Specific Element:



     T key;

     T1 value;

     myDictionary.RemoveElement(key, value);



  1. Remove a Specific Element by BiType:



     T key;

     T1 value;

     myDictionary.RemoveElement(new BiType<T, T1>(key, value));



  1. Check if Dictionary Contains Key:



     T key;

     bool hasKey = myDictionary.HasKey(key);



  1. Check if Dictionary Contains Value:



     T1 value;

     bool hasKey = myDictionary.HasValue(value);



  1. Check if Dictionary Contains the element:



     T key;

     T1 value;

     bool hasElement = myDictionary.HasElement(key, value);



  1. Check if Dictionary Contains the element by BiType:



     T key;

     T1 value;

     bool hasElement = myDictionary.HasElement(new BiType(key, value));



  1. Clear All Keys:



      myDictionary.ClearKeys();



  1. Clear All Values:



      myDictionary.ClearValues();



  1. Clear Dictionary:



      myDictionary.Clear();



Testing Scene

Open Debug/DictionaryScene.unity. Besides the older ExampleScriptUseDictionary object, it holds DictionarySystemTest carrying Mn_DictionarySystemTest. Every operation is on the component's context menu: right-click the header in the Inspector, with no play mode.

What to do, in order:

  1. Add — the pair in Key and Value goes into Sample, and the field shows it in the Inspector like any serialised list. That is the whole point of S_Dictionary: a real dictionary that Unity can serialise and an artist can edit.
  2. Get Value — returns 100, the value just stored.
  3. Convert To DictionaryToDictionary() hands back a plain Dictionary<TKey, TValue> with the same entries, for code that wants the standard type.
  4. Remove Key, then Get Value again. This is the frame worth stopping on: the read returns 0, not an error. 0 is int's default, not a value that was found, and nothing is raised. A dictionary of ints cannot tell "missing" from "zero" through the getter alone — check the key exists first when the difference matters.
  5. Clear empties it.

One more thing the component reports and the docs are easy to skip: duplicate keys. S_Dictionary is a serialised list underneath, so nothing stops two identical keys being typed into the Inspector. ToDictionary() keeps the first of each and drops the rest, silently, which is why the component names the duplicate when it finds one.


Technical Info

The Dictionary System module is composed of some key scripts and structures to manage and extend the functionality of serializable dictionaries.

Scripts


© 2026 Marcello De Bonis. All rights reserved

Real dependenciesOpen in the map →

Measured from the repository: code references (asmdef) plus prefab and asset GUIDs. Importing this module alone brings in 0 modules in total.