kkukshtel / castledb-unity-importer

Use CastleDB files in Unity with some extra sugar
MIT License
99 stars 8 forks source link

CastleDB Unity Importer

Requires Unity 2018 + Scripting Runtime 4.X
Define all your data in CastleDB and then directly access your data in code in Unity with full intellisense support!

Made in part as a database tool for Cantata.

General Information

NOTE: While this tool still works, the repo is in maitencne mode as most of my efforts are now directed to Depot. Depot aims to replicate most of the functionality of CastleDB, but in a context seperate from Haxe and without needing a separate application (it runs in VS Code).

Getting Started

Construct some data in CastleDB: CastleDB Example

Import the repo's .unitypackage release into Unity (or open the repo project directly), and then add your .cdb file somewhere in your Assets directory. Unity will import the .cdb file and generate it's classes.
NOTE: Your project needs to be using the latest .NET 4.X scripting runtime. For instructions on how to change this, see Unity's documentation here.

Open your code editor, and access your data like this:

// Inspector reference to imported asset
public TextAsset CastleDBAsset;
...
// Somewhere else inside your code
CastleDB DB = new CastleDB(CastleDBAsset);
Creature dragon = DB.Creatures.Dragon;

Sheets get converted into types that allow you to strongly reference your data.

// Get the Dragon from the Creatures sheet and get some of its properties
CastleDB DB = new CastleDB(CastleDBAsset);
Creature dragon = DB.Creatures.Dragon;
string dragonName = dragon.Name;
Creature.DeathSoundEnum = dragon.DeathSound;
// You can also easily access nested items:
// In this example, we get the first Drop from a Dragon and get that Drop's first possible Effect.
Effect effect = DB.Creatures.Dragon.Drops[0].PossibleEffects[0];
string effectName = effect.name;
int effectChange = effect.change;

The types update as you update your database schema.

Possible Uses

This plugin was built as a way to seperate core game data from Unity, as was the intention with CastleDB's intended Haxe use case. This means that you can move almost any static data into CastleDB and remove the need to keep track of it in code.

Possible uses include:

Basically, anytime you have a large list of things you need to have in your game, you can just make a sheet in CastleDB and then access that data inside Unity from this integration.

Another benefit is that the .cdb file is just JSON, meaning it versions well in source control, so tracking data changes is much easier.


Extended Information

Read on for a deeper dive on how to use this plugin as well as information about CastleDB and this plugin in general.

What is CastleDB?

From the CastleDB site:
CastleDB is used to input structured static data. Everything that is usually stored in XML or JSON files can be stored and modified with CastleDB instead.

Why this Plugin?

Read my blog post about this here
Unity doesn't provide an easy way to globally manage game data, so this plugin is an attempt to fix that. CastleDB was built for this explicit purpose, but until now has only had an easy interop with games made in Haxe. This plugin is meant to provide Unity developers with not only CastleDB support but also the ability to use the database like the original Haxe API. This means allowing the developer to strongly reference data values in code by type, without having to roll their own data interpretation layer.

Full Plugin Guide

To use this plugin, download and import the latest Unity package from the releases page. This repo can also directly be opened as a Unity project file and includes a sample CastleDB (.cdb) file.

If you want to work with the sample file further or create your own data, download and install CastleDB from here.

NOTE: Your project needs to be using the latest .NET 4.X scripting runtime. For instructions on how to change this, see Unity's documentation here.

If you are opening this repo as a project, you will see the Assets/TestFolder/GeneratedTypes folder by default (complete with generated code from the provided .cdb file).

If you are importing the package into an existing Unity project, you'll need to import or create a .cdb file for those default folders to generate. If you're just getting started, I recommend using the .cdb file in this repo.

When you add a .cdb file to your Unity project with this plugin, this plugin uses Unity's ScriptedImporter API to tie into the import process and generate code based on the contents of the .cdb file. Any time you make a change or reimport your .cdb file, this code will be auto-generated.

Once the code is generated, reload your solution and then get to working with your data!

Data Autocomplete Example

If you're using the repo, you can open up the Test.scene file and click the "Test" checkbox on the TestObject object (works in edit mode as well) and verify that the console prints out the proper values from the .cdb file.

Because this plugin does code generation, it's important to understand what is happening and structure your database accordingly. The main concepts employed are:

Try adding in new sheets/rows/columns via CastleDB to the default provided CastleDB file to get a feel for how the code generation works. Also be sure to check out the Limitations section below for some general guidelines on how to use CastleDB with Unity.

Config File

A config file is provided in the .unitypackage and repo that allows you to define the general behavior of the code generation by allowing you to set custom values for three diffferent fields:

Additional API

The generated CastleDB file provides a .GetAll() function for any defined sheet type. This is useful if you just want to grab all of a given Class' instances instead of needing to reference them one by one.

Limitations

While this plugin covers the general use cases of CastleDB, the fact it was not meant to be used in Unity means there are some limitations to using this plugin.

All sheets need to have a GUID column that defines the name of a row
This defaults to "id" with a "Unique Identifier" type. Note that this doesn't apply to list types, only "top-level" sheets.

You can only have one .cdb file in your project
If you find the need to use another .cdb file, you can very likely just use another sheet in your preexisting .cdb file.

Objects are created when requested and do not share direct internal references
This means that if you have two Creatures and they both reference a Location called Mountain, and you create those two creatures, the Location field of the creatures will point to different instances of Mountain (as each creature creates its own Mountain during retrieval).

Circular references of any kind will likely break object creation
Don't make a row in a sheet (Sheet A) with a reference type column that points to a row in another sheet (Sheet B) with a reference type column that points to a row in the first sheet (Sheet A) or you will likely crash your application.

If you add more columns or rows to a given type, the solution in Unity will properly update but you're need to reload your solution in your editor
This manifests as previous mentions of your type in code saying "Can't be found". To fix this, just reload the assembly in your editor. In VSCode, this is as easy as just clicking your .sln file at the bottom of the window and reselecting it.

There is no validation of column names, so column names in CastleDB need to be valid C# field names
I.E. don't use spaces in your column names, weird characters, etc. This also applies to text in your GUID column because these names become strongly typed. Because of this, make your GUID name something easy to remember/use, and have an additional text column that is the actual name of your row.

TODO

While this repo is ready to be used for general purpose cases, there is still some work to be done! Happy to accept any pull requests that tackle these issues.

References

Here's a list of resources that made a lot of this possible.

First off, this plugin could not have been possible without SimpleJSON. Thanks to all those who worked on this great library!

CastleDB Wedbsite and Repo
Best CastleDB Tutorial/Walkthrough
Unity ScriptedImporter Docs
Game From Scratch's CastleDB Overview