> 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/custom-targeting-rule.md).

# 编写自定义目标选择规则

当内置模式都不适合你的游戏如何选择对话对象时，用你自己的规则替换对话目标选择。

实现 `IConversationTargetProvider` 当你的游戏中三个内置目标选择模式都不适用时——比如 UI 列表中已选中的角色、当前任务所围绕的角色，或者站在触发体积中的角色。请使用此页面，而不是退回到 `手动` 并重建那些已经正常工作的目标选择部分。

### 前提条件

* 熟悉其中的模型 [对话定位的工作原理](/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/conversation-targeting/how-conversation-targeting-works.md)
* 一种三个内置模式无法表达的目标选择规则

### 实现该接口

`IConversationTargetProvider` 只有一个方法， `ResolveTarget`，会在每次目标选择评估时调用。

```csharp
using System.Collections.Generic;
using Convai.Runtime.Components;
using Convai.Runtime.Conversation;
using UnityEngine;

public sealed class QuestTargetProvider : MonoBehaviour, IConversationTargetProvider
{
    public ConvaiCharacter ResolveTarget(
        IReadOnlyList<ConvaiCharacter> candidates, ConvaiCharacter current) =>
        QuestState.CurrentSpeaker ?? current;

    private void Start() =>
        ConvaiManager.ActiveManager.SetConversationTargetProvider(this);
}
```

`候选角色` 会按稳定顺序列出房间中当前的每个角色，并且在 `ResolveTarget` 被调用。 `当前角色` 是当前正在掌控对话的角色，或者 `null` 当没有任何角色时。

返回玩家应当对话的角色。返回 `null` 以保持对话停留在原处。返回一个不在 `候选角色` 中的角色，也会使对话停留在原处。

### 注册该提供程序

```csharp
ConvaiManager manager = ConvaiManager.ActiveManager;
manager.SetConversationTargetProvider(new QuestTargetProvider());
```

传入 `null` 移动到 `SetConversationTargetProvider` 以移除自定义提供程序，并恢复由 `ConversationTargeting.Mode`.

{% hint style="info" %}
已注册的提供程序会完全取代内置模式，包括 `LookAt` 和 `Proximity`。该 `ConversationTargeting` 上的设置 `ConvaiManager` 在提供程序处于活动状态时不会生效。
{% endhint %}

### 你的规则之上仍然适用的内容

提供程序只负责选择要对话的角色。所有让这个选择更自然的机制仍然会叠加在提供程序返回值之上：对话仍然不会在句中途切换，仍然不会在某个目标第一次成为有效选择的那一帧就确定到新目标，也仍然不会让玩家无人可对话。有关这三条规则，请参见 [对话定位的工作原理](/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/conversation-targeting/how-conversation-targeting-works.md) ，了解这在实践中的含义。

如果 `ResolveTarget` 抛出异常，SDK 会捕获该异常、记录日志，并让对话保持原处，而不是让一个有问题的提供程序拖垮整个对话。

### 验证提供程序是否已激活

进入 Play 模式，并确认对话遵循你的自定义规则，而不是玩家的视角或位置。检查该 Live 部分的 `ConvaiManager` 检查器——它会报告被指向的角色以及上一次目标选择决策背后的判定，无论该结果是由内置模式还是自定义提供程序产生的。

### 下一步

{% content-ref url="/pages/f411a648fdc7e99258a645a2b4b897adb9e6a570" %}
[编写对话目标脚本](/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/conversation-targeting/script-the-conversation-target.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/custom-targeting-rule.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.
