Table of contents
1. Download the Excel2Json tool
3. Generate Json file and C# code
Customize the relationship between Excel real value and displayed value
Show results
achieve effect
1. Configure the character's attributes in the table, which is more intuitive and convenient
2. Export Excel to a Json format file and import it into Unity for deserialization, making data reading very fast.
3. Automatically generate the corresponding C# class based on the table
4. The enumeration variable Path is displayed in the table as the corresponding Chinese characters instead of numbers, which is easier to read.
Steps
1. Download the Excel2Json tool
Tool download address:excel2json | Self-cultivation of game programmers (neil3d.github.io)
2.Configure the table
The first line is the field name
The second line is the type. Pay attention to the case. This will affect the type of the generated C# code.
The third line is comments and the rest is data.
The table name in the lower left corner is the generated C# class name.
Note: I designed Path as an enum type, which is stored as an integer value in the table but displayed as a string. The implementation method is at the bottom of the article.
3. Generate Json file and C# code
Open the excel2json.exe file, drag the table up, and you can preview the generated json file on the right
ExportType is the exported type. The default is an array. If modified to Dict Object, it can be exported as a dictionary with the first field as the key
Click the C# tab to view the generated C# code
SaveJson can export Json files
4. Deserialization in Unity
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
public class DataManager: MonoBehaviour
{
void Start()
{
//string json = File.ReadAllText("Assets/Resources/Data/test.json");
string json = Resources.Load<TextAsset>("Data/test").text;
Dictionary<string,CharacterInfo> infos=JsonConvert.DeserializeObject<Dictionary<string, CharacterInfo>>(json);
}
}
public class CharacterInfo
{
public string CharacterName; // 角色名称
public float HP; // 最大生命
public int Attack; // 攻击力
public string Description; // 角色描述
public Path Path; // 命途
}
public enum Path
{
Preservation,
TheHunt,
Destruction,
Nihility
}
1. Copy the generated code. The enumeration type needs to be defined by yourself.
2. Use Path.ReadAllText or Resources.Load<TextAsset> to read the Json file
3. Use JsonConvert.DeserializeObject to deserialize into an object, which requires using Newtonsoft.Json;
Customize the relationship between Excel real value and displayed value
Select the required cells, Conditional Formatting->Manage Rules->New Rule
Select to format only cells containing the following content, set to cell value, equal to, specific value, click Format
Select Custom, enter the displayed value in the type, and click OK
You can see in the management that it is applied to the cell range using this rule and will automatically expand when rows are added.
The format only affects the displayed value in excel, not the actual value
If this little trick helps you, please give it a like and support~