# 编写自定义打字机（C#）

通过使用“Text Animator for Unity”你可以创建你自己的 **自定义打字机**，在字母之间设置不同类型的延迟以及更多功能。\
（如果你想了解默认的打字机，请 [在此阅读](/text-animator-unity/2.x-zh/typewriter/dong-tai-xian-shi-he-yin-cang-zi-mu.md)).

***

## 添加自定义打字机 <a href="#adding-custom-typewriters" id="adding-custom-typewriters"></a>

为了为你的游戏创建自定义打字机，你需要创建一个继承自的自定义类 `Febucci.UI.Core.TypewriterCore` \[[脚本 API](https://www.api.febucci.com/tools/text-animator-unity/api/Febucci.UI.Core.TypewriterCore.html)].

### 基本方法 <a href="#fundamental-methods" id="fundamental-methods"></a>

你必须重写/实现以下方法：

#### float GetWaitAppearanceTimeOf(int charIndex) <a href="#float-getwaitappearancetimeofint-charindex" id="float-getwaitappearancetimeofint-charindex"></a>

它告诉打字机每个字符需要等待的时间。通过这种方式，你可以实现自己的特殊字符，这些字符需要独特的等待时间等。

你可以通过以下方式根据索引访问字符：

```csharp
char character = TextAnimator.Characters[charIndex].info.character
```

例如，为了在输入标点符号时具有不同的等待时间：

```csharp

[SerializeField] float normalTime = .1f;
[SerializeField] float punctuationTime = .3f;
protected override float GetWaitAppearanceTimeOf(int charIndex)
{
    char character = TextAnimator.Characters[charIndex].info.character;
    return char.IsPunctuation(character) ? punctuationTime : normalTime;
}
```

***

### 可选方法 <a href="#optional-methods" id="optional-methods"></a>

你还可以重写以下方法以获得额外功能。

#### float GetWaitDisappearanceTimeOf(int charIndex) <a href="#float-getwaitdisappearancetimeofint-charindex" id="float-getwaitdisappearancetimeofint-charindex"></a>

如果你希望不同的消失等待时间/速度，请重写此方法。否则，你的自定义 textAnimatorPlayer 将使用与其打字机相同的延迟（GetWaitAppearanceTimeOf）；

***

✅ 就这些！真的！

👍🏻 别忘了将你的打字机组件添加到与 TextAnimator 组件相同的 gameObject 上。

玩得开心，自己实现你喜欢的打字机 <3


---

# 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/tong-guo-c-jin-xingewai-zi-ding-yi/bian-xie-zi-ding-yi-da-zi-ji-c.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.
