# 最佳实践

{% hint style="info" %}
每个项目都不同，但在本页你可以找到一些常见的最佳实践，帮助你更好地设置一切并避免大多数常见问题！
{% endhint %}

### 在代码中使用 Text Animator 的“SetText/ShowText”方法，而不是 TMPro

如果你通过代码设置文本，请直接使用 Text Animator 或其打字机，而不是 TextMeshPro。

```csharp
// 一些代码
TMP_Text tmpText;
Febucci.UI.Core.TAnimCore textAnimator;
Febucci.UI.Core.TypewriterCore typewriter;

// [...] 这样做
textAnimator.SetText(value); // 立即设置文本
//或者 
typewriter.ShowText(value); // 如果你有打字机则使用它来设置文本
```

参考 API： [textAnimator.SetText](https://www.api.febucci.com/tools/text-animator-unity/api/Febucci.UI.Core.TAnimCore.html#Febucci_UI_Core_TAnimCore_SetText_System_String_), [typewriter.ShowText](https://www.api.febucci.com/tools/text-animator-unity/api/Febucci.UI.Core.TypewriterCore.html#Febucci_UI_Core_TypewriterCore_ShowText_System_String_)

***

### 只设置整个文本/对话一次

请尽量只设置文本一次，使用打字机/可见性方法来控制它如何出现。

{% hint style="info" %}
如果你确实需要稍后追加文本，可以使用 "textAnimator.AppendText" 方法。
{% endhint %}

<details>

<summary>示例</summary>

如果有个角色说 “Helloooo how are you doing?”，并且你想逐字显示它，只需： `typewriter.ShowText("Hellooooo how are you doing?");` 就这样！ [动态显示和隐藏字母](/text-animator-unity/2.x-zh/typewriter/dong-tai-xian-shi-he-yin-cang-zi-mu.md)

***

如果你正在构建一个动态字符串，仍然可以在将其值设置给打字机/动画器之前完成构建。

```csharp
int apples = 5; // 稍后从游戏状态获取
string playerName = "Bob";

// 先构建整个对话行
string dialogue = $"Hello {playerName}, you've got {apples} apples";

// 然后只设置文本一次
typewriter.ShowText(dialogue);
```

（如果你使用对话系统，他们会为你处理这些——不用担心！ [集成](/text-animator-unity/2.x-zh/ji-cheng/ji-cheng-de-cha-jian-yu-dui-hua-xi-tong.md))

</details>

<details>

<summary>为什么我应该一次性设置整个文本，而不是逐字符设置？</summary>

性能！（即使你没有使用 Text Animator。）

每次设置文本时，TextMeshPro 都需要计算其网格、定位等，而 Text Animator 之后还要重新计算字符持续时间等。这意味着如果你每秒多次更改它（例如逐渐添加字母），这些计算会每次都进行。

要逐个显示字符，你只需将完整文本设置一次，然后启动打字机： [动态显示和隐藏字母](/text-animator-unity/2.x-zh/typewriter/dong-tai-xian-shi-he-yin-cang-zi-mu.md)&#x20;

</details>

***

### 使用 TextAnimator 的“ScheduleMeshRefresh”而不是 TMPro.ForceMeshUpdate()

如果你调用 "tmpText.ForceMeshUpdate"，TextAnimator 可能会丢失一些引用，导致字母显示不正确。如果文本发生了变化以至于你想调用该函数，请尝试改为调用 TextAnimator 的 "ScheduleMeshRefresh"！\[[脚本 API](https://www.api.febucci.com/tools/text-animator-unity/api/Febucci.UI.Core.TAnimCore.html#Febucci_UI_Core_TAnimCore_ScheduleMeshRefresh)] 它将在下一帧执行该方法，但会保持所有引用不变。

***

*如果你在文本检查器中更改了某些内容但没有得到更新，请告诉我们！*


---

# 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://docs.febucci.com/text-animator-unity/2.x-zh/qi-ta/zui-jia-shi-jian.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.
