> 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);
}
```

`candidates` 按稳定顺序列出当前房间中的每个角色，并且当 `ResolveTarget` 被调用。 `第二个触发。响应` 是当前正在进行对话的角色，或者 `null` 当没有任何角色符合时。

返回玩家应该交谈的角色。返回 `null` 以保持对话原状。返回一个不在 `candidates` 中的角色也会让对话保持原状。

### 注册提供者

```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 模式，确认对话遵循你的自定义规则，而不是玩家的视角或位置。查看 `ConvaiManager` Inspector 的 Live 部分——它会显示当前被交谈的角色，以及上一次目标选择决策背后的判断结果，无论该结果是由内置模式还是自定义提供者产生的。

### 下一步

{% 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 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/custom-targeting-rule.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.
