> 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-availability/gate-your-ui.md).

# 根据可用性控制 UI

根据对话可用性控制自定义聊天输入框或麦克风按钮，使玩家输入要等到角色能听到时才生效。

将你自己的聊天输入框、麦克风按钮或发送操作绑定到 `ConvaiManager.ConversationAvailability` 这样它只有在被提及的角色确实能听到时才接受输入。构建自定义输入 UI 而不是使用随附的聊天输入框时，请使用此页面，因为后者已经会自行进行门控——请参见 [自定义聊天输入框提示](/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/conversation-availability/customise-chat-prompts.md) 如果你配置的正是这个。

### 前提条件

* 一个具有 `ConvaiManager` 并且至少有一个处于激活状态的 `ConvaiCharacter`。请参见 [场景组件参考](/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/getting-started/scene-components.md).
* 熟悉会话可用性状态。请参见 [会话可用性如何工作](/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/conversation-availability/how-availability-works.md).

### 读取当前判定结果

`ConvaiManager.ConversationAvailability` 返回当前 `ConvaiConversationAvailability` 用于 `ConvaiManager.AddressedCharacter`。调用 `CanAcceptPlayerInput()` 在结果上调用它，以获得 UI 所需的单个布尔值：

```csharp
using Convai.Runtime.Components;
using UnityEngine;
using UnityEngine.UI;

public class SendButtonGate : MonoBehaviour
{
    [SerializeField] private ConvaiManager _manager;
    [SerializeField] private Button _sendButton;

    private void Update()
    {
        _sendButton.interactable = _manager.ConversationAvailability.CanAcceptPlayerInput();
    }
}
```

### 改为响应变更事件，而不是轮询

`ConvaiManager.ConversationAvailabilityChanged` 每当判定发生变化时都会触发，包括玩家开始对另一个可用性不同的角色说话时。订阅它，以便立即更新 UI，而不是等到下一帧：

```csharp
private void OnEnable() => _manager.ConversationAvailabilityChanged += HandleAvailabilityChanged;
private void OnDisable() => _manager.ConversationAvailabilityChanged -= HandleAvailabilityChanged;

private void HandleAvailabilityChanged(ConvaiConversationAvailability availability)
{
    _sendButton.interactable = availability.CanAcceptPlayerInput();
}
```

### 针对特定角色进行门控，而不是针对当前被提及的角色

绑定到某个特定角色的 UI 元素——例如名牌、多角色场景中的按角色指示器——应当读取 `ConvaiCharacter.ConversationAvailability` 该角色本身，而不是 `ConvaiManager.ConversationAvailability`，后者始终返回当前被提及对象的结果：

```csharp
[SerializeField] private ConvaiCharacter _character;
[SerializeField] private Image _indicator;

private void Update()
{
    _indicator.color = _character.CanAcceptPlayerInput ? Color.green : Color.gray;
}
```

{% hint style="warning" %}
`ConvaiCharacter.ConversationAvailability` 与……不同 `ConvaiCharacter.IsCharacterReady`. `IsCharacterReady` 由 Convai 的角色就绪信号设置一次，之后即使角色后来失去了在房间中的席位也不会清除。请将其用于 `ConversationAvailability` 任何需要对玩家输入进行门控的场景。
{% endhint %}

### 验证门控

进入 Play 模式，连接后，在角色尚未被确认之前尝试输入或按发送。门控应阻止该操作，直到可用性报告为止 `就绪` 或 `Answering`。在对话中途断开该角色，并确认门控会再次关闭。

### 下一步

{% content-ref url="/pages/d6e6831a3a8119af9517d8ac6de60d6c38cb05f1" %}
[响应玩家开始说话](/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/conversation-availability/local-player-activity.md)
{% endcontent-ref %}

{% content-ref url="/pages/529a05fa8aa08e60134072820b4eee04f3269f6f" %}
[对话可用性参考](/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/conversation-availability/availability-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-availability/gate-your-ui.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.
