> For the complete documentation index, see [llms.txt](https://docs.convai.com/api-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/conversation-targeting/script-the-conversation-target.md).

# 编写对话目标脚本

通过脚本将对话转移到特定角色，并选择房间连接时打开在哪个角色上。

使用以下方法将对话切换到脚本中的特定角色 `ConvaiManager.TalkTo`，或者使用以下方法选择房间打开时对应的角色 `ConvaiManager.SetInitialCharacter`。当对话菜单、任务步骤或触发器体积需要决定玩家正在与谁交互，而不是根据玩家的视角或位置时，请使用此页。

### 前置条件

* 两个或更多 `ConvaiCharacter` 在场景中注册的组件
* 对当前活动的引用 `ConvaiManager`

### 将对话指向某个角色

`TalkTo` 是用于脚本化对话切换的方法。可随时调用——当房间不是多角色房间，或该角色不在其中时，它不会执行任何操作。

```csharp
ConvaiManager manager = ConvaiManager.ActiveManager;
manager.TalkTo(questGiver);
```

`TalkTo` 可在任何目标模式下工作，包括 `LookAt` 和 `Proximity` ——它不需要 `手动`。在自动模式下，一旦玩家看向别处或移动，下一次评估就可以再次移动对话。请选择 `手动` 当除了脚本调用之外不应有任何操作移动目标时。

{% hint style="info" %}
如果玩家在说到一半时 `TalkTo` 被调用，请求会一直保留到该段发言结束，因此在玩家说话时发起的调用不会把他们的一句话拆分给两个角色。
{% endhint %}

### 选择房间打开时对应的角色

`SetInitialCharacter` 在房间连接时选择首先发言的角色——这与 `TalkTo`不同。它是设置，而不是引导：它决定对话从哪里开始，而在已连接的房间上调用它会排队重新连接，而不是移动当前对话。保持未设置是正常的——场景顺序中的第一个角色会获得第一回合。

```csharp
manager.SetInitialCharacter(receptionist);
```

{% hint style="warning" %}
`SetInitialCharacter` 只决定谁先发言。在 `LookAt` 和 `Proximity`下，对话会从房间准备就绪的那一刻重新评估，并且一旦其他角色得分更高，就会离开该角色。要在实时房间中移动对话，请调用 `TalkTo`，而不是 `SetInitialCharacter`.
{% endhint %}

### 读取当前目标

```csharp
ConvaiCharacter target = manager.ConversationTarget;
ConversationTargetSwitchVerdict why = manager.ConversationTargetingStatus;
```

`ConversationTargetingStatus` 说明上一次目标选择决策为何会那样进行——这有助于区分被保留的目标和卡住的目标，因为从外部看它们一模一样。

### 订阅目标事件

```csharp
manager.ConversationTargetRequested += character => ShowHint(character);
manager.ConversationTargetChanged   += character => Confirm(character);
```

`ConversationTargetRequested` 在 SDK 关闭玩家输入之后、并在尝试发送目标更改之前立即触发；发送仍然可能失败。 `ConversationTargetChanged` 当来自 Convai 的权威响应在一次往返之后修正路由时触发。Convai 因这次移动而执行的任何操作，包括结束前一个角色的回合，都会发生在这两个事件之间。

### 一次只允许一个角色发言

调用 `TalkTo` 会立即结束前一个角色的回合，即使是在句子中间——请参见 [一次只允许一个角色发言](/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/conversation-targeting/how-conversation-targeting-works.md#one-character-speaks-at-a-time) 以了解完整规则以及如何避免截断脚本台词。

### 验证更改

调用 `TalkTo` 在 Play 模式下并确认对话已移动到指定角色——该角色的音频和口型同步应在下一次响应时开始，并且 `ConversationTarget` 应返回该角色。

### 下一步

{% content-ref url="/pages/f5230929cc0d546dc9658ece39909d8316c019fd" %}
[编写自定义目标选择规则](/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/conversation-targeting/custom-targeting-rule.md)
{% endcontent-ref %}

{% content-ref url="/pages/8ebdb90f75876aa6a33c36e080adab2e377b1140" %}
[对话目标选择参考](/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/conversation-targeting/targeting-reference.md)
{% endcontent-ref %}


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/conversation-targeting/script-the-conversation-target.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
