# 动态显示和隐藏字母

您可以使用 `打字机` 来动态显示和隐藏字母，为任意类型的字符（标点、字母、\[…]）设置不同的停顿，触发事件等。

{% hint style="info" %}
打字机使用其关联的文本动画器 **时间缩放** 来推进时间，您可以在这里阅读更多内容： [时间缩放](/text-animator-unity/2.x-zh/xiao-guo/yu-lan-bing-bian-ji-xiao-guo/shi-jian-suo-fang.md)
{% endhint %}

***

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

有不同类型的打字机，例如“`按字符`” 会一个字母接着一个字母显示，或“`按单词`” 会逐字显示文本。

在选择要使用的打字机之后（即在与 “TextAnimator” 相同的 GameObject 上添加该组件），您可以通过两种主要方式启动它：

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

将 “`tmproText.text = textValue;`” 替换为 `typewriter.ShowText(textValue);` 在您的代码中（[脚本 API](https://www.api.febucci.com/tools/text-animator-unity/api/Febucci.UI.Core.TypewriterCore.html#Febucci_UI_Core_TypewriterCore_ShowText_System_String_)).

{% hint style="danger" %}
如果您通过代码设置文本，请不要漏掉此步骤！如果遇到问题，请查看 [最佳实践](/text-animator-unity/2.x-zh/qi-ta/zui-jia-shi-jian.md)
{% endhint %}

### B) 通过 “简易集成” <a href="#b-via-the-easy-integration" id="b-via-the-easy-integration"></a>

启用 “`打字机自动启动`” 选项在 TextAnimator 组件中（附：此选项以前称为“简易集成”）。\
这将让插件自动检查来自您的 TextMeshPro 组件的文本更改并基于此启动打字机。

<img src="/files/bf0832d66f7e17fb97cf309a066ce73ba4b7cbae" alt="" width="600">

![](/files/e79390439da6dbf03c277bf719a66ecc8a5c8dcf)

***

## 控制字母

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

在组件的检视面板中，您会找到一些选项用于控制打字机何时开始触发：

<img src="/files/16ccd457707eeeb8843f362b66cb3283b63799ce" alt="" width="500">

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

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

* `在启动时重置打字速度`：如果为 true，则每次显示新文本时打字机的速度会重置为 1，否则会保留上次使用的速度。

{% hint style="success" %}
您可以随时通过调用来暂停打字机 [typewriter.StopShowingText()](https://www.api.febucci.com/tools/text-animator-unity/api/Febucci.UI.Core.TypewriterCore.html#Febucci_UI_Core_TypewriterCore_StopShowingText)，并且可以通过调用来启动/继续它 [typewriter.StartShowingText()](https://www.api.febucci.com/tools/text-animator-unity/api/Febucci.UI.Core.TypewriterCore.html#Febucci_UI_Core_TypewriterCore_ShowText_System_String_).
{% endhint %}

{% hint style="success" %}
您也可以在检视面板的播放模式中测试此功能（这些按钮在编辑模式下是灰显的）。
{% endhint %}

### 跳过全部文本 <a href="#skip" id="skip"></a>

要跳过整个打字过程，您可以调用 [typewriter.SkipTypewriter()](https://www.api.febucci.com/tools/text-animator-unity/api/Febucci.UI.Core.TypewriterCore.html#Febucci_UI_Core_TypewriterCore_SkipTypewriter) 方法。

您还可以找到一些选项来控制其行为：

* `在跳过时隐藏出现效果`：如果为 true，则当打字机跳过时不会播放出现效果（意味着文本会立即显示）。
* `在跳过时触发事件`：如果为 true，则一旦打字机跳过，将触发所有剩余事件（如果您用这些事件运行一些游戏逻辑要小心，因为所有内容会同时执行）。在这里可以阅读有关事件的更多内容： [在打字时触发事件](/text-animator-unity/2.x-zh/typewriter/zai-da-zi-shi-chu-fa-shi-jian.md)

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

您可以使用 "<`notype>`" 标签在文本的特定部分跳过打字机。例如 "`Hello <notype>I am shown instantly</notype> and now I'm typing normally again.`"

{% hint style="success" %}
您也可以在全局设置中更改此标签，以防您希望设置不同的标签。
{% endhint %}

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

您可以通过脚本动态隐藏字母，方法是调用 [typewriter.StartDisappearingText()](https://www.api.febucci.com/tools/text-animator-unity/api/Febucci.UI.Core.TypewriterCore.html#Febucci_UI_Core_TypewriterCore_StartDisappearingText)，并且您也可以随时通过调用来停止它 [typewriter.StopDisappearingText()](https://www.api.febucci.com/tools/text-animator-unity/api/Febucci.UI.Core.TypewriterCore.html#Febucci_UI_Core_TypewriterCore_StopDisappearingText).

您也可以通过检视面板中的按钮在运行时测试此功能。

***

{% hint style="info" %}
您可以创建自己的打字机（阅读 [这里 ](/text-animator-unity/2.x-zh/tong-guo-c-jin-xingewai-zi-ding-yi/bian-xie-zi-ding-yi-da-zi-ji-c.md)如何通过 C# 创建自定义打字机）或者使用内置的打字机。
{% endhint %}

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

打字机可能共享相同的设置，也可能有特定设置，因此请确保将鼠标光标悬停在检视面板中的各个字段上以显示每个字段的工具提示。

<img src="/files/d95397373c6256519917f8d523fdbaf860cc77f6" alt="" width="500">

下面是一些最重要/常见设置的快速概述：

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

您可以使用基于打字机活动触发的 Unity 事件（例如：当它刚刚完成显示文本时）。

<img src="/files/822ce43c227f8b83d64e3a959245e7875df7e721" alt="" width="400">

<table><thead><tr><th width="288">事件</th><th>说明</th></tr></thead><tbody><tr><td><code>OnTextShowed</code></td><td>在整个文本显示完毕后调用的事件（<em>如果您已将“使用打字机”设置为 true，它会等到所有字母都显示完毕</em>)</td></tr><tr><td><code>OnTextDisappeared</code></td><td>在脚本开始隐藏最后一个字母时被调用</td></tr></tbody></table>

下面的内容仅在“**使用打字机**” 设置为 **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/dbf1e2f932a7e24f98038c9cac6f6318433dca89">这里</a></td></tr></tbody></table>

{% hint style="info" %}
您可以将鼠标悬停在打字机选项上（例如延迟）以显示一些工具提示和附加信息！
{% 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/2.x-zh/typewriter/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.
