将 Amazon GameLift 集成到虚幻引擎项目中 - Amazon GameLift
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

将 Amazon GameLift 集成到虚幻引擎项目中

本主题介绍如何为虚幻引擎设置 Amazon GameLift C++ 服务器 SDK 插件并将其集成到您的游戏项目中。

其他资源

先决条件

在继续之前,请确保您满足以下先决条件:

先决条件
  • 一台能够运行 Unreal Engine 的计算机。有关 Unreal Engine 要求的更多信息,请参阅 Unreal Engine 的硬件和软件规格文档。

  • Microsoft Visual Studio 2019 16.2.4 或更高版本。

  • EMR 版本 6.1.0 或更高版本

  • Python,版本 3.6 或更高版本。

  • PATH 上有一个 Git 客户端。

  • 一个 Epic 游戏账号。在 Unreal Engine 官方网站上注册一个账号。

  • 与你的虚幻引擎 GitHub 账号关联的账号。如需了解更多信息,请参阅虚幻引擎网站 GitHub上的访问虚幻引擎源代码

注意

Amazon GameLift 目前支持以下版本的虚幻引擎:

  • 4.22

  • 4.23

  • 4.24

  • 4.25

  • 4.26

  • 4.27

  • 5.1.0

  • 5.1.1

  • 5.2

  • 5.3

从源代码构建 Unreal Engine

通过Epic启动器下载的 Unreal Engine 编辑器的标准版本仅允许构建虚幻客户端应用程序。要构建虚幻服务器应用程序,您需要使用 Unreal Engine Github 存储库从源代码下载和构建 Unreal Engine 。如需了解更多信息,请参阅 Unreal Engine 文档网站上的从源代码构建 Unreal Engine 教程。

注意

如果你还没有这样做,请按照访问虚幻引擎源代码中的说明将你的 GitHub 账户关联 GitHub到你的Epic Games账户。

将 Unreal Engine 源代码克隆到您的开发环境中
  1. 在您选择的分支中将 Unreal Engine 源代码克隆到您的开发环境中。

    git clone https://github.com/EpicGames/UnrealEngine.git
  2. 查看您用来开发游戏的版本的标签。例如,以下示例查看了 Unreal Engine 版本5.1.1:

    git checkout tags/5.1.1-release -b 5.1.1-release
  3. 导航到本地存储库的根文件夹。当您在根文件夹中时,运行以下文件:Setup.bat

  4. 在根文件夹中,还要运行文件:GenerateProjectFiles.bat

  5. 运行前面步骤中的文件后,将创建 Unreal Engine 解决方案文件。UE5.sln打开 Visual Studio,然后在 Visual Studio 编辑器中打开该UE5.sln文件。

  6. 在 Visual Studio 中,打开查看菜单,然后选择解决方案资源管理器选项。这将打开虚幻项目节点的快捷菜单。在解决方案资源管理器窗口中,右键单击 UE5.sln 文件(可以将其列为只列出 UE5),然后选择构建,使用开发编辑器 Win64 目标构建 Unreal 项目。

    注意

    此教程可在 1 个小时内完成。

构建完成后,您就可以打开虚幻开发编辑器并创建或导入项目了。

为插件配置您的虚幻项目

按照以下步骤为你的游戏 GameLift 服务器项目准备好适用于虚幻引擎的Amazon服务器SDK插件。

为插件配置项目
  1. 打开Visual Studio后,导航到解决方案资源管理器窗格并选择UE5文件以打开虚幻项目的快捷菜单。在上下文菜单中,选择设置为启动项目选项。

  2. 在 Visual Studio 窗口的顶部,选择开始调试(绿色箭头)。

    这个动作会启动您新的源代码构建的虚幻编辑器实例。有关使用虚幻编辑器的更多信息,请参阅 Unreal Engine 文档网站上的虚幻编辑器界面

  3. 关闭您打开的 Visual Studio 窗口,因为虚幻编辑器会打开另一个包含虚幻项目和您的游戏项目的Visual Studio窗口。

  4. 在 编辑器中,执行以下操作之一:

    • 选择要与Amazon集成的现有虚幻项目 GameLift。

    • 创建新 项目 要尝试使用适用于虚幻引擎的Amazon GameLift 插件,请尝试使用虚幻引擎的第三人称视角模板。有关此模板的更多信息,请参阅 Unreal Engine 文档网站上的第三人称视角模板

      或者,使用以下设置配置新项目:

      • C++

      • 包含入门内容

      • Desktop

      • Project Name 在本主题的示例中,我们命名了我们的项目GameLiftUnrealApp

  5. 在Visual Studio的解决方案资源管理器中,导航到您的虚幻项目所在的位置。在虚幻Source文件夹中,找到一个名为的文件Your-application-name.Target.cs

    例如:GameLiftUnrealApp.Target.cs

  6. 为文件创建一个副本并将该副本命名为 Your-application-nameServer.Target.cs

  7. 打开新文件并进行以下更改:

    • 更改classconstructor以匹配文件名。

    • TypeTargetType.Game 更改为 TargetType.Server

    • 最终文件将类似于以下示例:

      public class GameLiftUnrealAppServerTarget : TargetRules { public GameLiftUnrealAppServerTarget(TargetInfo Target) : base(Target) { Type = TargetType.Server; DefaultBuildSettings = BuildSettingsVersion.V2; IncludeOrderVersion = EngineIncludeOrderVersion.Unreal5_1; ExtraModuleNames.Add("GameLiftUnrealApp"); } }

您的项目现已配置为接受 Amazon GameLift 服务器 SDK 插件。

下一个任务是为 Unreal Engine 构建 C++ 服务器软件开发工具包 库,这样您就可以将它们导入到您的项目中。

构建适用于 Unreal 的 C++ 服务器软件开发工具包库。
  1. 下载适用于虚幻引擎的 Amazon GameLift C++ 服务器 SDK 插件

    注意

    将软件开发工具包 放在默认下载目录可能会导致构建失败,因为路径超过 260 个字符的限制。例如:C:\Users\Administrator\Downloads\GameLift-SDK-Release-06_15_2023\GameLift-Cpp-ServerSDK-5.0.4

    例如,我们建议您将软件开发工具包移到另一个目录C:\GameLift-Cpp-ServerSDK-5.0.4

  2. 下载并安装 Maven。有关下载 OpenSSL 的更多信息,请阅读 Github Op enSSL 构建和安装文档。

    有关更多信息,请阅读适用于 Windows 平台的 OpenSSL 注意事项文档。

    注意

    你用来构建亚马逊 GameLift 服务器软件开发工具包的 OpenSSL 版本应与虚幻引擎用来打包游戏服务器的 OpenSSL 版本相匹配。你可以在虚幻安装目录中找到版本信息...Engine\Source\ThirdParty\OpenSSL

  3. 下载库后,为 Unreal Engine 构建 C++ 服务器软件开发工具包 库。

    在下载的软件开发工具包 的GameLift-Cpp-ServerSDK-<version>目录中,使用-DBUILD_FOR_UNREAL=1参数进行编译并构建服务器软件开发工具包。以下示例演示了如何使用 cmake 命令。

    在计算机终端中运行以下命令。

    mkdir cmake-build cmake.exe -G "Visual Studio 17 2022" -DCMAKE_BUILD_TYPE=Release -S . -B ./cmake-build -DBUILD_FOR_UNREAL=1 -A x64 cmake.exe --build ./cmake-build --target ALL_BUILD --config Release

    Windows 版本将在该out\gamelift-server-sdk\Release文件夹中创建以下二进制文件:

    • cmake-build\prefix\bin\aws-cpp-sdk-gamelift-server.dll

    • cmake-build\prefix\bin\aws-cpp-sdk-gamelift-server.lib

    将这两个库文件复制到亚马逊 GameLift 虚幻引擎插件包中的ThirdParty\GameLiftServerSDK\Win64文件夹。

使用以下步骤将 Amazon GameLift 插件导入您的示例项目。

导入 Amazon GameLift 插件
  1. 找到您在前面的过程中从插件中提取GameLiftServerSDK的文件夹。

  2. Plugins在您的游戏项目根文件夹中找到。(如果该文件夹不存在,则在那里创建。)

  3. 将该GameLiftServerSDK文件夹复制到Plugins

    这将允许虚幻项目看到该插件。

  4. 将 Amazon GameLift 服务器 SDK 插件添加到游戏.uproject文件中。

    在示例中,应用程序被调用GameLiftUnrealApp,因此文件将被调用GameLiftUnrealApp.uproject

  5. 编辑.uproject文件以将插件添加到您的游戏项目中。

    "Plugins": [ { "Name": "GameLiftServerSDK", "Enabled": true } ]
  6. 确保游戏依赖 ModuleRules 于插件。打开.Build.cs文件并添加 Amazon GameLiftServer SDK 依赖项。此文件位于 Your-application-name/Source//Your-application-name/

    例如,教程的文件路径是../GameLiftUnrealApp/Source/GameLiftUnrealApp/GameLiftUnrealApp.Build.cs

  7. "GameLiftServerSDK"添加到列表的末尾PublicDependencyModuleNames.

    using UnrealBuildTool; using System.Collections.Generic; public class GameLiftUnrealApp : ModuleRules { public GameLiftUnrealApp(TargetInfo Target) { PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "GameLiftServerSDK" }); bEnableExceptions = true; } }

该插件现在应该可以用于您的应用程序。继续下一节,将Amazon GameLift 功能集成到您的游戏中。

将 Amazon GameLift 服务器代码添加到你的虚幻项目中

现在插件已准备就绪,但您还没有编写代码来使用它。首先,请使用 Unreal Engine 示例项目中包含的现有GameMode文件。以下代码示例包括修改过的GameMode.hGameMode.cpp文件。

GameMode.h

// Copyright Epic Games, Inc. All Rights Reserved. #pragma once #include "CoreMinimal.h" #include "GameFramework/GameModeBase.h" #include "GameLift426TestGameMode.generated.h" DECLARE_LOG_CATEGORY_EXTERN(GameServerLog, Log, All); UCLASS(minimalapi) class AGameLift426TestGameMode : public AGameModeBase { GENERATED_BODY() public: AGameLift426TestGameMode(); protected: virtual void BeginPlay() override; private: void InitGameLift(); };

GameMode.cpp

// Copyright Epic Games, Inc. All Rights Reserved. #include "GameLift426TestGameMode.h" #include "GameLift426TestCharacter.h" #include "UObject/ConstructorHelpers.h" #include "GameLiftServerSDK.h" DEFINE_LOG_CATEGORY(GameServerLog); AGameLift426TestGameMode::AGameLift426TestGameMode() { UE_LOG(LogInit, Log, TEXT("Game Mode Constructor")); // set default pawn class to our Blueprinted character static ConstructorHelpers::FClassFinder<APawn> PlayerPawnBPClass(TEXT("/Game/ThirdPersonCPP/Blueprints/ThirdPersonCharacter")); if (PlayerPawnBPClass.Class != NULL) { DefaultPawnClass = PlayerPawnBPClass.Class; } } void AGameLift426TestGameMode::BeginPlay() { #if WITH_GAMELIFT InitGameLift(); #endif } void AGameLift426TestGameMode::InitGameLift() { UE_LOG(GameServerLog, Log, TEXT("Initializing the GameLift Server")); //Getting the module first. FGameLiftServerSDKModule* gameLiftSdkModule = &FModuleManager::LoadModuleChecked<FGameLiftServerSDKModule>(FName("GameLiftServerSDK")); //Define the server parameters FServerParameters serverParameters; //AuthToken returned from the "aws gamelift get-compute-auth-token" API. Note this will expire and require a new call to the API after 15 minutes. if (FParse::Value(FCommandLine::Get(), TEXT("-authtoken="), serverParameters.m_authToken)) { UE_LOG(GameServerLog, Log, TEXT("AUTH_TOKEN: %s"), *serverParameters.m_authToken) } //The Host/Compute ID of the GameLift Anywhere instance. if (FParse::Value(FCommandLine::Get(), TEXT("-hostid="), serverParameters.m_hostId)) { UE_LOG(GameServerLog, Log, TEXT("HOST_ID: %s"), *serverParameters.m_hostId) } //The EC2 or Anywhere Fleet ID. if (FParse::Value(FCommandLine::Get(), TEXT("-fleetid="), serverParameters.m_fleetId)) { UE_LOG(GameServerLog, Log, TEXT("FLEET_ID: %s"), *serverParameters.m_fleetId) } //The WebSocket URL (GameLiftServiceSdkEndpoint). if (FParse::Value(FCommandLine::Get(), TEXT("-websocketurl="), serverParameters.m_webSocketUrl)) { UE_LOG(GameServerLog, Log, TEXT("WEBSOCKET_URL: %s"), *serverParameters.m_webSocketUrl) } //The PID of the running process serverParameters.m_processId = FString::Printf(TEXT("%d"), GetCurrentProcessId()); UE_LOG(GameServerLog, Log, TEXT("PID: %s"), *serverParameters.m_processId); //InitSDK will establish a local connection with GameLift's agent to enable further communication. gameLiftSdkModule->InitSDK(serverParameters); //When a game session is created, GameLift sends an activation request to the game server and passes along the game session object containing game properties and other settings. //Here is where a game server should take action based on the game session object. //Once the game server is ready to receive incoming player connections, it should invoke GameLiftServerAPI.ActivateGameSession() auto onGameSession = [=](Aws::GameLift::Server::Model::GameSession gameSession) { FString gameSessionId = FString(gameSession.GetGameSessionId()); UE_LOG(GameServerLog, Log, TEXT("GameSession Initializing: %s"), *gameSessionId); gameLiftSdkModule->ActivateGameSession(); }; FProcessParameters params; params.OnStartGameSession.BindLambda(onGameSession); //OnProcessTerminate callback. GameLift will invoke this callback before shutting down an instance hosting this game server. //It gives this game server a chance to save its state, communicate with services, etc., before being shut down. //In this case, we simply tell GameLift we are indeed going to shutdown. params.OnTerminate.BindLambda([=]() { UE_LOG(GameServerLog, Log, TEXT("Game Server Process is terminating")); gameLiftSdkModule->ProcessEnding(); }); //This is the HealthCheck callback. //GameLift will invoke this callback every 60 seconds or so. //Here, a game server might want to check the health of dependencies and such. //Simply return true if healthy, false otherwise. //The game server has 60 seconds to respond with its health status. GameLift will default to 'false' if the game server doesn't respond in time. //In this case, we're always healthy! params.OnHealthCheck.BindLambda([]() { UE_LOG(GameServerLog, Log, TEXT("Performing Health Check")); return true; }); //This game server tells GameLift that it will listen on port 7777 for incoming player connections. params.port = 7777; //Here, the game server tells GameLift what set of files to upload when the game session ends. //GameLift will upload everything specified here for the developers to fetch later. TArray<FString> logfiles; logfiles.Add(TEXT("GameLift426Test/Saved/Logs/GameLift426Test.log")); params.logParameters = logfiles; //Calling ProcessReady tells GameLift this game server is ready to receive incoming game sessions! UE_LOG(GameServerLog, Log, TEXT("Calling Process Ready")); gameLiftSdkModule->ProcessReady(params); }
搭建您的游戏服务器

将服务器 SDK 代码添加到项目后,请按照以下步骤创建游戏服务器版本并将其上传到 Amazon GameLift 进行托管。

  1. 为以下两种目标类型构建游戏项目:开发编辑器和开发服务器

    注意

    您不需要重新构建解决方案。相反,只在与您的应用程序名称相匹配的Games文件夹下构建项目。否则,Visual Studio 会重建整个 UE5 项目,这可能需要长达一个小时的时间。

  2. 两个构建都完成后,关闭Visual Studio并打开项目的.uproject文件以在虚幻编辑器中将其打开。

  3. 在虚幻编辑器中,打包游戏的服务器版本。要选择目标,请前往 “平台”、“Windows”,然后选择 “Y our-application-name 服务器”。

  4. 要开始构建服务器应用程序的过程,请转到平台Windows,然后选择Package Project。构建完成后,您就有了可执行文件,例如GameLiftUnrealAppServer.exe

  5. 在虚幻编辑器中构建服务器应用程序会生成两个可执行文件。一个位于游戏构建文件夹的根目录中,用作实际服务器可执行文件的封装器。

    使用您的服务器版本创建 Amazon GameLift 队列时,我们建议您传入实际的服务器可执行文件作为运行时配置启动路径。例如,在您的游戏版本文件夹中,您的根目录可能有一个GameLiftFPS.exe文件,另一个位于根目录\GameLiftFPS\Binaries\Win64\GameLiftFPSServer.exe。创建队列时,我们建议您使用C:\GameLiftFPS\Binaries\Win64\GameLiftFPSServer.exe作为运行时配置的启动路径。

  6. 确保在 Amazon GameLift 舰队上打开必要的 UDP 端口,以便游戏服务器可以与游戏客户端通信。默认情况下, Unreal Engine 使用端口7777。有关更多信息,请参阅 UpdateFleetPortSettingsAmazon GameLift 服务 API 参考指南。

  7. 为您的游戏版本创建install.bat文件。每当游戏版本部署到 Amazon GameLift 舰队时,都会运行此安装脚本。下面给出了一个示例文件:

    VC_redist.x64.exe /q UE5PrereqSetup_x64.exe /q
  8. 现在,您可以将游戏版本打包并上传到 Amazon GameLift。

    您用于构建的 OpenSSL 版本需要与游戏服务器使用的版本相匹配。确保在游戏服务器版本中打包正确的 OpenSSL 版本。对于 Windows 操作系统,OpenSSL 格式为。.dll

    注意

    Package 将 OpenSSL DLL 打包到您的游戏服务器版本中。请务必使用与构建游戏服务器时相同版本的 DLL。

    • libssl-3-x64.dll

    • libcrypto-3-x64.dll

    Package 将依赖项和游戏服务器可执行文件一起打包到 zip 文件的根目录中。例如,openssl-libdll 应与.exe文件位于同一目录中。

后续步骤

你已经配置并设置了虚幻引擎环境,现在可以开始将Amazon GameLift 集成到你的游戏中了。

有关在游戏中添加 Amazon GameLift 的更多信息,请参阅以下内容:

有关测试游戏的说明,请参阅 使用 Amazon GameLift Local 测试您的集成