# 在打字时触发事件

事件是特殊的标签，允许你在打字机到达文本的特定位置时向任何监听脚本发送消息（字符串）。 *（因此，事件仅在打字机启用时有效）*

<figure><img src="/files/fb7a6ba53f12aaf33b69881fc863f593ba3ddb55" alt="textanimatorgif2febucci"><figcaption><p>场景“示例 3 - 事件”</p></figcaption></figure>

***

## 概述 <a href="#overview" id="overview"></a>

你可以通过使用富文本标签在文本中编写事件。

### 格式说明 <a href="#formatting" id="formatting"></a>

事件的消息以问号开头，如下所示： `<?eventMessage>`.

**分隔 示例：** 要调用名为“shakeCamera”的事件，写成： `<?shakeCamera>`

* 👍🏻 事件可以包含任何类型的标签，包括内置效果的标签。
* ⚠️ 事件区分大小写。写成 `<?camshake>` 与写成 `<?camShake>`不同。请注意！（或者在你的脚本中使用 `string.ToLower()` 方法来处理这种情况。）

### 参数 <a href="#parameters" id="parameters"></a>

事件可以有一个或多个参数（第一个以 `=` 符号开始，然后用逗号分隔其它参数 `,`），以便你向脚本发送多个数据。

* 一个参数： `<?eventID=parameter1>`，将产生消息“eventID”以及一个参数“parameter1”。
* 多个参数： `<?eventID=p1,p2>`，将产生消息“eventID”以及参数“p1”和“p2”。

***

## 监听事件 <a href="#listening-to-events" id="listening-to-events"></a>

想要监听事件/消息的脚本必须订阅 `onMessage` 回调到 `Typewriter` 类中。（[脚本 API](https://www.api.febucci.com/tools/text-animator-unity/api/Febucci.UI.Core.TypewriterCore.html#Febucci_UI_Core_TypewriterCore_onMessage)).

分隔 示例：

```csharp

//在你的脚本内

public Febucci.UI.Core.TypewriterCore typewriter;

//添加和移除对回调的监听
void OnEnable() => typewriter.onMessage.AddListener(OnTypewriterMessage);
void OnDisable() => typewriter.onMessage.RemoveListener(OnTypewriterMessage);

//根据事件执行操作
void OnTypewriterMessage(Febucci.UI.Core.Parsing.EventMarker eventMarker)
{
    switch (eventMarker.name)
    {
        case "something":
            // 做某事
            break;
    }
}
```

👍🏻 注意“message”字符串不包含 ‘<’、‘?’ 和 ‘>’ 字符，仅包含消息本身。

你可以在这里找到 [EventMarker API](https://www.api.febucci.com/tools/text-animator-unity/api/Febucci.UI.Core.Parsing.EventMarker.html)，例如用于获取事件的所有参数（如果有）。


---

# 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/zai-da-zi-shi-chu-fa-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.
