Let’s take a look at the LiteDB - a noSql alternative for current gaming storage standards.
In the last ten years, noSql solutions are slowly seeping into the basic set of development tools. It is understandable since the capabilities provided by relational databases not needed so often, and the performance gained with migration to pure noSql is never excessive. In the article about choosing the data storage method for Unity, we concluded that the de facto industry standard is SQLite, which can be adapted not only for the relational use case but also in the form of a noSql solution. The pure noSql solutions are not so popular and are the brightest examples of open source projects. However, as noted earlier, if your soul is hungry for adventure, and the project does not claim to take over the world, then why not give it a try.
LiteDB
LiteDB is an embedded noSql database written in .NET, which makes it easy to use in projects on the latest Unity versions. It is a small project with a small bus factor, but it has everything you need to use as a full-fledged embedded database. Databases are not a big problem in games creating, so there are not so many popular solutions, and LiteDB is the most visible option among the SQLite alternatives. More precisely, it is at least somehow noticeable, and you can find at least some information on its application. For example, one such article is a blog post by Mark Hedberg, where he shares his personal experience with its use. The second one is from Andrey Podkolzin, in which he compares performance between LiteDb, SQLite, and flat files.
Documentation
Pretty concise documentation can be found on the official website.
Internal organization
The description of the database’s internal structure can be found in the corresponding section of the documentation - Data Structure.
Setup
Go to the package page on nuget.org, download the version you need from the link Download package, rename the extension of the downloaded file to .zip, unzip and copy the version of Dll suitable for your Scripting Backend to the project.
Also you need to create a file link.xml with those settings (thanks to TigerHix for his gist)
Since the code of the real project is not very suitable for open demonstration, a minimal implementation was created that simulates actual application.
usingSystem.IO;usingSystem.Linq;usingLiteDB;usingUnityEngine;usingUnityEngine.UI;// POCO Object for storage
publicclassGameData{// Id needed to store object in database
publicintId{get;set;}// Click counter
publicintCounter{get;set;}}// Database usage class
publicclassDatabaseManager:MonoBehaviour{// Link to counter text
publicTexttext=null;// Parameters of access to the database
privatestring_connectionString;privatestring_databaseName;// Start is called before the first frame update
voidStart(){// Configure access settings
_connectionString="Filename="+Path.Combine(Application.persistentDataPath,"database");_databaseName="gameData";// Display initial data
text.text=GetGameData().Counter.ToString();}// Click the counter increase button
publicvoidIncreaseClick(){// Get an object from the database
GameDatagameData=GetGameData();// Increment counter
gameData.Counter+=1;// Save the object to the database
StoreGameData(gameData);// Update the text through receiving the object (for testing purposes only)
text.text=GetGameData().Counter.ToString();}// Click the cleanup database button
publicvoidClearClick(){// Clear the database
ClearData();// Update text
text.text=GetGameData().Counter.ToString();}// Save the object to the database
privatevoidStoreGameData(GameDatagameData){// Connect to the database
using(vardb=newLiteDatabase(_connectionString)){// Get the collection (or create if it does not exist)
varcol=db.GetCollection<GameData>(_databaseName);// Add or update an object
col.Upsert(gameData);}}// Get the object from the database
privateGameDataGetGameData(){// Connect to the database
using(vardb=newLiteDatabase(_connectionString)){// Get the collection (or create if it does not exist)
varcol=db.GetCollection<GameData>(_databaseName);// Create a new object if it is not in the database
if(col.Count()==0){GameDatagameData=newGameData();gameData.Counter=0;returngameData;}// Get the object from the database
varresult=col.FindAll();returnresult.First();}}// Clear the database
privatevoidClearData(){// Connect to the database
using(vardb=newLiteDatabase(_connectionString)){// Get the collection (or create if it does not exist)
varcol=db.GetCollection<GameData>(_databaseName);// Clear Collection
col.DeleteAll();}}}
Result
It just works =)
Disadvantage
The primary negative side of using this database is the lack of a tool for viewing its contents on platforms other than Windows, but you can solve it on our own.
Conclusion
The standard method for storing information for projects on Unity is SQLite. It is a simple database from which it is clear what to expect. Changing it for something fashionable without serious reason for that makes no sense. However, if you are starting a new project, then it is quite possible to consider LiteDB as an alternative. The experience of using this database in a personal project did not reveal any negative sides, so we can try to use it for more critical projects, which we will talk about in the future. See you next time! =)