如何通过 Edgegap 部署来自 Unity UGS Matchmaker 的专用服务器

本教程将介绍如何将 Unity Gaming Services Matchmaker 连接到 Edgegap,以便将匹配的玩家部署到根据需求在靠近玩家的全球区域中启动的专用游戏服务器上。

本教程基于一个已在 Edgegap 上运行专用服务器的项目。如果您还没有此类项目,我们的集成教程涵盖了各种引擎、网络代码和后端。我们还建议您参考我们的文档

让我们开始吧。

第 1 部分 - 设置

本教程假定两个前提。首先,您拥有一个已在 Edgegap 上作为专用服务器运行的 Unity 项目,并且该平台上已有应用和激活版本。其次,您已经使用或想要使用 Unity 的 Matchmaker。

在本教程中,我们使用的是 Edgegap 自己的 Unity NGO Boss Room 示例的分支。我们选择它是因为它已经包含了专用服务器项目所需的一切。它还包含一个小型匹配客户端,一个简单的游戏内 “Matchmake” 按钮,用于调用 UGS Matchmaker。由于该调用流程通常对每个项目的结构都是独特的,我们稍后将介绍如何重建它,包括使用我们分支的代码参考,以及您可以适应自己游戏的生命周期代码助手提示词。

第 2 部分 - Edgegap:游戏服务器托管与编排

您的项目首先需要的是 Unity Gaming Services 包以及此集成所依赖的三个服务:Authentication、Matchmaker 和 Cloud Code。如果您的项目中已经运行了这些服务,可以跳到下一部分。

在 Unity 编辑器中开始并安装您需要的两个包:

  1. 选择 “Window”,然后选择 “Package Manager”。

  2. 将包源设置为 “Unity Registry”。

  3. 搜索 “Cloud Code”,选择它,然后选择 “Install”。

  4. 搜索 “Multiplayer Services” 并选择 “Install”。该包捆绑了對局配對服务,并自动为您引入了 Authentication。

几秒钟后,两者都会出现在您的项目中。

接下来,在 Unity Cloud 控制面板中开启这些服务。首先,确保您的项目已链接到 Unity Cloud 项目:在 “Edit”、“Project Settings”、“Services” 下,您应该会看到一个已链接的项目及其 ID。如果尚未链接,请在此处进行链接。如果您在此步骤中遇到问题,可通过 Unity 的支持流程来解决。

然后,在浏览器中,前往 cloud.unity.com 上的项目并登录。启用这三个服务:Authentication、Matchmaker 和 Cloud Code。

记下您的环境,默认情况下为 “production”(生产环境)。从这里开始,您设置的所有内容(包括您的密钥、模块和队列)都必须存在于同一个环境中,因此请保持一致。

稍后,一旦 Matchmaker 队列所依赖的部分存在,我们就会回来处理它。

第 3 部分 - Edgegap 分配器

此集成的核心是一个小型的 Cloud Code 模块,它允许 Unity 的 Matchmaker 在 Edgegap 上部署服务器。它由 Unity 官方发布,因此您只需要对其进行配置和部署。

当一局比赛形成时,该模块执行两个任务。首先,它请求 Edgegap 为您的应用程序部署一个服务器。然后,它会对该部署进行轮询,直到服务器准备就绪,并将其地址和端口交还给 Matchmaker,然后再由 Matchmaker 传递给您的玩家。

该模块位于 Unity 的 Matchmaker 托管提供商仓库中,与您的游戏项目分开。打开终端并将其克隆到项目旁边。克隆命令在每个操作系统上都是相同的;只有文件夹路径不同:

macOS / Linux

mkdir -p ~/unity-ugs-hosting
cd ~/unity-ugs-hosting
git

mkdir -p ~/unity-ugs-hosting
cd ~/unity-ugs-hosting
git

mkdir -p ~/unity-ugs-hosting
cd ~/unity-ugs-hosting
git

Windows (PowerShell)

mkdir ~\unity-ugs-hosting
cd ~\unity-ugs-hosting
git clone https://github.com/Unity-Technologies/matchmaker-hosting-providers.git
mkdir ~\unity-ugs-hosting
cd ~\unity-ugs-hosting
git clone https://github.com/Unity-Technologies/matchmaker-hosting-providers.git
mkdir ~\unity-ugs-hosting
cd ~\unity-ugs-hosting
git clone https://github.com/Unity-Technologies/matchmaker-hosting-providers.git

打开克隆的文件夹并浏览到 Edgegap 分配器。您需要配置的唯一一个文件就是分配器本身;它在顶部附近有一些标记为 “TODO” 的常量,这会告知它要部署哪个 Edgegap 应用程序。

设置 application name,使其与 Edgegap 控制面板中显示的 Edgegap 应用程序完全一致,例如 com-unity-multiplayer-samples-coop。此处只要错一个字符,后续运行就会静默失败,因此请直接复制而不是重新键入。

version name 设置为 Edgegap 平台上应用程序版本的精确名称。

port name 应该已经是 gameport,这与我们版本上的端口相匹配。请注意,此示例中的端口是 UDP 的 7777 端口。对于其他网络代码,这可能会有所不同。因此,请确保查阅您的网络代码文档,以找到适合您使用场景的正确端口。

secret name 保留为 EDGEGAP_API_TOKEN;我们接下来将创建该密钥。保存文件。

Cloud Code 会从密钥中读取您的 Edgegap API 令牌,因此该模块中绝不会包含您的密钥。在 Edgegap 控制面板中,选择您的组织,然后选择 “Tokens”,并复制您的令牌。然后,在 Unity Cloud 控制面板中的 “Administration” 菜单下,转到您项目的 secrets,并添加一个密钥:

  • Key: EDGEGAP_API_TOKEN(全大写)

  • Value: 您的 Edgegap API 令牌,且仅填写令牌,不带有多余的单词或空格

  • Environment: 与其他所有内容相同的环境(例如 production)

现在从编辑器中部署该模块。回到 Unity 中,在项目的主文件夹中创建一个 “Cloud Code C# Module Reference”。选择它,并在 Inspector 中,将其指向分配器的解决方案文件,即您刚刚克隆的仓库中的 .sln 文件。

这是容易让人出错的步骤:如果引用指向了除真实解决方案文件以外的任何其他内容,部署看起来可能是成功的,但实际上推送的是一个空模板。因此,请确保它是实际的 .sln 文件。

打开 “Services” 下的 “Deployment” 窗口。您的模块将显示在其中。确保它已被勾选并设置为您的环境,然后选择 “Deploy Selected”。它会自动进行编译、打包并发布。这需要使用 .NET SDK 8 或更高版本;您可以使用以下命令确认您的版本:

dotnet --version
dotnet --version
dotnet --version

现在是重要部分:验证部署确实成功,而不是仅仅相信成功提示。回到 Unity Cloud 控制面板,打开您的 Cloud Code 模块并检查其端点。您应该会看到两个端点,分配端点(allocate endpoint)和轮询端点(poll endpoint),每个端点都会返回分配器自己的响应类型。如果看到了这两个端点,说明您的模块已上线。如果返回类型只显示为 “string”,则说明模块引用指向了错误的文件,因此在继续之前请返回并修复它。

第 4 部分 - 连接队列

模块上线后,您就可以指示 Matchmaker 使用它了。在此处,您现有的對局配對器将被指向 Edgegap,对于大多数项目来说,这是唯一真正需要改变的地方。

在 Matchmaker 区域,创建一个队列,例如 bossroom-queue。为了进行这种简单的测试,允许每个票据有两个玩家。

在队列内部,创建一个池,并将其托管类型设置为 “Hosting via Cloud Code”。这就是将分配发送到您的模块而不是其他任何地方的开关。将池指向您的模块:分配器,它的分配端点和轮询端点。分配超时是 Matchmaker 等待 Edgegap 启动服务器的时间;默认时间足够了,因为全新的部署可能需要一些时间。

最后,设置匹配规则。对于最简单可能的对局(一个由两名玩家组成的队伍):一个队伍,队伍数量为一,玩家数量最小和最大限制均为二。暂时关闭回填(backfill)且不添加其他规则;您稍后可以根据自己的游戏来定制所有这些规则。

保存后,您的队列就会激活。后端就搞定了,您的對局配對器现在就可以部署到 Edgegap 了。

第 5 部分 - 對局配對客户端

您的玩家需要一种请求对局的方法,以及一种能够获取分配的服务器并将其连接到该服务器的机制。在我们的分支中,该客户端已经存在,因此我们将看看它的作用以及如何在您自己的游戏中实现同等功能。

详细流程为:客户端让玩家登录,在您的队列上创建一个對局配對票据,并在轮询结果时进行等待。一旦 Matchmaker 分配了服务器,它就会读取该服务器的地址和端口,并将它们传递给游戏自己的连接调用。

该连接调用是特定于您网络代码(netcode)的唯一部分。在 Boss Room 中,它是 StartClientIp。在您的游戏中,它是您的项目已用于通过地址和端口将客户端连接到服务器的任何现有方法。至于其他所有内容,如创建票据、等待和读取分配,对于任何 Unity Gaming Services 项目都是相同的。

如果您的项目还没有此客户端,您可以给任何 AI 编程助手输入以下提示。它会首先审计您的项目,找出您的游戏如何连接到服务器,然后生成一个小型、自包含的對局配對客户端,该客户端可以创建票据、等待分配并调用您自己的连接方法,无论您使用什么网络代码。它还添加了一些我们在示例中保持简单的测试便利:它会给每个本地实例一个独特的玩家身份,以便您可以在一台机器上测试两个客户端,并且它会以更长的间隔进行轮询。一如既往,连接的代码行取决于您的游戏,因此请仔细检查它产生的结果。

Add Unity Gaming Services (UGS) Matchmaker support to my game as a temporary,
self-contained client for testing dedicated-server allocation. Do NOT modify my
existing netcode; only add a new component that drives matchmaking and then calls
my existing connect path.

First, audit (read-only) and tell me before writing code:
  1) The method my project calls to connect a client to a server by IP/address and
     port file, line, exact signature, and the TYPE of the port parameter.
  2) The transport in use and where connection data is ultimately set.
State these findings, then proceed.

Then create one MonoBehaviour that:
  - Initializes UGS (UnityServices.InitializeAsync) and signs in anonymously via
    Authentication.
  - For same-machine testing, sets a UNIQUE profile per running instance BEFORE
    sign-in (e.g. a value derived from the project path or a random id); if a player
    is already signed in, sign out first, then switch profile, then sign in so two
    local instances get distinct player IDs.
  - Creates a UGS ticket on a serialized queue-name field (default it to my queue),
    with one player entry using the signed-in player id.
  - Polls the ticket status no more often than every 3 seconds, and treats a failed
    poll as retryable (catch and continue the loop, do not abort) until an assignment
    arrives or a timeout elapses.
  - On assignment, reads the assigned IP and port and passes them into the connect
    method identified in the audit, converting the port to the exact type that method
    expects.
  - Exposes a simple temporary on-screen "Matchmake" button to trigger it.

Keep it minimal and commented. Tell me the audit findings first, then show the full
component. Do not change any other files

Add Unity Gaming Services (UGS) Matchmaker support to my game as a temporary,
self-contained client for testing dedicated-server allocation. Do NOT modify my
existing netcode; only add a new component that drives matchmaking and then calls
my existing connect path.

First, audit (read-only) and tell me before writing code:
  1) The method my project calls to connect a client to a server by IP/address and
     port file, line, exact signature, and the TYPE of the port parameter.
  2) The transport in use and where connection data is ultimately set.
State these findings, then proceed.

Then create one MonoBehaviour that:
  - Initializes UGS (UnityServices.InitializeAsync) and signs in anonymously via
    Authentication.
  - For same-machine testing, sets a UNIQUE profile per running instance BEFORE
    sign-in (e.g. a value derived from the project path or a random id); if a player
    is already signed in, sign out first, then switch profile, then sign in so two
    local instances get distinct player IDs.
  - Creates a UGS ticket on a serialized queue-name field (default it to my queue),
    with one player entry using the signed-in player id.
  - Polls the ticket status no more often than every 3 seconds, and treats a failed
    poll as retryable (catch and continue the loop, do not abort) until an assignment
    arrives or a timeout elapses.
  - On assignment, reads the assigned IP and port and passes them into the connect
    method identified in the audit, converting the port to the exact type that method
    expects.
  - Exposes a simple temporary on-screen "Matchmake" button to trigger it.

Keep it minimal and commented. Tell me the audit findings first, then show the full
component. Do not change any other files

Add Unity Gaming Services (UGS) Matchmaker support to my game as a temporary,
self-contained client for testing dedicated-server allocation. Do NOT modify my
existing netcode; only add a new component that drives matchmaking and then calls
my existing connect path.

First, audit (read-only) and tell me before writing code:
  1) The method my project calls to connect a client to a server by IP/address and
     port file, line, exact signature, and the TYPE of the port parameter.
  2) The transport in use and where connection data is ultimately set.
State these findings, then proceed.

Then create one MonoBehaviour that:
  - Initializes UGS (UnityServices.InitializeAsync) and signs in anonymously via
    Authentication.
  - For same-machine testing, sets a UNIQUE profile per running instance BEFORE
    sign-in (e.g. a value derived from the project path or a random id); if a player
    is already signed in, sign out first, then switch profile, then sign in so two
    local instances get distinct player IDs.
  - Creates a UGS ticket on a serialized queue-name field (default it to my queue),
    with one player entry using the signed-in player id.
  - Polls the ticket status no more often than every 3 seconds, and treats a failed
    poll as retryable (catch and continue the loop, do not abort) until an assignment
    arrives or a timeout elapses.
  - On assignment, reads the assigned IP and port and passes them into the connect
    method identified in the audit, converting the port to the exact type that method
    expects.
  - Exposes a simple temporary on-screen "Matchmake" button to trigger it.

Keep it minimal and commented. Tell me the audit findings first, then show the full
component. Do not change any other files

最后,将客户端接入场景,这是分支所需的唯一场景更改。在您的启动场景(这里以主菜单为例)中,创建一个空游戏对象:在 Hierarchy 中右键单击,选择 “Create Empty”,并将其命名为 “Matchmaker”。选中它后,添加 “Edgegap Matchmaker Client” 组件。

此处有一个重要字段:队列名称(queue name)。将其设置为您创建的队列的精确名称,例如 bossroom-queue。如果它与您的队列不匹配,票据将无处可去,因此请仔细检查。保存场景。

第 6 部分 - 测试

让我们在一台机器上用两个客户端来对整个流程进行端到端的测试。我们将使用 ParrelSync 来模拟第二个玩家,它会创建项目的一个单独副本;该分支已包含它。请勿在此分支中使用 Multiplayer Play Mode。

在编辑器中点击 Play。每个玩家都会进入主菜单,你会在左上角看到一个简单的“Matchmake”按钮,这是客户端提供的临时测试触发器。

在两个客户端中都选择“Matchmake”。每个客户端都会创建一个票证并开始等待。在幕后,Matchmaker 将这两个玩家分组,调用你的分配器,然后 Edgegap 为他们部署一个全新的服务器。在 Edgegap 控制面板中,你可以看到服务器出现,并被 Matchmaker 标记,位置是为你的玩家挑选的。

然后,两个玩家都会自动连接到同一个服务器,无需任何人输入地址。在一个窗口中移动,它就会在另一个窗口中同步。你可以在服务器的容器日志中确认整个循环:两张票证进入,一个匹配结果输出,调用了你的分配器,以及 Edgegap 上的一个活动服务器。

恭喜你,你的 Unity Matchmaker 现在正在 Edgegap 上部署并连接玩家。

第 7 部分 - 后续步骤

以上就是将 UGS Matchmaker 连接到 Edgegap 的全部步骤,Edgegap 为游戏开发者提供了简单、可按需在全球范围内部署的专用服务器托管和编排服务。

您游戏开发的下一步很可能是根据您的游戏定制匹配器的规则,包括队伍大小、技能范围等。Unity 的文档中包含了这些配置。对于一个完全托管的替代方案,Edgegap 自己免费、简单且全自动的匹配器也有其专属的教程。

如果您有任何问题,欢迎加入我们的 Discord

轻松在线游戏,尽在数分钟内

立即开始集成!

轻松在线获取游戏
及分钟内完成