> 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/embodiment/emotion/moods.md). Agents can also search and read this documentation through its [MCP server](https://docs.convai.com/api-docs/~gitbook/mcp).

# 心情

通过代码设置和清除 Convai 角色的常驻心情，并了解心情与短暂的情绪反应有何不同。

调用 `SetMood` 和 `ClearMood` 来 `ConvaiEmotionController` ，即可通过自己的代码更改角色的静息心情，而不受 Convai 回复当时驱动的任何短暂情绪影响。在情绪系统运行后，如果你希望角色的心情能反映对话之外发生的事情（例如任务结果、游戏事件或预设的剧情节点），请参阅本页。

***

### 心情与情绪

`ConvaiEmotionController` 每帧都会跟踪两个独立的值，本页介绍的是第二个：

* 短暂情绪（`CurrentResolvedEmotion` / `CurrentNormalizedIntensity`）是角色对最近一句话的反应。它由 Convai 的回复驱动，并会在反应消退后逐渐回归下方的心情。
* 心情（`CurrentMoodLabel` / `CurrentMoodScore`）是角色在两次反应之间保持的状态。它由角色的个性基线设定，也可以通过你自己的代码调用 `SetMood`来设定，并会持续到下一次变化。

角色可能正在积极表现出 `surprise` ，同时底层保持着 `joy` 心情——短暂状态不会覆盖心情，心情也不会表现为当前的活跃情绪。有关这两个通道所处完整流程，请参阅 [情绪系统的工作原理](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/embodiment/emotion/how-the-emotion-system-works.md) 。

***

### 设置心情

```csharp
using Convai.Modules.Emotion.Components;
using UnityEngine;

public class QuestMoodExample : MonoBehaviour
{
    [SerializeField] private GameObject character;
    private ConvaiEmotionController _emotionController;

    private void Awake() => _emotionController = character.GetComponent<ConvaiEmotionController>();

    public void OnQuestCompleted()
    {
        // 在 2 秒内逐渐过渡到更愉快的静息心境。
        _emotionController.SetMood("joy", intensity: 0.5f, transitionSeconds: 2f);
    }
}
```

`SetMood(string label, float intensity, float transitionSeconds = 1.5f)` 会解析 `label` ，并使用角色当前启用的 `EmotionTaxonomyAsset` 将静息心情逐渐过渡到对应状态，过渡时长为 `transitionSeconds`。如果标签为空或无法识别，或者 `intensity` 小于等于 `0` ，角色都会过渡到无心情状态，而不会抛出异常；如果标签无法识别，还会记录一条包含该标签名称的警告。 `TryResolveEmotionLabel` 可用于在调用 `SetMood`之前验证标签，这样调用方就能返回明确且可操作的错误信息，而不会静默地进入无心情状态。

***

### 清除心情

```csharp
// 在默认的 1.5 秒过渡时间内恢复到设定的基线。
_emotionController.ClearMood();
```

`ClearMood(float transitionSeconds = 1.5f)` 将心境过渡回角色的 **预设基线** ，但不一定是零。如果在 `ConvaiEmotionController` 的检视器中设置了角色自身的静息心境覆盖值，则预设基线就是该值；否则，它是 `ConvaiEmotionProfile`的人格基线。调用 `ClearMood` 可结束 `SetMood` 覆盖；使用不同的标签再次调用 `SetMood` 时，心境会直接交叉渐变到新目标，无需先清除。

{% hint style="info" %}
会话重置（断开连接或发生错误）时，始终会丢弃运行时的 `SetMood` 覆盖，并立即将心境恢复到预设基线，无论下文所述的情况如何。
{% endhint %}

***

### Convai 的心境命令如何到达角色

Convai 的响应也可以自行改变角色的心境：心境或反应提示等动作会通过 `MoodCommandHandlerAdapter`路由到该角色。Convai 会在添加 `ConvaiEmotionController` 时自动添加这一基础组件。它调用的 `SetMood` 与您在代码中调用的是同一个方法，因此脚本触发的心境变化和 Convai 驱动的心境变化在到达控制器后的行为完全相同。有关可在对话中触发心境变化的执行器，请参阅 [角色动作](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/character-actions.md) 。

***

### 与锁定和覆盖的交互

`LockEmotion`/`UnlockEmotion` 和 `SetEmotionOverride`/`ClearEmotionOverride` 都作用于瞬时情绪通道，而不是心境。在锁定或覆盖生效期间，心境仍会独立变化。

* `LockEmotion(label, intensity)` 将瞬时情绪保持在固定值并忽略 Convai 的响应，直到 `UnlockEmotion()` 将其释放。 `SetMood`/`ClearMood` 在情绪被锁定时的工作方式完全不变。
* `SetEmotionOverride(label, score)` 设置一个一次性的瞬时值；与锁定不同，它不需要 `UnlockEmotion` 。调用 `ClearEmotionOverride()` 可释放该覆盖，让 Convai 的响应重新驱动瞬时情绪通道。 `SetMood`/`ClearMood` 在上述任何情况下均不受影响。

由于心情和短暂情绪属于独立通道，因此使用 `LockEmotion` 锁定问候表情，同时在同一脚本中通过 `SetMood` 设置心情，两者绝不会冲突：前者固定当前面部表情，后者决定表情最终恢复到什么状态。

***

### 响应心情变化

```csharp
using Convai.Modules.Emotion.Components;
using UnityEngine;

public class MoodLogger : MonoBehaviour
{
    [SerializeField] private GameObject character;
    private ConvaiEmotionController _emotionController;

    private void Awake() => _emotionController = character.GetComponent<ConvaiEmotionController>();

    private void OnEnable() => _emotionController.MoodChanged += HandleMoodChanged;
    private void OnDisable() => _emotionController.MoodChanged -= HandleMoodChanged;

    private void HandleMoodChanged(string label, float score) =>
        Debug.Log($"Character's resting mood changed to {label} @ {score:F2}");
}
```

`MoodChanged` 每次标签转换时触发一次——包括首次应用作者设定的基准心情、调用 `SetMood`/`ClearMood`以及由 Convai 驱动的心情指令——并附带新标签及其 `CurrentMoodScore`。当交叉淡化仍在进行且目标标签不变时，仅分数发生变化不会触发此事件。

***

### 后续步骤

{% content-ref url="/pages/a169ddd3c944e6f42f2701922ad7b55a03ac789a" %}
[情绪系统的工作原理](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/embodiment/emotion/how-the-emotion-system-works.md)
{% endcontent-ref %}

{% content-ref url="/pages/39d161d99d04e6ca98725c740d134ccb5eac9e73" %}
[Emotion 脚本 API](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/embodiment/emotion/scripting-api.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/embodiment/emotion/moods.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.
