Skip to content

Commit

Permalink
修复文档错误
Browse files Browse the repository at this point in the history
  • Loading branch information
pirunxi committed Jul 11, 2024
1 parent 37cf709 commit 16dcbaa
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 57 deletions.
11 changes: 7 additions & 4 deletions docs/manual/loadconfigatruntime.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# 加载配置

加载数据依赖一些Luban Runtime代码。对于Unity+C#,已经提供了`com.code-philosophy.luban`包。在Package Manager中安装com.code-philosophy.luban包,地址 `https://github.com/focus-creative-games/luban_unity.git`(或者从`https://github.com/focus-creative-games/luban_unity`下载)。对于其他语言请在 [示例项目](https://github.com/focus-creative-games/luban_examples/tree/main/Projects)中找到与你项目类型相符的项目,
从该项目中复制Luban相关的Runtime代码。

## 安装Luban.Runtime

加载数据依赖一些Luban Runtime代码。对于Unity+C#,已经提供了`com.code-philosophy.luban`包。在Package Manager中安装com.code-philosophy.luban包,地址 `https://github.com/focus-creative-games/luban_unity.git`(或者从`https://github.com/focus-creative-games/luban_unity`下载)。对于其他语言请在 [示例项目](https://github.com/focus-creative-games/luban_examples/tree/main/Projects)中找到与你项目类型相符的项目,从该项目中复制Luban相关的Runtime代码。

## unity + c# + json

对应示例项目为[Csharp_Unity_json](https://github.com/focus-creative-games/luban_examples/tree/main/Projects/Csharp_Unity_json)
`Assets/LubanLib`目录复制到你的项目,在'Player Building'中开启'unsafe code'支持。接着只需一行代码即可加载所有配置。
先完成`安装Luban.Runtime`操作,然后使用如下代码加载配置。

```csharp

Expand All @@ -32,6 +33,8 @@

## unity项目中使用c#代码并自动判断加载bin或json配置

先完成`安装Luban.Runtime`操作,然后使用如下代码加载配置。

开发期希望使用json导出格式,但为了节约导出文件大小以及提高加载性能,希望使用bin导出格式。通过反射创建cfg.Tables的方式,可以做到不改代码,自动适应这两种方式。

```csharp
Expand Down
Original file line number Diff line number Diff line change
@@ -1,68 +1,71 @@
# load configuration
# Load configuration

Loading data relies on some Luban Runtime code. For Unity+C#, the `com.code-philosophy.luban` package has been provided. Install the com.code-philosophy.luban package in Package Manager at the address `https://github.com/focus-creative-games/luban_unity.git` (or from `https://github.com/focus-creative-games/luban_unity`download). For other languages, please find the project that matches your project type in [Example Projects](https://github.com/focus-creative-games/luban_examples/tree/main/Projects).
Copy the Luban related runtime code from this project.
## Install Luban.Runtime

##unity + c# + json
Loading data depends on some Luban Runtime code. For Unity+C#, the `com.code-philosophy.luban` package is already provided. Install the com.code-philosophy.luban package in the Package Manager, address `https://github.com/focus-creative-games/luban_unity.git` (or download it from `https://github.com/focus-creative-games/luban_unity`). For other languages, please find the project that matches your project type in [Example Project](https://github.com/focus-creative-games/luban_examples/tree/main/Projects) and copy the Luban-related Runtime code from the project.

The corresponding example project is [Csharp_Unity_json](https://github.com/focus-creative-games/luban_examples/tree/main/Projects/Csharp_Unity_json),
Copy the `Assets/LubanLib` directory to your project and enable 'unsafe code' support in the 'Player Building'. Then load all the configuration with just one line of code.
## unity + c# + json

First complete the `Install Luban.Runtime` operation, then use the following code to load the configuration.

```csharp
void Load()
{
// One line of code can load all configurations. cfg.Tables contains an instance field for all tables.
var tables = new cfg. Tables(Loader);
// access a singleton table
Console.WriteLine(tables.TbGlobal.Name);
// access normal key-value table
Console.WriteLine(tables.TbItem.Get(12).Name);
// support operator [] usage
Console.WriteLine(tables.TbMail[1001].Desc);
}

private static JSONNode LoadJson(string file)
{
return JSON.Parse(File.ReadAllText($"{your_json_dir}/{file}.json", System.Text.Encoding.UTF8));
}

void Load()
{
// One line of code can load all configurations. cfg.Tables contains an instance field for all tables.
var tables = new cfg.Tables(Loader);
// Access a singleton table
Console.WriteLine(tables.TbGlobal.Name);
// Access a normal key-value table
Console.WriteLine(tables.TbItem.Get(12).Name);
// Support operator [] usage
Console.WriteLine(tables.TbMail[1001].Desc);
}

private static JSONNode LoadJson(string file)
{
return JSON.Parse(File.ReadAllText($"{your_json_dir}/{file}.json", System.Text.Encoding.UTF8));
}

```

## Use c# code in unity project and automatically judge whether to load bin or json configuration

## Use c# code in unity project and automatically judge to load bin or json configuration
First complete the `Install Luban.Runtime` operation, and then use the following code to load the configuration.

During the development period, I hope to use the json export format, but in order to save the export file size and improve loading performance, I hope to use the bin export format. By creating cfg.Tables through reflection, you can automatically adapt to these two methods without changing the code.
During the development phase, we want to use the json export format, but in order to save the export file size and improve loading performance, we want to use the bin export format. By creating cfg.Tables through reflection, we can automatically adapt to these two methods without changing the code.

```csharp
void Start()
{
var tablesCtor = typeof(cfg. Tables). GetConstructors()[0];
var loaderReturnType = tablesCtor.GetParameters()[0].ParameterType.GetGenericArguments()[1];
// Determine whether to use json or ByteBuf Loader according to the return value type of Loader in the constructor of cfg.Tables
System.Delegate loader = loaderReturnType == typeof(ByteBuf) ?
new System.Func<string, ByteBuf>(LoadByteBuf)
: (System.Delegate)new System.Func<string, JSONNode>(LoadJson);
var tables = (cfg.Tables)tablesCtor.Invoke(new object[] {loader});

// access a singleton table
Console.WriteLine(tables.TbGlobal.Name);
// access normal key-value table
Console.WriteLine(tables.TbItem.Get(12).Name);
// support operator [] usage
Console.WriteLine(tables.TbMail[1001].Desc);
}

private static JSONNode LoadJson(string file)
{
return JSON.Parse(File.ReadAllText($"{your_json_dir}/{file}.json", System.Text.Encoding.UTF8));
}

private static ByteBuf LoadByteBuf(string file)
{
return new ByteBuf(File. ReadAllBytes($"{your_json_dir}/{file}.bytes"));
}
void Start()
{
var tablesCtor = typeof(cfg.Tables).GetConstructors()[0];
var loaderReturnType = tablesCtor.GetParameters()[0].ParameterType.GetGenericArguments()[1];
// Decide whether to use json or ByteBuf Loader based on the return value type of the Loader of the constructor of cfg.Tables
System.Delegate loader = loaderReturnType == typeof(ByteBuf) ?
new System.Func<string, ByteBuf>(LoadByteBuf)
: (System.Delegate)new System.Func<string, JSONNode>(LoadJson);
var tables = (cfg.Tables)tablesCtor.Invoke(new object[] {loader});

// Access a singleton table
Console.WriteLine(tables.TbGlobal.Name);
// Access a normal key-value table
Console.WriteLine(tables.TbItem.Get(12).Name);
// Support operator [] usage
Console.WriteLine(tables.TbMail[1001].Desc);
}

private static JSONNode LoadJson(string file)
{
return JSON.Parse(File.ReadAllText($"{your_json_dir}/{file}.json", System.Text.Encoding.UTF8));
}

private static ByteBuf LoadByteBuf(string file)
{
return new ByteBuf(File.ReadAllBytes($"{your_json_dir}/{file}.bytes"));
}
```

## Other item types
## Other project types

Please find an example project that matches your project type in [Projects](https://github.com/focus-creative-games/luban_examples/tree/main/Projects), and just refer to it to load the code.
Please find the sample project that matches your project type in [Projects](https://github.com/focus-creative-games/luban_examples/tree/main/Projects) and refer to its loading
code.

0 comments on commit 16dcbaa

Please sign in to comment.