Static structured database for Unity. I create this project inspired by CastleDB.
- Install
- How to use
- Get prefab from config
- Supported types
- Editor tools
- Attributes
- __GUID field
- In the plans
- FuryDB Components
First Install FDB. Create your database class DB.cs
:
using FDB;
using Newtonsoft.Json;
[JsonConverter(typeof(DBConverter<DB>))]
[FuryDB("Assets/Resources/DB.json.txt", "Assets/Kinds.cs")]
public class DB
{
}
Create folder Editor
and then create class Editor/DBWindow.cs
using UnityEditor;
using FDB.Editor;
public class DBWindow : DBInspector<DB>
{
[MenuItem("Game/DB")]
public static void Open()
{
var window = GetWindow<DBWindow>("DB");
window.Show();
}
}
Then open menu Game -> DB and look at window. Now you have empty database.
Now lets reach DB.cs
with few types
using FDB;
using Newtonsoft.Json;
[JsonConverter(typeof(DBConverter<DB>))]
[FuryDB("Assets/Resources/DB.json.txt", "Assets/Kinds.cs")]
public class DB
{
public Index<UnitConfig> Units;
public Index<WeaponConfig> Weapons;
public Index<TextConfig> Texts;
}
public class UnitConfig
{
public Kind<UnitConfig> Kind;
public Ref<TextConfig> Name;
public int Str;
public int Dex;
public int Int;
public int Chr;
public Ref<WeaponConfig> Weapon;
}
public enum WeaponType
{
Melee,
Range
}
public class WeaponConfig
{
public Kind<WeaponConfig> Kind;
public Ref<TextConfig> Name;
public WeaponType Type;
public int Damage;
public int DamageVar;
}
public class TextConfig
{
public Kind<TextConfig> Kind;
public string En;
public string Ru;
}
We have main DB
class with three pubic fields
public Index<UnitConfig> Units;
public Index<WeaponConfig> Weapons;
public Index<TextConfig> Texts;
This is three tables you can to edit. Every table have types: UnitConfig
WeaponConfig
or TextConfig
Important
Every class in Index must contains field Kind
Look to class UserConfig it have following fields
Kind<UnitConfig> Kind
- this is unique name of config. If it possible this kind exports to fileKinds.cs
. If kind not possible to export it mark little darkness and eye iconRef<TextConfig> Name
- this mean that unit refered to config fromIndex<TextConfig>
you first need goto to pageTexts
add line and after select this line inUnit.Name
fieldStr
Dex
Int
Chr
areint
. This just numberRef<WeaponConfig> Weapon
- this is reference again but to tableWeapons
}
Fill database with data.
Now you can load database in your code
class Boot {
public static DB DB { get; private set; }
void Awake() {
DB = DBResolver.Load<DB>();
foreach (var unit in DB.Units.All()) {
Debug.Log(unit.Kind);
}
}
}
You also can access for db items using Kinds.cs
:
class Boot {
public static DB DB { get; private set; }
void Awake() {
DB = DBResolver.Load<DB>();
var rogue = DB.Units.Get(Kinds.Units.Rogue);
}
}
If you need attach prefab MonoBehaviour(Prefab) or ScriptableObject to your config you need Addressables and types AssetReference
or AssetReferenceT<>
Well you have prefab
Browse prefab in hierarchy window and turn on toggle Addressable
Add field Prefab
in UnitConfig
public class UnitConfig
{
public Kind<UnitConfig> Kind;
public Ref<TextConfig> Name;
public AssetReference Prefab; // <<<<
[Space]
public int Str;
public int Dex;
public int Int;
public int Chr;
[Space]
public Ref<WeaponConfig> Weapon;
}
And drag prefab into unit field
Add code for load prefab int Boot
class Boot : MonoBehaviour
{
public static DB DB { get; private set; }
private async void Awake()
{
DB = DBResolver.Load<DB>();
var warrior = DB.Units[Kinds.Units.Warrior];
var prefab = await warrior.Prefab.InstantiateAsync().Task;
}
}
Run game and look to result.
Read more about addressable and resource management in Internet.
For read and edit database from editor use EditorDB<DB>.DB
Warning
EditorDB available only in UNITY_EDITOR
When you modify some data from EditorDB<DB>
call EditorDB<DB>.SetDirty()
- bool
- int
- float
- string
- enum
- Color
- AnimationCurve
- List<>
- Ref<>
- AssetReference
- AssetReferenceT<>
UnityEngine.SpaceAttribyte
add vertical space between columns
public class UnitConfig
{
public Kind<UnitConfig> Kind;
public Ref<TextConfig> Name;
[Space]
public int Str;
public int Dex;
public int Int;
public int Chr;
[Space]
public Ref<WeaponConfig> Weapon;
}
Separete lines using regexp
public class DB
{
//...
[GroupBy("Kind", @"(.+?)_")]
public Index<TextConfig> Texts;
}
public class UnitConfig
{
///...
[Aggregate("Sum")]
public List<int> Levelups;
private static int Sum(int a, int b)
{
return a + b;
}
}
public class DB
{
[GroupBy("Kind", @"(.+?)_")]
[Aggregate(nameof(CalcTextChars), typeof(TextAgg))]
public Index<TextConfig> Texts;
private static TextAgg CalcTextChars(TextAgg agg, TextConfig config)
{
agg.EnChars += config.En.Length;
agg.RuChars += config.Ru.Length;
return agg;
}
private class TextAgg
{
public int EnChars;
public int RuChars;
public override string ToString()
{
return $"EN chars = {EnChars}\nRU chars = {RuChars}";
}
}
}
public class TextConfig
{
public Kind<TextConfig> Kind;
[MultilineText(MinLines = 3, Condition = "IsMultiline")]
public string En;
[MultilineText(MinLines = 3, Condition = "IsMultiline")]
public string Ru;
static bool IsMultiline(TextConfig config)
{
var kind = config.Kind.Value;
if (kind == null)
return false;
return kind.Contains("Description_");
}
}
You have a way to quickly create links to other tables an attribute AutoRef
:
public class UnitConfig
{
public Kind<UnitConfig> Kind;
[AutoRef(Prefix ="UnitName_")]
public Ref<TextConfig> Name;
//
}
Press "Create" and start edit TextConfg-record
New line insert in the end of group of same lines
You can declare filed __GUID
in any object.
class UserConfig {
public string __GUID; // Not visible in DBWindow but work!
public Kind<UserConfig> Kind;
}
Field automatic fill GUID values
See projects with opened issues
Use FuryDB Components package for integrate database with unity