> For the complete documentation index, see [llms.txt](https://docs.edgegap.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.edgegap.com/zh/docs/sample-projects/unity-netcodes/purrnet-di-zeng-shi.md).

# Purrnet 递增式

在 Edgegap 上托管一个 Unity Purrnet 游戏，使用 Purrnet Incremental 示例逐步了解构建和部署流程。

探索如何使用来自以下内容中的技术在 Edgegap 上托管 Unity 项目 [Purrnet 增量示例](https://purrnet.dev/docs/full-game-guides/incremental-game-sample).

{% hint style="success" %}
我们的 [该示例的分支版本](https://github.com/edgegap/netcode-game-sample-unity-purrnet-incremental) 在 Edgegap 专用服务器上运行游戏，请参阅 [Edgegap 分支新增内容](#what-the-edgegap-fork-adds).
{% endhint %}

{% embed url="<https://youtu.be/NWa4JfS0d3A>" %}

## ✔️ 准备工作

在开始之前，你需要：

* [Unity 6](https://unity.com/releases/unity-6) - 使用 Unity Hub 下载。
* [Purrnet 增量示例](https://purrnet.dev/docs/full-game-guides/incremental-game-sample) （上游项目），
  * 或 [适用于 Edgegap 的 Purrnet 增量示例](https://github.com/edgegap/netcode-game-sample-unity-purrnet-incremental)，我们为 Edgegap 准备的分支版本。
* [Purrnet Unity 包](https://purrnet.dev/docs/getting-started/installation-setup) （包含在示例项目中）。
* [Edgegap 的 Unity 专用服务器快速入门插件](https://docs.edgegap.com/zh/unity.md).
  * 可通过 git 或 Purrnet 包安装——两者都包含相同的插件。

在 Unity 中打开你的增量示例项目即可开始。

## ⚡ 部署并连接

{% hint style="success" %}
请参阅 [Unity](https://docs.edgegap.com/zh/unity.md) 以 **了解如何构建和自定义服务器**.
{% endhint %}

### 1. 在 Edgegap 上部署服务器

☑️ 开始之前，你需要 [在 Edgegap 注册一个免费账户](https://app.edgegap.com/auth/register)。无需信用卡。

☑️ 按照 [Unity 入门指南](https://docs.edgegap.com/zh/unity.md) 来构建并上传你的专用服务器。

☑️ [部署服务器](https://app.edgegap.com/deployment-management/deployments/create) 使用你新创建的 Incremental Sample 应用版本。

☑️ [打开你新部署的详情](https://app.edgegap.com/deployment-management/deployments/list) 并找到你唯一的一次性连接信息：

* **主机 URL** 格式为 `780aa4260e83.pr.edgegap.net` ,
* **外部端口** 格式为 `30854` （5 位数字）。

✅ 现在你可以继续下一步。

### 2. 从编辑器连接

☑️ 在 Unity 中打开你的新项目。

☑️ 确认你已打开主场景： `Scenes/GameScene.unity`.

☑️ 输入上一步中的 Network Manager 连接详情。

☑️ 按下 ▶️ Play 按钮启动你的游戏客户端。

☑️ 使用以下方式连接第二个虚拟玩家： [Multiplayer Play Mode](https://docs-multiplayer.unity3d.com/mppm/current/about/) 或 [ParrelSync](https://github.com/VeriorPies/ParrelSync).

🙌 恭喜你在 Edgegap 上完成第一次部署！

## 🧩 Edgegap 分支新增内容

我们的 [分支](https://github.com/edgegap/netcode-game-sample-unity-purrnet-incremental) 保留上游游戏，并将所有 Edgegap 相关内容添加到 `Assets/Edgegap/`。请查看其 `EDGEGAP.md` 文件以了解完整步骤。

* **运行时引导，无需修改场景** - `EdgegapBootstrap` 会在场景加载前运行，并在运行时添加 Edgegap 组件，因此 `GameScene` 不会被修改。在构建版本中，它会禁用 PurrNet 的自动启动。在编辑器中，播放模式会保持 PurrNet 的正常主机和克隆流程，除非 `在编辑器中应用` 被勾选。
* **一个设置资源** - `Assets/Edgegap/Resources/EdgegapSettings.asset` 包含模式、端口、匹配器 URL 和认证令牌。命令行选项例如 `-edgegapAddress` 和 `-edgegapPort` 会在构建中覆盖它。
* **直接连接（默认）** - 客户端构建会显示一个包含地址和端口的连接面板。可连接到 Docker 中的本地服务器，或连接到你从以下位置启动的部署： [控制面板](https://app.edgegap.com/deployment-management/deployments/list).
* **Edgegap 匹配器（可选，默认禁用）** - 勾选 `使用 Edgegap 匹配器` 在设置资源中，并粘贴你的匹配器的 `API URL` 和 `认证令牌`。然后玩家点击 `查找匹配` 并自动连接到分配的服务器。请参阅 [匹配系统](https://docs.edgegap.com/zh/learn/pi-pei-xi-tong.md).
* **服务器自动停止** - 在服务器构建中， `EdgegapServerLifecycle` 会在以下位置启动 PurrNet 服务器： `0.0.0.0:7777`。在 Edgegap 上，如果 120 秒内没有人加入，或者最后一名玩家离开后 30 秒，它会停止自己的部署。
* **UDP 端口 7777** - 你的应用版本需要一个 [端口](https://docs.edgegap.com/zh/learn/bian-pai/application-and-versions.md#port-mapping) 名称为 `gameport`，端口 `7777`，协议为 `UDP`，与其他示例相同的默认值。该分支包含一个 `Dockerfile` 以及一个 Tools / Edgegap / Build Linux Dedicated Server 菜单。

{% hint style="danger" %}
**不要在游戏客户端中集成 Edgegap API 端点，因为你的 API 令牌会为你的账户提供无限访问权限。请参阅** [集成](https://docs.edgegap.com/zh/docs/api/ji-cheng.md) **了解面向客户端的安全 API 端点和函数。**
{% endhint %}

{% hint style="info" %}
匹配认证令牌仅限于你的匹配器，因此可以安全地随游戏客户端发布。
{% endhint %}

{% hint style="info" %}
该分支更改了上游的一行代码： `ResourceSpawner.InitSeed` 现在是 `[ObserversRpc(runLocally: true, bufferLast: true)]`. **如果没有 `runLocally`，专用服务器就不会收到自己的种子，因此不会生成树木。** 主机模式不受影响。
{% endhint %}
