UGTK / Toolkit / Data Systems / Dictionary System Completed
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:
- Create a new class derived from
SO_Dictionary<T, T1>definingTandT1:
```csharp
using UGTK.Data.DictionarySystem;
CreateAssetMenu(menuName = "UGTK/Dictionary/ExampleDictionary", fileName = "ExampleDictionary")]
public class SO_ExampleDictionary : SO_Dictionary
{
// Your class implementation here
}
```
- Create you Scriptable Object instance:
- Add Elements:
- 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:
- Add in you code the struct and define the data types
TandT1that will be stored in the dictionary:
```csharp
[SerializeField] public S_Dictionary
```
- 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):
- Retrieve All Keys:
List<T> keys = myDictionary.Keys();
- Retrieve All Value:
List<T1> keys = myDictionary.Keys();
- Add a New Key-Value Pair:
T elementKey;
T1 elementValue;
myDictionary.Add(elementKey, elementValue);
- Add a New Key-Value Pair By BiType Element:
T elementKey;
T1 elementValue;
myDictionary.Add(new BiType<T,T1>(elementKey, elementValue));
- Get Value by Key:
T key;
T1 value = myDictionary.GetValue(key);
- Get Key by Value:
T1 value;
T key = myDictionary.dictionary.GetValue(value);
- Remove Key-Value Pair by Key:
T key;
myDictionary.RemoveKey(key);
- Remove Key-Value Pair by Value:
T1 value;
myDictionary.RemoveValue(value);
- Remove a Specific Element:
T key;
T1 value;
myDictionary.RemoveElement(key, value);
- Remove a Specific Element by BiType:
T key;
T1 value;
myDictionary.RemoveElement(new BiType<T, T1>(key, value));
- Check if Dictionary Contains Key:
T key;
bool hasKey = myDictionary.HasKey(key);
- Check if Dictionary Contains Value:
T1 value;
bool hasKey = myDictionary.HasValue(value);
- Check if Dictionary Contains the element:
T key;
T1 value;
bool hasElement = myDictionary.HasElement(key, value);
- Check if Dictionary Contains the element by BiType:
T key;
T1 value;
bool hasElement = myDictionary.HasElement(new BiType(key, value));
- Clear All Keys:
myDictionary.ClearKeys();
- Clear All Values:
myDictionary.ClearValues();
- 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:
- 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. - Get Value — returns 100, the value just stored.
- Convert To Dictionary —
ToDictionary()hands back a plainDictionary<TKey, TValue>with the same entries, for code that wants the standard type. - Remove Key, then Get Value again. This is the frame worth stopping on: the read returns
0, not an error.
0isint'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. - 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
-
SO_Dictionary
: A generic ScriptableObject class designed to store dictionary data, allowing manipulation through the Unity editor. -
UGTK.Data.DictionarySystem.Dictionary
: A serializable struct for implementing dictionaries, supporting Unity's serialization system and facilitating code operations. -
BiType
: A helper struct representing a key-value pair, used for bulk addition and removal of elements in the dictionary. -
Dictionary Extension: Static methods to enhance dictionary functionality, including
Keys(),Values(),Add(),RemoveKey(),RemoveValue(),GetValue(),HasKey(),HasValue(),Clear(), and methods for checking elements and retrieving keys and values.
© 2026 Marcello De Bonis. All rights reserved
Depends on 0
- Standalone: no other module required.
Measured from the repository: code references (asmdef) plus prefab and asset GUIDs. Importing this module alone brings in 0 modules in total.
UGTKengine within an engine

