Skip to content

Latest commit

 

History

History
87 lines (65 loc) · 1.85 KB

DataDriven.md

File metadata and controls

87 lines (65 loc) · 1.85 KB

Data Driven

Use Unreal-like DataTable workflow in Unity.

Customize Data Row

Implement IDataTableRow and add [Serializable] attribute.

Example

using System;
using Chris.DataDriven;
using Chris.Resource;
using Chris.Serialization;
using UnityEngine;

// Must add this attribute
[Serializable]
public class MyDataRow : IDataTableRow
{
    public int id;
    public SoftAssetReference reference;
    public string name;
}

Runtime Loading

Recommend to implement DataTableManager<T> for managing DataTable.

Use await DataTableManager.InitializeAsync() to initialize managers at start of your game.

Or enable Initialize Managers in AkiFrameworkSettings to initialize managers before scene load automatically.

Example

public class MyDataTableManager : DataTableManager<MyDataTableManager>
{
    public MyDataTableManager(object _) : base(_)
    {
    }

    private const string TableKey = "MyDataTable";

    protected sealed override async UniTask Initialize(bool sync)
    {
        try
        {
            if (sync)
            {
                ResourceSystem.LoadAssetAsync<DataTable>(TableKey, (x) =>
                {
                    RegisterDataTable(TableKey, x);
                }).WaitForCompletion();
                return;
            }
            await ResourceSystem.LoadAssetAsync<DataTable>(TableKey, (x) =>
            {
                RegisterDataTable(TableKey, x);
            });
        }
        catch (InvalidResourceRequestException)
        {

        }
    }
    public DataTable GetDataTable()
    {
        return GetDataTable(TableKey);
    }
}

Editor

Support two kinds of editing mode.

Edit DataTable in Inspector.

DataTable Inspector

Edit DataTable in an EditorWindow (opened by double-left clicked).

DataTable EditorWindow