In the last blog, we talked about how you can store data using ScriptableObjects. ScriptableObjects are great for storing static or design-time data.
But what if your game needs to store huge amounts of data?
I’m talking hundreds or thousands of records — dialogues, inventories, quest data, item lists, etc. JSON or XML will start slowing down at that point.
And that’s exactly what we’re going to talk about today.
🤔What is SQLite?
SQLite is a free and open-source relational database. It’s lightweight, self-contained, and perfect for storing large amounts of structured, queryable data. It’s also cross-platform and works extremely well inside Unity.
SQLite is a great choice when you want to store data that is both huge and queryable, for example:
➡️Item lists
➡️Inventories
➡️Dialogue trees
➡️NPC data
➡️Quest systems
➡️Any dataset you need fast search/filter operations on
⚙️Setting up SQLite in Unity
Setting up SQLite is not as straightforward as PlayerPrefs or ScriptableObjects, but don’t worry — I’ll walk you through everything. Just follow the steps exactly.
⚠️Prerequisites
You should know the very basics of SQL — what a table is, what a query is, and how SELECT/INSERT work.
⚙️Initial Setup
- ➡️Download or clone the repo: on your PC.
- ➡️Create a new Unity project.
- ➡️Copy the following folders/files from the repo into your project in the exact same locations:
- - ➡️Assets/Plugins folder
- - ➡️Scripts/DataService.cs and Scripts/SQLite.cs
- - ➡️StreamingAssets folder
- ➡️Download and install DB Browser for SQLite.
🤔What are the DataService and SQLite scripts?
✏️DataService
This class creates a connection to your database using SQLite’s methods. Once the connection is open, you can query or store data just by calling DataService methods. You mainly need its constructor and the helper functions inside it.
✏️SQLite script
This is the interface that provides low-level methods for opening/closing connections, creating tables, inserting data, deleting data, etc.
💡Note:
You don’t need to understand how the full implementation works internally. You only need to know how to use the DataService to interact with your database.
🤔What are the .db files inside StreamingAssets?
These are the actual SQLite database files.
Unity keeps everything inside StreamingAssets as-is, meaning the database file is copied exactly into the build. SQLite then opens this file and reads/writes data during gameplay.
🚀Creating Your Own Database
You can do this in two ways:
➡️Option A:
Duplicate any .db file from StreamingAssets, rename it, open it in DB Browser, and clear the existing data so you can add your own later.
➡️Option B:
Rename one of the existing .db files and delete all the data inside it. You will write fresh data using Unity scripts.
🔗Setting up a Connection
To connect to your database, create an instance of the DataService class in your main script:
DataService dataService = new DataService("YourDatabaseName.db");
This opens the connection and prepares everything for inserting or reading data.
📑Creating a Table
First, define the structure of your table using a C# class.
For example, here is a simple Word table:
public class Word
{
public string Text { get; set; }
public int IsUsed { get; set; }
public int TextLength { get; set; }
}
Once your class is ready, you need to write a method inside DataService that creates the table:
public void CreateTable()
{
try
{
_connection.CreateTable<Word>();
}
catch (System.Exception e)
{
Debug.LogError(e.Message);
}
}
Similarly, you can then use the _connection object inside DataService to access all SQLite methods. You can check the SQLite class to see what methods are provided or paste the file into ChatGPT (or any AI) to understand the available functions.
Below is a basic example of DataService class to get you started:
public class DataService
{
private SQLiteConnection _connection;
// constructor will be here; I removed to keep the script simple. Make sure you have it.
public void CreateTable()
{
try
{
_connection.CreateTable<Word>();
}
catch (System.Exception e)
{
Debug.LogError(e.Message);
}
}
public void AddWordsToDatabase(List<Word> words)
{
try
{
_connection.InsertAll(words);
}
catch (System.Exception e)
{
Debug.LogError(e.Message);
}
}
public IEnumerable<Word> GetAllWords()
{
return _connection.Table<Word>();
}
public List<Word> GetWords(int wordLength, int numberOfWords)
{
List<Word> words = new List<Word>();
try
{
TableQuery<Word> result = _connection.Table<Word>()
.Where(x => x.TextLength == wordLength && x.IsUsed == 0)
.Take(numberOfWords);
foreach (Word word in result)
words.Add(word);
return words;
}
catch (System.Exception e)
{
Debug.LogError($"Error fetching words from database: {e.Message}");
}
return words;
}
public void DropTable()
{
try
{
_connection.DropTable<Word>();
}
catch (System.Exception e)
{
Debug.LogError(e.Message);
}
}
}
🗝️Key Takeaway
SQLite is perfect when your game needs to store large, structured, searchable data.
If your data grows beyond what JSON/XML or ScriptableObjects can handle easily, SQLite gives you a fast and scalable solution right inside Unity.
Top comments (0)