Amazon GameLift
开发人员指南 (版本 )
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 Amazon AWS 入门

将 Amazon GameLift 添加到 Unity 游戏服务器项目

本主题可帮助您设置 Amazon GameLift C# 服务器开发工具包并将 Amazon GameLift 集成到 Unity 游戏服务器项目中。如果您不确定 Amazon GameLift 是否支持您所使用的操作系统的信息,请参阅对于游戏服务器

设置适用于 Unity 的 C# Server 开发工具包

执行以下步骤以构建适用于 C# 的 Amazon GameLift Server 开发工具包并将其添加到您的 Unity 游戏服务器项目。

设置适用于 Unity 的 Amazon GameLift Server 开发工具包

  1. 下载 Amazon GameLift 服务器开发工具包 要验证是否支持您的游戏系统要求,请参阅 Amazon GameLift 软件开发工具包。此服务器开发工具包中包含以下两种解决方案:

    • GameLiftServerSDKNet35.sln 适用于 .Net framework 3.5

    • GameLiftServerSDKNet45.sln 适用于 .Net framework 4.5

    仅 .Net 3.5 解决方案与 Unity 兼容。

  2. 构建 C# 开发工具包库。 有关最低要求和附加构建选项,请参阅 C# 服务器开发工具包的 文件。在 IDE 中,加载 .Net 3.5 解决方案文件。要生成软件开发工具包库,请还原 NuGet 程序包并构建解决方案。

  3. 检查 API 兼容性设置。 在 Unity Editor 中,请转到 FileBuild SettingsPlayer Settings。在 Other Settings 部分中的 Optimization 下,确保将 API 兼容性级别设置为 .Net 2.0

  4. 将 Amazon GameLift 库添加到 Unity。 在 Unity Editor 中,将以下库导入到 Assets/Plugins 目录:

    • EngineIoClientDotNet.dll

    • GameLiftServerSDKNet35.dll

    • log4net.dll

    • Newtonsoft.Json.dll

    • protobuf-net.dll

    • SocketIoClientDotNet.dll

    • System.Threading.Tasks.NET35.dll

    • WebSocket4Net.dll

添加 Amazon GameLift Server 代码

有关添加 Amazon GameLift 功能的更多信息,请参阅以下主题:

以下代码示例使用 MonoBehavior 演示使用 Amazon GameLift 执行的简单游戏服务器初始化。

using UnityEngine; using Aws.GameLift.Server; using System.Collections.Generic; public class GameLiftServerExampleBehavior : MonoBehaviour { //This is an example of a simple integration with GameLift server SDK that makes game server processes go active on Amazon GameLift public void Start() { var listeningPort = 7777; //InitSDK establishes a local connection with the Amazon GameLift agent to enable further communication. var initSDKOutcome = GameLiftServerAPI.InitSDK(); if (initSDKOutcome.Success) { ProcessParameters processParameters = new ProcessParameters( (gameSession) => { //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() GameLiftServerAPI.ActivateGameSession(); }, () => { //OnProcessTerminate callback. GameLift invokes 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 shut down. GameLiftServerAPI.ProcessEnding(); }, () => { //This is the HealthCheck callback. //GameLift invokes 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! return true; }, listeningPort, //This game server tells GameLift that it will listen on port 7777 for incoming player connections. new LogParameters(new List<string>() { //Here, the game server tells GameLift what set of files to upload when the game session ends. //GameLift uploads everything specified here for the developers to fetch later. "/local/game/logs/myserver.log" })); //Calling ProcessReady tells GameLift this game server is ready to receive incoming game sessions! var processReadyOutcome = GameLiftServerAPI.ProcessReady(processParameters); if (processReadyOutcome.Success) { print("ProcessReady success."); } else { print("ProcessReady failure : " + processReadyOutcome.Error.ToString()); } } else { print("InitSDK failure : " + initSDKOutcome.Error.ToString()); } } void OnApplicationQuit() { //Make sure to call GameLiftServerAPI.Destroy() when the application quits. This resets the local connection with GameLift's agent. GameLiftServerAPI.Destroy(); } }