数据类和数据文件创建
- 创建excel表格,填充数据;
- 使用Luban工具生成数据类和数据文件;
Code Target:c cs-newtonsoft-json
Data Target:d json
注意 :Code Target: cs-simple-json在Unity2021.3.45f2c1会出现错误
升级luban工具可以解决这个问题 - 数据类放到项目中;数据文件放在指定文件夹中。
运行时使用
数据文件:放置在Assets/StreamingAssets/Luban中
数据:
csharp
[
{
"id": 1001,
"name": "道具1",
"desc": "描述1",
"count": 10
},
{
"id": 1002,
"name": "道具2",
"desc": "描述2",
"count": 100
}
]
加载数据,遍历表中数据
item类描述数据,TbItem管理表中所有的item,类似容器
csharp
using System;
using System.IO;
using Newtonsoft.Json.Linq;
using UnityEngine;
public class TestLuban : MonoBehaviour
{
void Awake()
{
//加载配置
//所有配置文件放置在Assets/StreamingAssets/Luban文件夹中,没有子文件夹
Func<string, JArray> loader = (fileName) =>
{
//拼接文件路径
string path = Path.Combine(
Application.streamingAssetsPath,
"Luban",
fileName + ".json");
string jsonText = File.ReadAllText(path);
return JArray.Parse(jsonText);
};
var tables = new cfg.Tables(loader);
//使用配置
//每个数据类有一个对应Tb类管理
//所有表的管理器 获取对应的表管理器 获取表中对应数据类的所有对象
var items = tables.Tbitem.DataList;
foreach (var item in items)
{
Debug.Log(item);
}
}
}