-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
63 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 56 additions & 53 deletions
109
i18n/en/docusaurus-plugin-content-docs/current/manual/loadconfigatruntime.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |