[Programming tools] Unity table export tool TableExporter

0. Preface

The Excel export tool used in unity that I was working on before continued to be improved, adding the use of enumeration class attributes, and improving the perfection of the demo, adding data and code loading methods for reference. Relevant codes and demos have been packaged as Unity packages, and the links are as follows:

链接:https://pan.baidu.com/s/1llkwYZ7zEYN-TrZ4NRd8Fw?pwd=wsad 
提取码:wsad

1. Function

TableExporter provides a function of exporting Excel tables as specific codes and data

  • Support multi-type export: bool, int, float, double, string, enumeration class, Vector2, including arrays corresponding to these basic types
  • The export type can be extended: it is very convenient to add attributes other than the basic attributes to the table, such as Vector3
  • Support multi-language export: After marking as multi-language, it will be additionally exported as a configuration file without additional processing
  • Export templates are easy to change: Export codes and multi-languages ​​are replaced according to template text. Templates can be changed in template.Asset,
  • Support multi-file export: multiple files can be exported as one data and code, which is convenient for multi-person version collaboration

2. Introduction

Briefly talk about the usage, assuming the form is as follows, the form ForTest:

different outputKey
Id Property1 Property2 Property3
int int string bool
serial number int test string test bool test
1 2 str1 FALSE
2 3 str2 FALSE

When we need to use data, we need to export it first. Because the code has also been exported, we only need to handle data loading and retrieval.

  • (1) Load the localization dictionary, if you use multilingual (outputKey) then DataTableManager.Inst.LoadLocalizationDic(name)
  • (2) Load table data DataTableManager.Inst.LoadDataTable<type>()
  • (3) Obtain the data corresponding to the table Id table.GetData(1);

The specific code is as follows

// 加载数据
DataTableManager.Inst.LoadLocalizationDic("Default");
DataTableManager.Inst.LoadDataTable<DRForTest>();

// 获取数据
DataTable<DRForTest> table = DataTableManager.Inst.GetDataTable<DRForTest>();
DRForTest test1 = table.GetData(1);
Debug.Log("test1.Property1:" + test1.Property1);
Debug.Log("test1.Property2:" + test1.Property2);

At this time, the data of Property1 and Property2 corresponding to Id=1 in the table can be obtained. Of course, it corresponds to the attribute, not all strings

test1.Property1:2
test1.Property2:str1

For more detailed usage, please refer to the following or the files in the demo

3. DEMO

The demo provides basic data loading and acquisition methods, and the relevant paths have been configured in the prefab

  • Data data export and export content
  • Data/ExcelData: the table to be exported
  • Data/ExportCode: export code path
  • Data/ExportData: export code path
  • Data/ExportLang: multilingual export path
  • Scene sample use scene
  • Sprite Game Script
  • Functions used when Sprite/DataTableExtension table data is exported
  • Code for Sprite/DataTableManager to properly load and use data
  • Sprite/Demo test code

The TestMono.cs of Scene in the demo demonstrates the data loading and acquisition method, which can be used as a reference for the
relevant data and export path of the demo. If you need to change it, you need to make changes in the corresponding Config.Asset of TableExporter before it can be used normally.

4. Editor menu

After importing, there will be an additional menu MyTool/Table Export, and its specific functions are as follows

  • Start the export tool: /Export

  • Create a default configuration file: /Create Asset/Create Config Asset

  • Create a default template file: /Create Asset/Create Template Asset

  • View the default configuration file: /Selection Asset/Selection Config Asset

  • View the default template file: /Selection Asset/Selection Template Asset

  • Delete the export code file: /Clear Output/Clear Code

  • Delete export data files: /Clear Output/Clear Data

The location where the configuration file is written is specified in ExportorEditor. If the location of the configuration file needs to be changed, it must be changed in ExportorEditor.

5. Configuration

The configuration content is stored in the two files Config Asset and Template Asset through ScriptableObject. Now let’s talk about the basic configuration content, the content represented by other items, and the corresponding scripts that can be changed by clicking on them, all of which have relatively complete comments.

The main configuration content of Config Asset is

  • Table file loading path LoadPath
  • Export data save path SaveDataPath
  • Export code save path SaveCodePath
  • Export multilingual save file SaveOutputKeyFile
  • Array Property Split String PropertyArrSplitChar

The main configuration content of Template Asset is

  • Code template CodeTemplate
  • Export code file name CodeFileName
  • Export data file name DataFileName

6. Agreed form format

Support multiple files to configure the same class, only need to be a sheet with the same name, but the attribute items need to be consistent (the columns can be inconsistent)

The configuration format of the table is as follows, for an example, please see the Execl file in Demo

table correspondence table content
1. Setting items different outputKey
2. Attribute name Id NextId MapType Name
3. Attribute type int int int string
4. Attribute annotation serial number next serial number map type name
5. Data row 1 1 2 1001 shooting guide
n. data line 2 3 1002 block guide
  • Setting items
    can be empty or other, there is no limit
    If it is different, all attribute values ​​​​of this attribute must not be repeated, otherwise an error will be reported
    If it is outputKey, this attribute will be exported as a key and exported to multilingual xml
  • Attribute type
    can handle bool, int, float, double, string, enumeration class, Vector2, and corresponding arrays such as bool[]
    When the attribute value is exported, it will check according to the attribute type, and if it is wrong, it will report an error
    attribute When the value is left blank, bool defaults to false, int\float\double defaults to false, string defaults to "", and array defaults to a 0-length array.

7. Export attribute expansion

If you need new properties, you can refer to the extension of Vector2, see Vector2Property.cs

(1). Create a property class and implement it, Vector2Property

First of all, we need a property class to represent this property, which needs to inherit the Property class, and the name needs to be the property name + Property. In the attribute class we need to define:

  • The format checking method of the attribute, if the format is not satisfied, an error will be reported
  • Replacement method when the table is empty (null character "")
  • Attribute resolution method when exporting code
  • Attribute array parsing method when exporting code

For example, when exporting Vector2, it will obtain the Exportor.Vector2Property class through reflection to create a new class, so there is no need for additional configuration.

At this point, the content export of the data has been completed, but in order to be used when importing, we also need to add the "property analysis method" and "property array analysis method". These two methods need to be used in the main program, so we need to pay attention is outside the Editor folder

// 属性类
// Vector2Property.cs
internal class Vector2Property : Property
{
    protected override bool OnFormatCheck(string content)
    {
        string[] temp = content.Split(",");
        bool flag = false;
        float tempFloat;
        if (temp.Length == 2 &&
            float.TryParse(temp[0], out tempFloat) &&
            float.TryParse(temp[1], out tempFloat))
        {
            flag = true;
        }
        return flag;
    }

    public override string OnEmptyReplace()
    {
        return "0,0";
    }

    public override string GetParseFunc()
    {
        return "DataTableExtension.ParseVector2";
    }

    public override string GetArrParseFunc()
    {
        return "DataTableExtension.ParseVector2Arr";
    }
}

(2). Implement attribute analysis method

Responsible for converting strings to this property

// Parse 函数
// DataTableExtension.ParseVector2
public static Vector2 ParseVector2(string text)
{
    string[] temp = text.Split(",");
    float x, y;
    if (temp.Length == 2 &&
        float.TryParse(temp[0], out x) &&
        float.TryParse(temp[1], out y))
    {
        return new Vector2(x, y);
    }
    else
    {
        return Vector2.zero;
    }
}

(3). Realize the attribute array parsing method

Responsible for converting strings to this attribute array

// 数组 Parse 函数
// DataTableExtension.ParseVector2Arr
public static Vector2[] ParseVector2Arr(string text)
{
    string[] texts = ParseStringArr(text);
    int length = texts.Length;
    Vector2[] arr = new Vector2[length];
    for (int i = 0; i < length; i++)
    {
        arr[i] = ParseVector2(texts[i]);
    }
    return arr;
}

8. It's over

The basic introduction here is over, I hope it can be useful.

Guess you like

Origin blog.csdn.net/Blue_carrot_/article/details/130954127
Recommended