# 动态拼接本地化文本

动态拼接可本地化的文本通常需要做一些准备才能时拼接文本生效.&#x20;

在LocalizationSetting GameObject 上会自动挂上一个组件: MyStringPack

<figure><img src="/files/SYMPUMJfSFA0lL8lw2Pq" alt=""><figcaption></figcaption></figure>

这个组件中会保持两个StringTable列表, 分别用于两种访问方式, 一种是根据字段名称查找访问的方式; 另一种是根据文本表的名称和字段名称或字段ID查找访问的方式.

例如Demo中的代码 DynamicSetTextDemo.cs 中下面一行代码, 字符串后面加了.L() ,意思是需要查询获得本地化文本.

```csharp
if (index % 2 == 0) {
    uiTextSetter.text = "Now Index = ".L() + ("<color=yellow>" + index + "</color>, ") + "Come on!".L();
}
```

Demo中的 MyStringPack 组件的 代码拼接文字集合列表中就关联了一张名叫'DemoCommonTexts'的文本表, 内容如下:

<figure><img src="/files/V2K1XIHsKoIlfnshO6Lz" alt=""><figcaption></figcaption></figure>

从表中可以发现: 任何代码中出现后面加了.L()的字符串都在这张文本表中的字段名称中出现了.

**换句话说, 后面加了.L()的字符串在拼接过程中一定会来 MyStringPack 脚本中去查询结果. 当MyStringPack 组件的 代码拼接文字集合列表中多张文本表中出现相同名称的字段时, 将使用后者的字段数据.**

**当需要根据文本表表名+字段名 或 文本表表名+字段ID 查询本地化文字时, 会使用到MyStringPack 组件的 填表拼接文字集合列表, 如下表表格中填写: Excel文件名\_子表名 :: 字段名 , Excel文件名\_子表名 组合成为一个文本表StringTable的名称. 这种方式就解决了把游戏数值表当作翻译表的尴尬.**

| 简介                                    | 详细介绍                                    |
| ------------------------------------- | --------------------------------------- |
| simpleDescription                     | detailDescription                       |
| string                                | string                                  |
| PlayerRoleConfig\_PlayerRole :: 杨小凡简介 | PlayerRoleConfig\_PlayerRole :: 杨小凡详细介绍 |
| PlayerRoleConfig\_PlayerRole :: 林欣艺简介 | PlayerRoleConfig\_PlayerRole :: 林欣艺详细介绍 |
| PlayerRoleConfig\_PlayerRole :: 邪不改简介 | PlayerRoleConfig\_PlayerRole :: 邪不改详细介绍 |
| PlayerRoleConfig\_PlayerRole :: 方天辰简介 | PlayerRoleConfig\_PlayerRole :: 方天辰详细介绍 |

在代码中也可以方便地使用此种方式访问本地化文本:

```csharp
    while (enabled) {
        uiTextSetter.text = "Player Name".L() + ": <color=yellow>" + 
                            $"PlayerAttributeForm::PlayerName{index}".LN()
                            + "</color>";
        if (index >= 2) {
            index = 0;
        }
        index++;
        yield return new WaitForSeconds(duration);
    }
```

在上面的代码实例中有一行: $"PlayerAttributeForm::PlayerName{index}".LN(), 其中 index 可以取值是0, 1, 2, 此**字符串后面跟了.LN() 代表需要去MyStringPack 组件的 填表拼接文字集合列表中的数据中去查找本地化数据.** PlayerAttributeForm 这张文本表中的数据如下:

<figure><img src="/files/0HAa9taXbAs1oaQpPSmh" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://fuliufuliu.gitbook.io/mylocalization-decument/v1.3/mylocalization-zhong-wen-wen-dang/yi-qie-zhun-bei-dou-wei-le-setter-zu-jian-shi-xian-ben-di-hua/dong-tai-pin-jie-ben-di-hua-wen-ben.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
