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

# 长期记忆 | Unity

了解如何启用角色在多个会话间保留对话历史

长期记忆（LTM）可持久保存与 NPC 的对话历史，让玩家即使跨多个游戏会话，也能无缝地从上次中断处继续互动。此功能显著提升了 NPC 的真实感，符合我们在游戏中打造更具沉浸感、更栩栩如生角色的目标。

{% hint style="info" %}
前提条件：项目中使用的 Convai SDK 版本为 3.1.0 或更高版本。如果尚未满足此条件，请查看此文档

[设置 Unity 插件](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/unity-plugin/setting-up-unity-plugin.md)
{% endhint %}

### 启用 LTM 的步骤

1. 选择你的 Convai 角色

   <figure><img src="https://2402152281-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEtUJA212Zc1S9ACc8T4l%2Fuploads%2FrHtaOfEpXu3ysWD8sTB0%2FAddComponent.png?alt=media&amp;token=47cea28b-e84b-431a-aa6b-e83a8474b6f3" alt=""><figcaption></figcaption></figure>
2. 将长期记忆组件添加到角色上

   <figure><img src="https://2402152281-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEtUJA212Zc1S9ACc8T4l%2Fuploads%2FxdkYI2iRn9bx1YI4RxeX%2FAdding%20LTM.png?alt=media&amp;token=bfefc477-c25a-4536-b828-7e465aafc38d" alt=""><figcaption></figcaption></figure>
3. 确保已为该角色启用长期记忆

   <figure><img src="https://2402152281-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEtUJA212Zc1S9ACc8T4l%2Fuploads%2FqD1nUtoQmcicJj6gPyNw%2FLTM%20Enabled.png?alt=media&amp;token=ec6de233-f4c1-42e6-ae0a-c4810aeff12b" alt=""><figcaption></figcaption></figure>

现在应该已为你的角色启用长期记忆。

### LTM 系统的组件

#### Convai 长期记忆组件

此组件可让你直接在 Unity 编辑器中启用或禁用 LTM

<figure><img src="https://2402152281-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEtUJA212Zc1S9ACc8T4l%2Fuploads%2F6xYd1ICNAiYweKBLLHWT%2FLTM%20Disabled.png?alt=media&amp;token=0bc592ff-e524-46ab-8731-79b4e4898f10" alt=""><figcaption></figcaption></figure>

切换长期记忆状态

1\) 点击组件中的按钮

<figure><img src="https://2402152281-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEtUJA212Zc1S9ACc8T4l%2Fuploads%2FfVZPUWzK2186MwADjjtM%2FLTM%20Disabled%20Button%20Highlighted.png?alt=media&amp;token=03d6bb41-819e-409c-96b0-469e920f060b" alt=""><figcaption></figcaption></figure>

2\) 更新需要一些时间。完成后，Inspector 中会显示 LTM 的最新状态。

<figure><img src="https://2402152281-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEtUJA212Zc1S9ACc8T4l%2Fuploads%2FniiudjNLSDVWXpwVsxlF%2FLTM%20Updating%20Status.png?alt=media&amp;token=1c7a8096-7f8d-4f4a-9d7d-3d190cabd29e" alt=""><figcaption></figcaption></figure>

{% hint style="warning" %}
为角色启用或禁用长期记忆（LTM）是一项全局操作，会影响所有与该角色互动的玩家，因此我们强烈建议不要在运行时切换 LTM 状态。为确保游戏体验一致，此功能应仅由开发者或设计师通过编辑器进行管理。
{% endhint %}

### 故障排查

{% hint style="danger" %}
Grpc.Core.RpcException: Status(StatusCode=InvalidArgument, Detail="Cannot find speaker with id: 99fbef96-5ecb-11ef-93ce-42010a7be011.")
{% endhint %}

如果遇到此错误，请确保 **SpeakerID** 是使用当前正在使用的同一个 API 密钥创建的。如果不确定使用的是哪个 API 密钥，可以通过以下方式重置 **SpeakerID** 和 **PlayerName** 访问位于 `ConvaiPlayerDataSO` 的文件 `Assets > Convai > Resources`然后即可重新开始该流程。

### 管理 Speaker ID

开发者必须妥善管理使用其 API 密钥生成的 Speaker ID，因为可创建的 ID 数量有限，并且取决于订阅等级。妥善管理可确保资源得到最佳利用，并避免应用功能可能出现的中断。

#### 每个 API 密钥的 Speaker ID 数量限制如下：

| 等级                 | 限制        |
| ------------------ | --------- |
| 个人                 | 1         |
| 游戏玩家 / 独立开发者 / 专业版 | 5         |
| 合作伙伴 / 企业版         | 100（可自定义） |

你可以在 Unity 项目中访问 **Convai 窗口** ，查看与特定 API 密钥关联的所有 Speaker ID。此功能会提供完整的 ID 列表，便于管理和监控。

{% hint style="danger" %}
请确保正确输入 API 密钥，否则此功能将无法按预期运行。准确输入 API 密钥是通过 Unity 中的 Convai 窗口访问和管理 Speaker ID 的关键。
{% endhint %}

<figure><img src="https://2402152281-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEtUJA212Zc1S9ACc8T4l%2Fuploads%2Fnc2iy1JbHWSckNM6CXTc%2FUntitled.png?alt=media&amp;token=9a8328be-1830-4d4b-b0ed-5a282c686a9a" alt=""><figcaption></figcaption></figure>

前往“长期记忆”部分

<figure><img src="https://2402152281-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEtUJA212Zc1S9ACc8T4l%2Fuploads%2FZaIoupylUGK2BDIavUen%2FUntitled.png?alt=media&amp;token=754058c0-0cfd-45bd-a9ce-2aaf9d60a8ec" alt=""><figcaption></figcaption></figure>

如果出现“未找到 Speaker ID”消息，则无需继续阅读本指南。但如果显示了 Speaker ID 列表，建议删除所有不再使用或不需要的 ID，以优化可用资源。

<figure><img src="https://2402152281-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEtUJA212Zc1S9ACc8T4l%2Fuploads%2FN4kfR5PD4wN9vUaQCUK1%2FUntitled.png?alt=media&amp;token=a450c610-11dc-45da-aa82-a4047cd975db" alt=""><figcaption></figcaption></figure>


---

# 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/unity-plugin/utilities/long-term-memory.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.
