> 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).

# 长期记忆 | Unity

长期记忆（LTM）可实现与 NPC 的对话历史持久存储，使玩家能够无缝地从之前中断的地方继续互动，即使跨越多个会话也可以。这一功能显著提升了 NPC 的真实感，与我们在游戏中创建更具沉浸感和更接近真实人物角色的目标一致。

{% hint style="info" %}
前提条件：拥有一个安装了 Convai SDK 3.1.0 或更高版本的项目。如果你还没有，请查看此文档

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

### 使 LTM 正常工作的步骤

1. 选择你的 Convai 角色

   <figure><img src="/files/04b3824d06830725464c488e4646f0515651bd82" alt=""><figcaption></figcaption></figure>
2. 将长期记忆组件添加到你的角色上

   <figure><img src="/files/6bde019de4aa77150990c6a739960ed25ff262d6" alt=""><figcaption></figcaption></figure>
3. 确保该角色已启用长期记忆

   <figure><img src="/files/ec3b41e4390b487e508492cd52666bc7849d9fbb" alt=""><figcaption></figcaption></figure>

现在，你的角色应该已经可以正常使用长期记忆了。

### LTM 系统的组件

#### Convai 长期记忆组件

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

<figure><img src="/files/67144e22bdbd971cdbc79b6e4c83cca1e501a49e" alt=""><figcaption></figcaption></figure>

切换长期记忆

1）点击组件中提供的按钮

<figure><img src="/files/6c7c3f0662a7c0a76c78b896c08fc1c70a1e1438" alt=""><figcaption></figcaption></figure>

2）它需要一些时间来更新，之后你应当能在检查器中看到 LTM 的新状态。

<figure><img src="/files/55bc6c2afa58070c709f4ba9de902bafb601eb18" alt=""><figcaption></figcaption></figure>

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

### 故障排除

{% hint style="danger" %}
Grpc.Core.RpcException: Status(StatusCode=InvalidArgument, Detail="找不到 ID 为: 99fbef96-5ecb-11ef-93ce-42010a7be011 的 speaker。")
{% endhint %}

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

### Speaker ID 的管理

开发者高效管理使用其 API 密钥生成的 Speaker ID(s) 至关重要，因为可创建的 ID 数量是有限的，并且取决于订阅等级。妥善管理可确保资源的最佳利用，并防止应用功能可能出现的中断。

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

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

你可以通过访问 **Convai 窗口** 来查看与特定 API 密钥关联的所有 Speaker ID(s)。此功能提供了完整的 ID 列表，便于更轻松地管理和监控。

{% hint style="danger" %}
请确保 API 密钥输入正确；否则该功能将无法按预期运行。准确输入 API 密钥对于通过 Unity 中的 Convai 窗口访问和管理 Speaker ID(s) 至关重要。
{% endhint %}

<figure><img src="/files/303906f28d5c6a04d4f89da698f4c6b3dbca597c" alt=""><figcaption></figcaption></figure>

前往长期记忆部分

<figure><img src="/files/c276c260d3ff36e2a1f42464036106432502b098" alt=""><figcaption></figcaption></figure>

如果出现“未找到 Speaker ID(s)”消息，则无需继续本指南。不过，如果显示了 Speaker ID 列表，建议删除任何不再使用或不再需要的 ID(s)，以优化你可用的资源。

<figure><img src="/files/73e7a28a1a6b3e9d381cb048156d4540355dcca6" 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/unity-plugin/utilities/long-term-memory.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
