# 动态显示和隐藏字母

<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" %}
在 Inspector 中的 TypewriterComponent：

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

{% tab title="UI 工具包" %}
在 UI Builder 中的 AnimatedLabel：

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

{% hint style="warning" %}
请务必分配计时脚本化对象，否则打字机将立即显示整段文本！
{% 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 工具包" %}
通过 UI 工具包， `AnimatedLabel` 已经有一个可以交互的“`Typewriter`”值！\
你无需做任何其他操作，只需确保已分配打字机延迟。
{% endtab %}
{% endtabs %}

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

如果你没有遵循上面的步骤，只要你添加了 Typewriter 组件，或者通过 UI 工具包中的 AnimatedLabel 设置了“Timings”，TextAnimator 仍会尝试自动启动打字机。&#x20;

{% hint style="warning" %}
简易集成可能会晚一帧发生（因为它必须先发现某些内容已发生变化，而这通常是在上一帧完成的，然后才启动打字机）。如果这有问题，要么遵循步骤 [动态显示和隐藏字母](/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>

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

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

<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>

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

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

### 回调（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: 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.
