> 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/2.x-zh/tong-guo-c-jin-xingewai-zi-ding-yi/bian-xie-zi-ding-yi-dong-zuo-c.md).

# 编写自定义动作（C#）

除了使用 [内置操作](https://docs.febucci.com/text-animator-unity/typewriter/wait-actions-when-typing)，你可以通过脚本（C#）编写你自己的。

***

### 操作基类 <a href="#actions-base-class" id="actions-base-class"></a>

创建新类时，继承自 `Febucci.UI.Actions.ActionScriptableBase` ([脚本API](https://www.api.febucci.com/tools/text-animator-unity/api/Febucci.UI.Actions.ActionScriptableBase.html)).

```csharp
public class CustomAction : ActionScriptableBase
{
    //[...]
```

### 属性 <a href="#attributes" id="attributes"></a>

既然你正在创建一个 ScriptableObject，就像事件一样，确保也添加所需的属性以便实例化和序列化它们：

```csharp
[CreateAssetMenu(fileName = "YourCustomAction", menuName = "Text Animator/Custom/YourCustomAction")]
[System.Serializable]
public class CustomAction : ActionScriptableBase
{
    //[...]
```

这样你就可以从项目窗口中创建它们。

### DoAction 方法 <a href="#doaction-method" id="doaction-method"></a>

然后，你所需要做的就是重写“DoAction”方法并在其中编写你的代码。

请记住，操作会 **暂停** 打字机直到它们完成（例如，等待玩家输入或等待时间流逝）。

```csharp
public override IEnumerator DoAction(ActionMarker action, TypewriterCore typewriter, TypingInfo typingInfo)
{
    //[...]
}
```

* 该 `操作` 参数包含关于你的标签的有用信息，例如 ID 或是否有随附的任何参数（例如 `<playSound=02>`).
* 该 `typewriter` 引用当前正在执行操作的组件
* 该 `typingInfo` 包含诸如当前打字速度（你可以修改）以及在打字机中经过的时间等信息。

请参阅 <mark style="color:默认;background-color:orange;">脚本API</mark> 以便完整查看这些类及更多内容。

***

### ✅ 完成！ <a href="#done" id="done"></a>

完成！通过这个简单的步骤，你可以添加任何你想要的自定义操作。

附注：别忘了在项目视图中创建你的操作 ScriptableObject，并将其添加到操作数据库中。


---

# 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, and the optional `goal` query parameter:

```
GET https://docs.febucci.com/text-animator-unity/2.x-zh/tong-guo-c-jin-xingewai-zi-ding-yi/bian-xie-zi-ding-yi-dong-zuo-c.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
