Adopt Database in Unity3D! SQLite Manager make easy to read and update your local data. Use a real database to store environment data, player settings, gameplay variables or whatever is in your mind. Query with SQL from simple schema is much easier than manipulate local textfiles for the same reason.
**TODO:** Everything!
Under development, the syntax might change in the future. Although I try to make it safe please use it at your own risk, I haven’t used it in production yet.
Tested only with Unity 2018.1.1f1 for Windows, Linux/Mac/Mobile support is under blockchain kripto’er. (sic)
A C# wrapper class around Mono’s SQLite functions to make it easier and faster to use.
Find some basic codes with the plugin. Check out the test file for more usage example.
SQLiteManager db;
try {
db = new SQLiteManager("Game.db");
} catch (SqliteException e) {
Debug.Log("Sqlite exception: " + e.Message);
}
public class User
{
public int UserID { get; set; }
public string Name { get; set; }
}
using (IDbCommand dbcmd = db.Connection.CreateCommand()) {
dbcmd.CommandText = "INSERT INTO `User` (`UserId`, `Name`) VALUES (1, 'John Doe'), (2, 'Jane Doe');";
dbcmd.ExecuteNonQuery();
}
User user = db.GetObj<User>(1);
User user = db.GetObj<User>("SELECT * FROM User WHERE UserID = 1");
List<User> users = db.GetObjList<User>("SELECT * FROM User");
We gonna store the database connection variable in a static class, the connection will be always available for a command regardless of the scene. All the steps of this workflow already exist on project SampleScreen.
Unity Settings
Use creaky C# version. Go to menu _File, Build Settings…, Player Settings…, Other Settings, Scripting Runtime Version_ and set to recent .NET 4.x Equivalent.
Add Container
To achieve connection container use PersistData. It’s a basic but effective class of mine just handy to store anything in a HashMap. Included to this repository.
Create a new empty game object, name it Persist Data. In the Inspector window select Add Component, Miscellaneous, Persist Data for initialising the static class.
Create Database
There is no plan to create an enormous database admin inside the Unity editor in the near future by me. It would be pointless. There is plenty of 3rd party software for this job. Just find one you like, install it and get ready to create your game .db file. The entire database is in a single file, just think about how simple will be its online synchronisation.
I recommend using DB Browser for SQLite open source desktop app. Very straightforward, create a new database, create the schema and insert potential initial records.
Create or open the Streaming Assets folder into your Assets directory. Copy here your Game.db file.
Wake Up Database
Create a new empty game object, name it Game Manager. Create InitPersistData.cs script and connect to the SQLite database at the start.
if (!PersistData.Instance.Has(PDKey.Database)) {
SQLiteManager db = null;
UseDatabase udb = GetComponent<UseDatabase>();
try {
db = new SQLiteManager(udb.GetDatabaseFilePath());
} catch (Exception e) {
udb.UnselectBinary();
Debug.LogException(e);
}
if (db.IsOpen()) {
PersistData.Instance.Set(PDKey.Database, db);
}
}
Drop InitPersistData.cs script to the Game Manager Inspector window.
Create Helper Class
Create PD.cs script and add a shorter connection syntax for SQLite.
public static SQLiteManager Database() {
return (SQLiteManager)PersistData.Instance.Get(PDKey.Database);
}
Drop PD.cs script to the Game Manager Inspector window.
Additional Component
If you’d like to test the connection or get disappointed of lack of functions just simply add this component Add Component, SQLite Manager, Manage Database. This component has a chance to become big.
Ready To Use
At this point, your database should be ready to use without any error message on the Console window. Game Manager Inspector window should look like below.
using DemoLand.PersistData;
using DemoLand.SQLiteManager;
PersistData.Instance.Set("db", db);
string data = ((SQLiteManager)PersistData.Instance.Get("db")).GetObj<User>(1).Name;
Raw public method list, more info in the source code.
The first version will come with the first release, once all the essential functions are there.