> 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 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.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/conversation-targeting/script-the-conversation-target.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.
