> 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/building-for-supported-platforms/building-for-webgl.md).

# 为 WebGL 构建

本指南将帮助你将 Convai 的 WebGL 功能集成到 Unity 项目中，使你能够将具有类人对话能力的 AI 角色生动呈现出来。

## 说明

Convai 的 Unity WebGL SDK 旨在补充我们 Unity Asset Store 版本的独立应用功能。借助这个专用 SDK，您可以构建并部署交互式 WebGL 应用，利用 Convai 的先进对话处理流程。请参阅以下说明，或观看我们 [*最新的教程视频*](https://youtu.be/2F2_tjtu-lY?feature=shared) （YouTube）。

{% embed url="<https://youtu.be/2F2_tjtu-lY?feature=shared>" %}

## 快速入门

### 下载 SDK 和示例

{% hint style="info" %}
[**在此处下载 Convai Unity SDK 的 WebGL 版本。**](https://github.com/Conv-AI/Convai-Unity-WebGL-SDK/releases)
{% endhint %}

{% hint style="info" %}
[**在此处下载完整的 WebGL 示例。**](https://github.com/Conv-AI/Unity-WebGL-Demo-Game)
{% endhint %}

{% hint style="info" %}
[**在 itch.io 上试用此示例。**](https://convai.itch.io/webgl-demo)
{% endhint %}

### 前提条件

{% hint style="warning" %}
继续操作前，请确保已在计算机上安装 Git。\
[**在此处下载 Git。**](https://git-scm.com/downloads)
{% endhint %}

### 导入和设置说明

请参阅以下位置的导入和设置说明： [导入与设置（夜间版）](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/unity-plugin/import-and-setup.md) 以及 [设置 Unity 插件](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/unity-plugin/setting-up-unity-plugin.md).

### WebGL 与 Unity Editor 不兼容

尝试在 Unity Editor 中运行场景时，您可能会遇到以下错误：

{% code overflow="wrap" %}

```
WebGL SDK 无法在 Unity Editor 中运行。请构建 WebGL 版本并运行。
```

{% endcode %}

出现此错误是因为无法直接在 Unity Editor 中测试 WebGL SDK。要测试 WebGL 应用，您必须创建开发版本。

## 切换到 WebGL

现在，Unity 已设置完成，接下来设置 WebGL。

前往 `File` → `Build Settings`，然后：

1. 点击 `WebGL`.
2. 勾选 `Development Build` 选项。
3. 选择 `Switch Platform.`

耐心点，还记得吗？切换需要一些时间。

平台切换为 WebGL 后，点击 `Player Settings`。精彩之处从这里开始：

<figure><img src="https://2402152281-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEtUJA212Zc1S9ACc8T4l%2Fuploads%2FOoSUy7Jn85xoGXsDtp0W%2Fimage.png?alt=media&amp;token=20690c31-be40-46b6-8217-221e20cd6f33" alt=""><figcaption></figcaption></figure>

### 配置 Player Settings

平台转换完成后，

1. 打开`Player Settings`.
2. 前往`WebGL settings`.
3. 在`Resolution and Presentation` 选项卡中，选择`Convai PWA Template`.

<figure><img src="https://2402152281-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEtUJA212Zc1S9ACc8T4l%2Fuploads%2FyCWnB1ZUzRsdMiSgl1Xs%2Fimage.png?alt=media&amp;token=c4d4190c-7915-4809-91d6-0a2fedb163ae" alt=""><figcaption></figcaption></figure>

## 导入角色并构建场景

重新加载完成后，检查设置是否已更改。然后关闭所有已打开的菜单，并按以下步骤操作：

<figure><img src="https://cdn.videotap.com/6608/screenshots/RM98hWOigrswo0Uksw8F-202.29.png" alt=""><figcaption></figcaption></figure>

1. 双击 `Convai folder` 并进入 scenes。
2. 打开 `Convey Demo WebGL` 场景。
3. 前往 Convai 网站获取 API 密钥，然后在 Unity 的 `Convai` → `Convai Setup`.
4. 现在，再次前往 Convai 网站，获取你最喜欢的角色 ID，并将其粘贴到 `Convai` → `Character Importer`.

请记住，Unity 编辑器无法直接测试 WebGL。不过别担心，我们有一个 `Build and Run` 选项：

<figure><img src="https://cdn.videotap.com/6608/screenshots/suKZ4w5jIDskFWyBqyO1-261.69.png" alt=""><figcaption></figcaption></figure>

1. 返回 `File` → `Build Settings`.
2. 点击 `Add Open Scenes` 然后点击 `Build and Run`.

选择一个文件夹来存放构建输出；如有需要，可以新建一个文件夹，并将其命名为“WebGL”。

<figure><img src="https://cdn.videotap.com/6608/screenshots/z5kMvyZPsvD8V5dvudCN-274.6.png" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
首次构建可能需要一些时间。\
之后再次构建和运行时，可以使用 Unity 快捷键 Ctrl + B。
{% endhint %}

首次构建耗时最长，不妨稍微活动一下——但别走太远。很快，你就能见到我们的演示角色 Amelia，或是你带入数字绿洲的其他角色。只需授予麦克风权限，就可以开始了！

## 与 Convai AI NPC 互动

现在，精彩时刻到了。按住“T”键，即可与精心挑选的角色聊天。也可以点击文本框输入问题。想要更细致地调整设置？按 F10 打开设置面板，在那里更改你的姓名和 UI 样式。

<figure><img src="https://cdn.videotap.com/6608/screenshots/hnaNRdZthU6yzX7gmIvw-318.59.png" alt=""><figcaption></figcaption></figure>

是不是很有成就感？当然！现在，你已经成功在浏览器中运行 WebGL 构建版本。有兴趣的开发者还可以更进一步，从 GitHub 下载项目文件，一探究竟。

{% hint style="info" %}
准备好发布正式构建版本时，请在发布前取消选中 Build Settings 中的 Development Build 选项。
{% endhint %}


---

# 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/building-for-supported-platforms/building-for-webgl.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.
