# 输入时触发事件

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

<figure><img src="/files/b2ef63dcf481b939e077236bd3c2b8b115b8787c" 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
// 在你的脚本内
[SerializeField] TypewriterComponent typewriter;

// 添加和移除回调
void OnEnable() => typewriter.onMessage.AddListener(OnMessage);
void OnDisable() => typewriter.onMessage.RemoveListener(OnMessage);

// 根据接收到的标记执行操作
void OnMessage(EventMarker marker)
{
    switch (marker.name)
    {
        // 一旦打字机遇到 "<?something>" 标签
        
        case "something":
            // 做某事
            break;
    }
}
```

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


---

# 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/shu-ru-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.
