> For the complete documentation index, see [llms.txt](https://docs.febucci.com/text-animator-unity/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.febucci.com/text-animator-unity/3.x-zh/da-zi-ji/dong-tai-xian-shi-he-yin-cang-zi-mu.md).

# 动态显示和隐藏字母

<mark style="color:默认;background-color:yellow;">**你可以使用打字机效果动态显示和隐藏字母**</mark><mark style="color:默认;background-color:yellow;">,</mark> 为各种字符（标点、字母、\[…]）选择不同的暂停时间、触发事件等。

***

## 显示文本 <a href="#showing-text" id="showing-text"></a>

该 打字机包含通用设置和事件监听器，并支持不同的暂停/计时模式：

* **按字符**：逐字显示。
* **按单词**：逐词推进文本。

{% hint style="success" %}
这种新的架构（自 3.0 起）允许你在开发过程中因任何原因更改打字机的计时，同时保持事件引用和设置不变！<3
{% endhint %}

**你的打字机应如下所示：**

{% tabs %}
{% tab title="Text Mesh Pro" %}
在检查器中的 TypewriterComponent 里：

<figure><img src="/files/d7b6641c6a3307adee450d4e4e56f767d902515b" alt=""><figcaption></figcaption></figure>
{% endtab %}

{% tab title="UI Toolkit" %}
在 UI Builder 中的 AnimatedLabel 里：

<figure><img src="/files/c075dee4d8276931b0014bf35c547be6b0ea924e" alt=""><figcaption></figcaption></figure>
{% endtab %}
{% endtabs %}

{% hint style="warning" %}
请确保已分配 timings ScriptableObject，否则打字机将立即显示整段文本！
{% endhint %}

***

你可以通过两种主要方式启动打字机：

### A) 通过代码（推荐） <a href="#a-via-code-recommended" id="a-via-code-recommended"></a>

如果你想使用打字机， **建议你通过代码直接将文本设置到该组件。**

{% tabs %}
{% tab title="Text Mesh Pro" %}
如果你使用的是 TextMeshPro，请将引用 TMPro 或 Text Animator（[设置文本](/text-animator-unity/3.x-zh/xiao-guo/she-zhi-wen-ben.md)）的脚本替换为引用 `Febucci.TextAnimatorForUnity.TypewriterComponent`  。

* :x: 不要：“`tmproText.text = textValue;`”，或“`textAnimator.SetText(textValue);`"&#x20;
* :white\_check\_mark: 应当：  `typewriter.ShowText(textValue);`
  {% endtab %}

{% tab title="UI Toolkit" %}
通过 UI Toolkit， `AnimatedLabel` 已经有一个“`Typewriter`”值可供你交互！\
你无需做任何其他操作，只需确保已分配打字机延迟。
{% endtab %}
{% endtabs %}

### B) 自动识别 <a href="#b-via-the-easy-integration" id="b-via-the-easy-integration"></a>

如果你没有按上面的步骤操作，只要你添加了 Typewriter 组件，或在 UI Toolkit 的 AnimatedLabel 中设置了“Timings”，TextAnimator 仍会尝试自动启动打字机。&#x20;

{% hint style="warning" %}
Easy Integration 可能会延后一帧发生（因为它必须先发现某些内容已发生变化，而这通常是在上一帧完成的，然后才启动打字机）。如果这会造成问题，请执行步骤 [动态显示和隐藏字母](/text-animator-unity/3.x-zh/da-zi-ji/dong-tai-xian-shi-he-yin-cang-zi-mu.md#a-via-code-recommended)，或者查看 [故障排除](/text-animator-unity/3.x-zh/qi-ta/gu-zhang-pai-chu.md#when-i-set-the-text-i-see-the-previous-one-for-one-frame-before-showing-the-new-one)
{% endhint %}

***

## 控制字母

### 开始和停止输入 <a href="#start-and-stop-typing" id="start-and-stop-typing"></a>

在该组件的检查器中，你会找到一些选项，用于控制打字机的启动触发方式：

* `启动打字机模式`：告诉打字机何时开始显示字母。

<table><thead><tr><th width="300">值</th><th>说明</th></tr></thead><tbody><tr><td><strong>仅通过脚本</strong></td><td>打字机只能通过调用 <a href="https://www.api.febucci.com/tools/text-animator-unity/api/Febucci.UI.Core.TypewriterCore.html#Febucci_UI_Core_TypewriterCore_ShowText_System_String_">TextAnimatorPlayer.StartShowingText()</a></td></tr><tr><td><strong>OnEnable</strong></td><td>每次 gameObject 设为激活时，打字机都会启动</td></tr><tr><td><strong>OnShowText</strong></td><td>一旦设置了新文本，打字机就会立即启动（<a href="#showing-text">如“显示文本”部分所述</a>)</td></tr><tr><td><strong>从所有事件自动启动</strong></td><td>以上全部</td></tr></tbody></table>

* `启动时重置打字速度`：如果你希望每次显示新文本时打字机速度都重置为 1，则设为 true；否则将保留上一次使用的速度。

{% hint style="success" %}
你可以随时通过调用以下方法暂停打字机： `typewriter.StopShowingText()`，并且可以通过调用以下方法开始/继续它： `typewriter.StartShowingText()`.
{% endhint %}

### 跳过整段文本 <a href="#skip" id="skip"></a>

要跳过整个打字机效果，可以调用 `typewriter.SkipTypewriter()` 方法。

你还可以找到一些用于控制其行为的选项：

* `跳过时隐藏出现效果`：如果你希望在打字机跳过时不播放出现效果，则设为 true（这意味着文本会立即显示）。
* `跳过时触发事件`：如果你希望在打字机跳过后触发所有剩余事件，则设为 true（如果这些事件包含游戏逻辑，请谨慎使用，因为所有内容都会一次性执行）。更多关于事件的信息请见： [输入时触发事件](/text-animator-unity/3.x-zh/da-zi-ji/shu-ru-shi-chu-fa-shi-jian.md)

### 跳过文本的特定部分

{% hint style="warning" %}
该功能正在 3.0 中测试，并将在下一个版本很快恢复！感谢你的理解！
{% endhint %}

### 隐藏文本 <a href="#hiding-text" id="hiding-text"></a>

你可以通过脚本动态隐藏字母，调用 `typewriter.StartDisappearingText()`，并且也可以随时通过调用以下方法停止： `typewriter.StopDisappearingText()`.

***

{% hint style="info" %}
你可以创建自己的计时等待（阅读 [这里 ](/text-animator-unity/3.x-zh/bian-xie-zi-ding-yi-lei/bian-xie-zi-ding-yi-shu-ru-deng-dai-c.md)了解如何通过 C# 实现），或者使用内置的等待方式。
{% endhint %}

## 选项 <a href="#options" id="options"></a>

不同打字机可能共享相同的设置，也可能有各自特定的设置，因此请务必将鼠标悬停在检查器中的字段上，以显示每个字段的提示。

下面是最重要/最常见选项的简要概览：

### 回调（Unity 事件） <a href="#callbacks-unity-events" id="callbacks-unity-events"></a>

你可以使用会根据打字机活动触发的 Unity 事件（例如：刚结束显示文本时）。

<figure><img src="/files/52713625e04bcd1a58661106969ddaa00cc4a395" alt=""><figcaption></figcaption></figure>

<table><thead><tr><th width="288">事件</th><th>说明</th></tr></thead><tbody><tr><td><code>OnTextShowed</code></td><td>整段文本显示完毕后调用的事件（<em>如果你将“Use Typewriter”设为 true，它会等待直到所有字母都显示完毕</em>)</td></tr><tr><td><code>OnTextDisappeared</code></td><td>脚本一开始隐藏最后一个字母时立即调用</td></tr></tbody></table>

以下内容仅在“**use typewriter**” 设置为 **true**:

<table><thead><tr><th width="288">事件</th><th>说明</th></tr></thead><tbody><tr><td><code>OnTypewriterStart</code></td><td>在打字机开始显示第一个字母之前立即调用。<br>如果打字机关闭，则不起作用，因为这会与 “OnTextShowed” 事件重合 <em>（在这种情况下你可以改用那个事件）</em></td></tr><tr><td><code>OnCharacterVisible(Char)</code></td><td>每当一个字符变为可见时调用</td></tr><tr><td><code>OnMessage(EventMarker)</code></td><td>每当打字机在文本中遇到消息/事件时触发。更多关于事件的信息 <a href="/pages/e56d8ce31ef0c178b7797edb6055edd236275b75">这里</a></td></tr></tbody></table>

{% hint style="info" %}
打字机使用其关联的 Text Animator **时间缩放** 来推进时间（你可以在这里了解更多： [动画器设置](/text-animator-unity/3.x-zh/xiao-guo/ru-he-tian-jia-xiao-guo/dong-hua-qi-she-zhi.md#time-scale)），这意味着如果时间设置为“Unscaled”，那么即使游戏暂停，打字机也会继续推进。
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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/da-zi-ji/dong-tai-xian-shi-he-yin-cang-zi-mu.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.
