本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
整合亚马逊GameLift进入虚幻引擎项目
本主题说明如何设置亚马逊GameLift虚幻引擎的 C++ 服务器 SDK 插件并将其集成到你的游戏项目中。
其他资源:
先决条件
在继续之前,请确保您已查看以下先决条件:
先决条件
-
一台能够运行虚幻引擎的计算机。有关虚幻引擎要求的更多信息,请参阅虚幻引擎的硬件和软件规格
文档。 -
微软 Visual Studio 2019 或更新版本
-
cMake 版本 3.1 或更高版本。
Python 版本 3.6 或更高版本。
-
PATH 上有一个 Git 客户端。
-
一个史诗游戏账号。在官方网站上注册一个账户虚幻引擎网站。
-
一个GitHub与你的虚幻引擎账号关联的账户。有关更多信息,请参见访问虚幻引擎的源代码GitHub
在虚幻引擎网站上。
注意
亚马逊GameLift目前支持以下版本的虚幻引擎:
-
4.22
-
4.23
-
4.24
-
4.25
-
4.26
-
4.27
-
5.1.0
-
5.1.1
从源代码构建虚幻引擎
通过 Epic 启动器下载的虚幻引擎编辑器的标准版本仅允许构建虚幻客户端应用程序。要构建虚幻服务器应用程序,你需要使用虚幻引擎 Github 存储库从源代码下载和构建虚幻引擎。如需更多信息,请参阅从源代码构建虚幻引擎
注意
如果您尚未这样做,请按照以下说明进行操作访问虚幻引擎的源代码GitHub
将虚幻引擎源代码克隆到你的开发环境中
-
在你选择的分支中将虚幻引擎源代码克隆到你的开发环境中。
git clone https://github.com/EpicGames/UnrealEngine.git
-
查看你用于开发游戏的版本的标签。例如,以下示例查看了虚幻引擎5.1.1版:
git checkout tags/5.1.1-release -b 5.1.1-release
-
导航到本地存储库的根文件夹。当你在根文件夹中时,运行以下文件:
Setup.bat
。 -
在根文件夹中,还要运行以下文件:
GenerateProjectFiles.bat
。 -
运行前面步骤中的文件(虚幻引擎解决方案文件)后,
UE5.sln
,已创建。打开视觉工作室,然后在 Visual Studio 编辑器中打开UE5.sln
文件。 -
在视觉工作室中,打开查看菜单并选择解决方案浏览器选项。这将打开虚幻项目节点的快捷菜单。在解决方案浏览器窗口,右键单击
UE5.sln
文件(可以将其列为UE5
),然后选择构建使用开发编辑器 Win64 目标构建虚幻项目。注意
构建可能需要一个多小时才能完成。
构建完成后,你就可以打开虚幻开发编辑器并创建或导入项目了。
为插件配置你的虚幻项目
按照以下步骤获取亚马逊GameLift适用于虚幻引擎的服务器 SDK 插件已为你的游戏服务器项目做好准备。
为插件配置您的项目
-
打开 Visual Studio 后,导航到解决方案浏览器窗格并选择
UE5
文件打开虚幻项目的快捷菜单。在上下文菜单中,选择设置为启动项目选项。 -
在 Visual Studio 窗口的顶部,选择开始调试(绿色箭头)。
此操作将启动你新的虚幻编辑器源代码构建实例。有关使用虚幻编辑器的更多信息,请参阅虚幻编辑器界面
在虚幻引擎文档网站上。 -
关闭你打开的 Visual Studio 窗口,因为虚幻编辑器会打开另一个包含虚幻项目和你的游戏项目的 Visual Studio 窗口。
-
在虚幻编辑器中,执行以下操作之一:
选择一个你想与亚马逊集成的现有虚幻项目GameLift。
-
创建新项目。在亚马逊上做实验GameLift虚幻引擎的插件,尝试使用虚幻引擎的第三人称模板。有关此模板的更多信息,请参见第三人称模板
在虚幻引擎文档网站上。 或者,使用以下设置配置新项目:
C++
包含入门内容
Desktop
项目名称。在本主题的示例中,我们命名了我们的项目
GameLiftUnrealApp
。
在视觉工作室里解决方案浏览器,导航到你的虚幻项目所在地。在虚幻中
Source
文件夹,找到一个名为的文件
。Your-application-name
.Target.cs例如:
GameLiftUnrealApp.Target.cs
。制作此文件的副本并命名副本:
。Your-application-name
Server.Target.cs打开新文件并进行以下更改:
更改
class
和constructor
以匹配文件名。更改
Type
从TargetType.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"); } }
您的项目现已配置为接受 AmazonGameLift服务器 SDK 插件。
下一个任务是为虚幻引擎构建 C++ 服务器 SDK 库,这样你就可以将它们导入到你的项目中。
为虚幻构建 C++ 服务器 SDK 库
-
下载亚马逊GameLift适用于虚幻的 C++ 服务器 SDK 插件
。 注意
由于路径超过 260 个字符的限制,将 SDK 放在默认下载目录中可能会导致构建失败。例如:
C:\Users\Administrator\Downloads\GameLift-SDK-Release-06_15_2023\GameLift-Cpp-ServerSDK-5.0.4
我们建议你将 SDK 移到另一个目录,例如
C:\GameLift-Cpp-ServerSDK-5.0.4
。 下载并安装 OpenSSL。有关下载 OpenSSL 的更多信息,请阅读 GithubOpenSSL 构建和安装
文档。 欲了解更多信息,请阅读 OpenSSLWindows 平台注意事项
文档。 注意
亚马逊GameLiftEC2 实例的 OpenSSL 主要版本应与您下载并用于构建 SDK 的 OpenSSL 的主要版本相匹配。
-
下载库后,为虚幻引擎构建 C++ 服务器 SDK 库。
在
GameLift-Cpp-ServerSDK-
下载的 SDK 中的目录,使用编译<version>
-DBUILD_FOR_UNREAL=1
参数并构建服务器 SDK。以下示例显示了如何使用进行编译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
文件夹:-
out\prefix\bin\aws-cpp-sdk-gamelift-server.dll
-
out\prefix\lib\aws-cpp-sdk-gamelift-server.lib
将两个库文件复制到
ThirdParty\GameLiftServerSDK\Win64
亚马逊中的文件夹GameLift虚幻引擎插件包。 -
使用以下步骤导入亚马逊GameLift插件到你的示例项目中。
导入亚马逊GameLift插件
-
添加亚马逊GameLift游戏的服务器 SDK 插件
.uproject
文件。在示例中,该应用程序被称为
GameLiftUnrealApp
,所以文件将是GameLiftUnrealApp.uproject
。 -
编辑
.uproject
文件将插件添加到您的游戏项目中。"Plugins": [ { "Name": "GameLiftServerSDK", "Enabled": true } ]
确保游戏是ModuleRules依赖于插件。打开
.Build.cs
归档并添加亚马逊GameLiftServerSDK 依赖关系。此文件位于
。Your-application-name
/Source//Your-application-name/
例如,教程文件路径是
../GameLiftUnrealApp/Source/GameLiftUnrealApp/GameLiftUnrealApp.Build.cs
。添加
"GameLiftServerSDK"
到列表的末尾PublicDependencyModuleNames
。public class GameLiftUnrealApp : ModuleRules { public GameLiftUnrealApp(TargetInfo Target) { PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "GameLiftServerSDK" }); bEnableExceptions = true; } }
该插件现在应该适用于您的应用程序。继续阅读下一节以整合亚马逊GameLift将功能融入您的游戏。
添加亚马逊GameLift你的虚幻项目的服务器代码
现在该插件已准备好使用,但您还没有编写使用它的代码。要开始使用,请使用现有的GameMode
虚幻引擎示例项目中包含的文件。以下代码示例包括修改后的代码GameMode.h
和GameMode.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 代码添加到项目后,请执行以下步骤创建游戏服务器版本并将其上传到亚马逊GameLift用于托管。
-
为以下两种目标类型构建游戏项目:开发编辑器和开发服务器。
注意
您无需重新构建解决方案。取而代之的是,只在下面构建项目
Games
与您的应用程序名称相匹配的文件夹。否则,Visual Studio 会重建整个 UE5 项目,这可能需要长达一个小时的时间。 -
两个版本都完成后,关闭 Visual Studio 并打开项目的
.uproject
文件在虚幻编辑器中将其打开。 -
在虚幻编辑器中,打包游戏的服务器版本。要选择目标,请转至平台,Windows然后选择
Your-application-name服务器
。 -
要开始构建服务器应用程序的过程,请转到平台,Windows然后选择打包项目。构建完成后,你有一个可执行文件,例如
GameLiftUnrealAppServer.exe
。 -
在虚幻编辑器中构建服务器应用程序会生成两个可执行文件。一个位于游戏编译文件夹的根目录中,用作实际服务器可执行文件的封装。
创建亚马逊时GameLift在服务器版本中,我们建议您将实际的服务器可执行文件作为运行时配置启动路径传入。例如,在你的游戏编译文件夹中,你可能有一个
GameLiftFPS.exe
根目录下的文件和另一个在根目录下的文件\GameLiftFPS\Binaries\Win64\GameLiftFPSServer.exe
。创建舰队时,我们建议您使用C:\GameLiftFPS\Binaries\Win64\GameLiftFPSServer.exe
作为运行时配置的启动路径。 -
确保在亚马逊上打开必要的 UDP 端口GameLift舰队,以便游戏服务器可以与游戏客户端通信。默认情况下,虚幻引擎使用端口
7777
。有关更多信息,请参见UpdateFleetPortSettings在亚马逊GameLift服务 API 参考指南。 -
创建一个
为你的游戏版本准备文件。每当游戏版本部署到亚马逊时,此安装脚本都会运行GameLift舰队。这是个例子install.bat
install.bat
文件:VC_redist.x64.exe /q UE5PrereqSetup_x64.exe /q
-
现在,您可以打包游戏版本并将其上传到亚马逊GameLift。
您用于构建的 OpenSSL 版本需要与游戏服务器使用的版本相匹配。确保在游戏服务器版本中打包了正确的 OpenSSL 版本。对于 Windows 操作系统,OpenSSL 格式为
.dll
。注意
将 OpenSSL DLL 打包到你的游戏服务器版本中。确保使用与构建游戏服务器时相同版本的 DLL。
-
libssl-3-x64.dll
-
libcrypto-3-x64.dll
将依赖项和游戏服务器可执行文件打包到压缩文件的根目录中。例如,
openssl-lib
dll 应与 dll 位于同一个目录中.exe
文件。 -
后续步骤
你已经配置并设置了虚幻引擎环境,现在可以开始集成亚马逊了GameLift进入你的游戏。
有关添加亚马逊的更多信息GameLift在你的游戏中,参见以下内容:
有关测试游戏的说明,请参阅 使用亚马逊GameLift本地测试您的集成。