GameLift 將亞馬遜整合到統一項目中 - Amazon GameLift

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

GameLift 將亞馬遜整合到統一項目中

本主題說明如何為 Unity 設定 Amazon GameLift C# 伺服器 SDK 外掛程式,並將其整合到您的遊戲專案中。

其他資源:

先決條件

若要使用亞馬遜 GameLift C# 伺服器 SDK 外掛程式的統一,您需要下列元件:

設定 UnityNuGet

如果您尚未為您的遊戲專案 UnityNuGet 設定,請使用下列步驟使用 Unity 套件管理員安裝工具。或者,您可以使用 NuGet CLI 手動下載 DLL。如需詳細資訊,請參閱適用於統一的 Amazon GameLift C# 伺服器開發套件README

整合 UnityNuGet 到您的遊戲專案
  1. 在 Unity 編輯器中打開項目後,轉到主菜單並選擇編輯項目設置。從選項中,選擇「Package 件管理員」區段,然後開啟「範圍登錄」群組。

  2. 選擇 + 按鈕,並為 UnityNuGet 範圍登錄輸入下列值:

    Name: Unity NuGet URL: https://unitynuget-registry.azurewebsites.net Scope(s): org.nuget
  3. 對於統一 2021 版本的用戶:

    設置後 UnityNuGet,檢查 Unity 控制台中顯示的Assembly Version Validation錯誤。如果 NuGet封裝中強式名稱組件的繫結重新導向未正確解析為 Unity 專案中的路徑,就會發生這些錯誤。若要解決此問題,請設定 Unity 的組件版本驗證:

    1. 在 Unity 編輯器中,轉到主菜單,然後選擇編輯項目設置,然後打開播放器部分。

    2. 取消選取「組件版本驗證」選項。

安裝插件

使用下列程序安裝適用於 Unity 的 Amazon GameLift C# 伺服器 SDK 外掛程式,並設定 log4net 記錄。

安裝外掛程式
  1. 在 Unity 編輯器中打開項目後,轉到主菜單並選擇窗口,P ackage 管理器。

  2. 選擇「+」按鈕以新增套件。選擇選項從壓縮包中添加包裹

  3. 在選取磁碟上的套件中,找出 Unity 下載檔案的 Amazon GameLift C# 伺服器 SDK 外掛程式,然後選擇 Amazon GameLift 伺服器 SDK .tgz 檔案。選擇「開啟」以安裝外掛程式。

亞馬遜 GameLift 服務器開發套件使用 log4net 框架來輸出日誌消息。默認情況下,它被配置為將消息輸出到服務器構建的終端,但 Unity 需要配置以添加文件日誌記錄支持。您可以在 Amazon GameLift Server SDK 套件中匯入提供的範例,將此支援新增至您的專案。請使用下列程序來新增範例並設定 log4net:

若要為檔案輸出設定 log4net
  1. 在 Unity 編輯器中打開項目後,轉到主菜單並選擇窗口,P ackage 管理器。

  2. 從下拉式功能表中,選取套件:在專案中,然後從套件清單中選取 Amazon GameLift Server SDK。這會開啟套件詳細資訊。

  3. 在套件詳細資訊中,選取 [範例] 群組選項,然後按 [匯入]。

  4. log4net.config文件和隨附的LoggingConfiguration.cs腳本會自動執行配置,該配置現在已在項目的文件Assets/Samples夾中進行設置。

    注意

    如果您需要將log4net.config檔案移至專案中的其他資料夾,您也必須LoggingConfiguration.cs使用新路徑更新指令碼中的組態檔案路徑。如需詳細資訊,請參閱有關配置 log4net 的 log4net 手冊

有關更詳細的說明和測試指導,請參閱插件下載中的。README

建立亞馬遜 GameLift Anywhere車隊進行測試

您可以將開發工作站設定為 Amazon GameLift Anywhere 託管叢集,以反覆測試您的 Amazon GameLift 整合。使用此設定,您可以在工作站上啟動遊戲伺服器程序、將玩家加入或配對請求傳送 GameLift至 Amazon 以開始遊戲工作階段,以及將用戶端連接到新的遊戲工作階段。將您自己的工作站設置為託管服務器後,您可以監控與 Amazon 的遊戲整合的各個方面 GameLift。

如需設定工作站的指示,請參閱使用 Amazon 設置本地測試 GameLift Anywhere以完成下列步驟:

  1. 為您的工作站建立自訂位置。

  2. 使用您的新自訂位置建立 Amazon GameLift Anywhere 叢集。如果成功,此要求會傳回叢集 ID。請記下此值,因為您稍後會需要它。

  3. 將您的工作站註冊為新Anywhere叢集中的運算。提供唯一的運算名稱,並指定工作站的 IP 位址。如果成功,此要求會以 WebSocket URL 的形式傳回服務 SDK 端點。請記下此值,因為您稍後會需要它。

  4. 為您的工作站計算產生驗證 Token。這種短期身份驗證包括令牌和到期日期。您的遊戲伺服器會使用它來驗證與 Amazon GameLift 服務的通訊。將驗證儲存在您的工作站計算機上,以便執行中的遊戲伺服器程序可以存取它。

添加亞馬遜 GameLift 服務器代碼到您的 Unity 項目

您的遊戲伺服器會與 Amazon GameLift 服務進行通訊,以接收指示並報告進行中的狀態。若要完成此操作,您可以新增使用 Amazon 伺服器 SDK 的遊戲 GameLift伺服器程式碼。

提供的程式碼範例說明基本必要的整合元素。它使用 a 來說明MonoBehavior使用亞馬遜進行簡單的遊戲服務器初始化 GameLift。此範例假設遊戲伺服器在 Amazon GameLift Anywhere 叢集上執行以進行測試。它包括以下代碼:

  • 初始化亞馬遜 GameLift API 客戶端。此範例使用的版本InitSDK()與伺服器參數適用於您的Anywhere叢集和運算。使用 WebSocket URL、叢集 ID、計算名稱 (主機 ID) 和驗證 Token,如上一個主題所定義建立亞馬遜 GameLift Anywhere車隊進行測試

  • 實作回呼函數以回應 Amazon GameLift 服務的請求OnStartGameSession,包括OnProcessTerminate、和onHealthCheck

  • 使用指定的連接埠呼叫 ProcessReady (),以在程序準備好主持遊戲工作階段時通知 Amazon GameLift 服務。

本主題中介紹的程式碼建立與 Amazon GameLift 服務和的通訊。它還實現了一組回調函數,以響應來自. 有關每個函數以及代碼功能的詳細信息,請參閱初始化服務器進程。有關此代碼中使用的 SDK 操作和數據類型的更多信息,請閱讀C# 的亞馬遜GameLift伺服器 SDK 參考

此範例顯示如何新增所有必要元素,如將 Amazon 新增 GameLift 至遊戲伺服器中所述。它包括:

如需新增 Amazon GameLift 功能的詳細資訊,請參閱下列主題:

using System.Collections.Generic; using Aws.GameLift.Server; using UnityEngine; public class ServerSDKManualTest : MonoBehaviour { //This example is a simple integration that initializes a game server process //that is running on an Amazon GameLift Anywhere fleet. void Start() { //Identify port number (hard coded here for simplicity) the game server is listening on for player connections var listeningPort = 7777; //WebSocketUrl from RegisterHost call var webSocketUrl = "wss://us-west-2.api.amazongamelift.com"; //Unique identifier for this process var processId = "myProcess"; //Unique identifier for your host that this process belongs to var hostId = "myHost"; //Unique identifier for your fleet that this host belongs to var fleetId = "myFleet"; //Authorization token for this host process var authToken = "myAuthToken"; //Server parameters are required for a GameLift Anywhere fleet. //They are not required for a GameLift managed EC2 fleet. ServerParameters serverParameters = new ServerParameters( webSocketUrl, processId, hostId, fleetId, authToken); //InitSDK establishes a local connection with an Amazon GameLift agent //to enable further communication. var initSDKOutcome = GameLiftServerAPI.InitSDK(serverParameters); if (initSDKOutcome.Success) { //Implement callback functions ProcessParameters processParameters = new ProcessParameters( //Implement OnStartGameSession callback (gameSession) => { //GameLift sends a game session activation request to the game server //with game session object containing game properties and other settings. //Here is where a game server takes action based on the game session object. //When the game server is ready to receive incoming player connections, //it invokes the server SDK call ActivateGameSession(). GameLiftServerAPI.ActivateGameSession(); }, (updateGameSession) => { //GameLift sends a request when a game session is updated (such as for //FlexMatch backfill) with an updated game session object. //The game server can examine matchmakerData and handle new incoming players. //updateReason explains the purpose of the update. }, () => { //Implement callback function OnProcessTerminate //GameLift invokes this callback before shutting down the instance hosting this game server. //It gives the game server a chance to save its state, communicate with services, etc., //and initiate shut down. When the game server is ready to shut down, it invokes the //server SDK call ProcessEnding() to tell GameLift it is shutting down. GameLiftServerAPI.ProcessEnding(); }, () => { //Implement callback function OnHealthCheck //GameLift invokes this callback approximately every 60 seconds. //A game server might want to check the health of dependencies, etc. //Then it returns health status true if healthy, false otherwise. //The game server must respond within 60 seconds, or GameLift records 'false'. //In this example, the game server always reports healthy. return true; }, //The game server gets ready to report that it is ready to host game sessions //and that it will listen on port 7777 for incoming player connections. listeningPort, new LogParameters(new List<string>() { //Here, the game server tells GameLift where to find game session log files. //At the end of a game session, GameLift uploads everything in the specified //location and stores it in the cloud for access later. "/local/game/logs/myserver.log" })); //The game server calls ProcessReady() to tell GameLift it's ready to host 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.ProcessEnding() and GameLiftServerAPI.Destroy() before terminating the server process. //These actions notify Amazon GameLift that the process is terminating and frees the API client from memory. GenericOutcome processEndingOutcome = GameLiftServerAPI.ProcessEnding(); GameLiftServerAPI.Destroy(); if (processEndingOutcome.Success) { Environment.Exit(0); } else { Console.WriteLine("ProcessEnding() failed. Error: " + processEndingOutcome.Error.ToString()); Environment.Exit(-1); } } }

其他資源

使用下列資源來測試您的遊戲伺服器並擴充功能: