# 设置文本

您可以通过两种不同的 UI 系统向 Text Animator 设置文本：

* [#ui-toolkit](#ui-toolkit "mention")
* [#text-mesh-pro](#text-mesh-pro "mention")

{% hint style="info" %}
本页包含一些已经在 [安装与快速开始](/text-animator-unity/3.x-zh/kuai-su-kai-shi/an-zhuang-yu-kuai-su-kai-shi.md)中提供的信息，但也包含每个系统以及整体上的其他细节和建议。请务必阅读 [#best-practices](#best-practices "mention") 部分！&#x20;
{% endhint %}

***

## UI Toolkit

{% hint style="success" %}
就是这样！！您已经准备好使用 [如何添加效果](/text-animator-unity/3.x-zh/xiao-guo/ru-he-tian-jia-xiao-guo.md)
{% endhint %}

***

## Text Mesh Pro

{% hint style="success" %}
就是这样！！您已经准备好使用 [如何添加效果](/text-animator-unity/3.x-zh/xiao-guo/ru-he-tian-jia-xiao-guo.md)
{% endhint %}

{% hint style="warning" %}
如果您看到空文本（但已经在组件中设置了它们），请确保您至少点击过一次 TextMeshPro 组件，并导入了“Essentials”（其窗口会弹出并要求您这样做）。
{% endhint %}

#### 通过代码设置文本的最佳实践

要通过代码将文本设置到您的 TextMeshPro 对象上，请引用 Text Animator 的脚本而不是 TMPro，如下所示：

```csharp
using UnityEngine;
using TMPro; 
using Febucci.TextAnimatorForUnity.TextMeshPro; // <- 导入 Text Animator 的命名空间

public class ExampleScript : MonoBehaviour
{
    [SerializeField] TMP_Text textMeshPro;
    [SerializeField] TextAnimator_TMP textAnimator;

    void Start()
    {
        // 🚫 不要：通过 TMPro 设置文本
        textMeshPro.SetText("<wave>hello");

        // ✅ 要这样：直接通过 Text Animator 设置文本
        textAnimator.SetText("<wave>hello");
    }

}
```

{% hint style="info" %}
附言：引用 TMPro 仍然可以工作，但使用 TextAnimator 设置文本的集成更好，因为我们对文本有更多控制。
{% endhint %}

***

## 最佳实践

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

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

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

<details>

<summary>示例</summary>

如果有一个角色说“Hellooooo how are you doing?”，并且您想逐个字母显示，只需这样做： `typewriter.ShowText("Hellooooo how are you doing?");` 就这样！ [动态显示和隐藏字母](/text-animator-unity/3.x-zh/da-zi-ji/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/3.x-zh/ji-cheng/ji-cheng-de-cha-jian-yu-dui-hua-xi-tong.md))

</details>

<details>

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

性能！（即使你没有使用 Text Animator 也是如此。）

每次设置文本时，TextMeshPro 或 UI 工具包都需要计算其网格、位置等，而 Text Animator 随后还必须重新计算字符持续时间等。这意味着如果您每秒多次更改它（例如添加更多字母），您每次都在执行这些计算。

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

</details>


---

# 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/3.x-zh/xiao-guo/she-zhi-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.
