Skip to content

Latest commit

 

History

History
394 lines (299 loc) · 8.07 KB

README.md

File metadata and controls

394 lines (299 loc) · 8.07 KB

openupm GitHub issues

FuryDB

Static structured database for Unity. I create this project inspired by CastleDB.

How to use

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.

New window

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 file Kinds.cs. If kind not possible to export it mark little darkness and eye icon
  • Ref<TextConfig> Name - this mean that unit refered to config from Index<TextConfig> you first need goto to page Texts add line and after select this line in Unit.Name field
  • Str Dex Int Chr are int. This just number
  • Ref<WeaponConfig> Weapon - this is reference again but to table Weapons }

Fill database with data.

Units Weapons Text

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);
    }
}

Get prefab from config

If you need attach prefab MonoBehaviour(Prefab) or ScriptableObject to your config you need Addressables and types AssetReference or AssetReferenceT<>

Well you have prefab

Text

Browse prefab in hierarchy window and turn on toggle Addressable

Text

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

Text

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.

Text

Read more about addressable and resource management in Internet.

Editor tools

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()

Supported types

  • bool
  • int
  • float
  • string
  • enum
  • Color
  • AnimationCurve
  • List<>
  • Ref<>
  • AssetReference
  • AssetReferenceT<>

Space Attribute

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;
}

Text

GroupBy Attribute

Separete lines using regexp

public class DB
{
    //...
    [GroupBy("Kind", @"(.+?)_")]
    public Index<TextConfig> Texts;
}

Text

Aggregate Attribute

public class UnitConfig
{
    ///...
    [Aggregate("Sum")]
    public List<int> Levelups;

    private static int Sum(int a, int b)
    {
        return a + b;
    }
}

Text

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}";
        }
    }
}

Text

MultilineText Attribute

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_");
    }
}

Text

AutoRef Attribute

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;

    //
}

Text

Press "Create" and start edit TextConfg-record

Text

New line insert in the end of group of same lines

Text

__GUID field

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

In the plans

See projects with opened issues

FuryDB Components

Use FuryDB Components package for integrate database with unity