UGTK / Toolkit / Data Systems / Database System Completed
Database System
Summary
The Database System within the Unity Games Toolkit (UGTK) provides a flexible and generic framework for managing serializable elements in a database. It allows various types of data to be stored and managed efficiently. This system includes a generic DatabaseElement class for individual data entries and an abstract SO_Database class for creating databases as ScriptableObjects. Additionally, custom property drawers enhance the Unity Inspector experience by providing ID generation and copy functionality.
Content
Modules Dependencies
The Database System does not have external dependencies within the UGTK. It can be used independently.
Setup
To set up the Database System, follow these steps:
-
Create a
structwith[Serializable]as property. This will be your struct class to use into your database```csharp
[System.Serializable] public struct MyStruct { public int MyInt; public float MyFloat; public string MyString; }
```
-
Create a class and derive the class from
SO_Database<T>whereTis the type of data you want to store.:```csharp
using UGTK.Data.DatabaseSystem;
[UnityEngine.CreateAssetMenu(menuName = "UGTK/Database/MyDatabase", fileName = "MyDatabase")] public class ExampleDatabase : SO_Database
{ // Your class implementation here } ```
-
Create Database Instance:
- Manipulate Database Elements:
- Use Elements in the project
How To Use
SO_Database\<T>
- Add Element (Editor only):
SO_Database<T> database = // get your database instance
database.AddElement("MyElementID", myData);
- Modify Element (Editor only):
SO_Database<T> database = // get your database instance
database.ModifyElement("MyElementID", newData);
- Modify Element (Editor only):
SO_Database<T> database = // get your database instance
database.ModifyElement("MyElementID", newData);
- Delete Element (Editor only):
SO_Database<T> database = // get your database instance
database.DeleteElement("MyElementID");
- Get Element:
SO_Database<T> database = // get your database instance
var element = database.GetElement("MyElementID");
if (element.Item1)
{
// Element found, element.Item2 contains the data
}
else
{
// Element not found
}
- Get All Elements:
SO_Database<T> database = // get your database instance
List<DatabaseElement<T>> allElements = database.GetElements();
DatabaseElement
- Create a Database Element:
DatabaseElement<T> newElement = new DatabaseElement<T>("MyElementID", myData);
- Set Element ID::
newElement.Setid("MyNewElementID");
- Set Element Info::
newElement.SetInfo(newData);
- Get Element ID:
string id = newElement.Getid();
- Get Element Info:
T info = newElement.GetInfo();
Testing Scene
Open DatabaseScene in the module's Debug/ folder. The scene wires an
ExampleDatabaseUser to a simple and a randomic database; the grid one is an asset, in
Debug/Scriptables/ExampleIntGridDatabase, and the GIF below is its inspector.
What to do, in order:
- Add elements to a simple database and read them back by id.
- Add two elements with the same id.
- Read an id that does not exist.
- Draw repeatedly from a randomic database and check the weights are respected.
- Select ExampleIntGridDatabase and use Grid Size: the
-1/+1buttons on Columns and Rows resize the table under them, and the cells you already filled keep their values.
Steps 2 and 3 are the pair worth pinning down before building on top: what a duplicate id does, and whether a miss returns a default or announces itself. Step 5 also confirms the grid inspector is applying — it is registered on UGTK's own database base, so it should claim these assets and leave every other ScriptableObject in the project alone.
Technical Info
The following are the main scripts for Database System. Refer to the UML section for the detailed structure.
Scripts:
- DatabaseElement
: Represents a serializable element within a database, storing data of type T. - DatabaseElementDrawer: Custom property drawer for DatabaseElement
, adding ID generation and copy functionality in the Unity Inspector. - SO_Database
: Represents a generic database as a ScriptableObject, managing a list of DatabaseElement .
© 2026 Marcello De Bonis. All rights reserved
Depends on 2
Measured from the repository: code references (asmdef) plus prefab and asset GUIDs. Importing this module alone brings in 2 modules in total.
UGTKengine within an engine

