A simple way to create a database with global access and persistence, using ScriptableObjects
Table of Contents
The BaseDatabase<T>
class provides a generic implementation for managing a collection of items of type T
, where T
must implement IDatabaseItem
. It offers methods to add, remove, and search for items in the database, ensuring unique IDs for each item. It also includes support for data integrity in the Unity editor and can be easily extended for different types of databases.
To get a local copy up and running follow these simple example steps.
- Download or fork this project
- Move the content to your Unity project (if preferred, only the Scripts folder is necessary)
- Create the ScriptableObject that will be responsible for storing and providing global access to all created databases, by right-clicking on the desired folder in the Project tab window and choosing the option
Create > Database > Database Collection
- Create a new script that inherits from
BaseDatabase<T>
. Example:PlayerProfileDatabase.cs
using UnityEngine; [CreateAssetMenu(menuName = "Database/Sample/Player Profile Database", fileName = "PlayerProfileDatabase")] public class PlayerProfileDatabase : BaseDatabase<PlayerProfile> { }
- Create an instance of the global variable by right-clicking on the desired folder in the Project tab window and choosing the option (in this example)
Create > Database > Sample > Player Profile Database
- Give your new database a name, for example,
PlayerProfileDatabase
- Assign the new instance of the
PlayerProfileDatabase
to theDatabaseCollection
ScriptableObject - Now, to access the PlayerProfileDatabase, in any MonoBehaviour script, you can use the static call
DatabaseCollection.TryGet(out PlayerProfileDatabase playerProfileDatabase);
LuizThiago - @CodeLuiz - [email protected]
Project Link: https://github.com/LuizThiago/SimpleScriptableObjectDatabase