將 Amazon GameLift 整合至 Unity 專案 - Amazon GameLift

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

將 Amazon GameLift 整合至 Unity 專案

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

開始將遊戲伺服器部署到 Amazon GameLift 進行託管。使用適用於 Unity 的 Amazon GameLift 獨立外掛程式,您可以整合遊戲程式碼、部署簡單但完整的託管解決方案,以及測試您的遊戲元件。請參閱適用於 Unity 的 Amazon GameLift 外掛程式 (伺服器 SDK 5.x)

其他資源:

先決條件

若要使用適用於 Unity 的 Amazon GameLift C# 伺服器 SDK 外掛程式,您需要下列元件:

  • 外掛程式支援的開發環境和 Unity Editor 版本 (請參閱 取得 Amazon GameLift 開發工具)。如需有關 Unity 版本的資訊,請參閱 Unity 文件中的 Unity 的系統需求

  • Unity 套件的 Amazon GameLift 伺服器 SDK 外掛程式。此套件包含適用於 C# 的伺服器 SDK 5+。您可以從此網站下載最新版本:Amazon GameLift 入門。檢查每個伺服器 SDK 下載套件中的讀我檔案,以取得 Unity 版本支援。

  • 第三方範圍的登錄 UnityNuGet。此工具會管理第三方 DLLs。如需詳細資訊,請參閱 UnityNuGet Github 儲存庫。

設定 UnityNuGet

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

將 UnityNuGet 整合到您的遊戲專案
  1. 在 Unity Editor 中開啟您的專案時,前往主功能表,然後選取編輯專案設定。從選項中選擇 Package Manager 區段,然後開啟範圍註冊群組

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

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

    設定 UnityNuGet 後,請檢查 Unity 主控台中顯示的Assembly Version Validation錯誤。如果 NuGet 套件中強有名組件的繫結重新導向無法正確解析至 Unity 專案中的路徑,則會發生這些錯誤。若要解決此問題,請設定 Unity 的組件版本驗證:

    1. 在 Unity Editor 中,前往主功能表,然後選取編輯專案設定,然後開啟播放器區段。

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

安裝外掛程式。

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

安裝外掛程式
  1. 在 Unity Editor 中開啟您的專案時,前往主功能表,然後選取 WindowPackage Manager

  2. 選擇 按鈕以新增套件。選擇選項 從 tarball 新增套件

  3. 磁碟上的選取套件中,找到適用於 Unity 下載檔案的 Amazon GameLift C# Server SDK 外掛程式,然後選擇 Amazon GameLift Server SDK .tgz 檔案。選擇開啟以安裝外掛程式。

Amazon GameLift 伺服器 SDK 使用 log4net 架構來輸出日誌訊息。根據預設,它會設定為將訊息輸出至伺服器建置的終端,但 Unity 需要組態才能新增檔案記錄支援。您可以在 Amazon GameLift Server SDK 套件內匯入提供的範例,將此支援新增至您的專案。使用下列程序新增範例並設定 log4net:

設定 log4net 用於檔案輸出
  1. 在 Unity Editor 中開啟您的專案時,前往主功能表,然後選取 WindowPackage Manager

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

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

  4. log4net.config 檔案和隨附的LoggingConfiguration.cs指令碼會自動執行組態,現在已在專案的Assets/Samples資料夾中設定。

    注意

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

如需更詳細的說明和測試指南,請參閱README位於外掛程式下載中的 。

設定 Amazon GameLift Anywhere機群進行測試

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

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

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

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

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

  4. 為您的工作站運算產生身分驗證字符。此短期身分驗證包含權杖和過期日期。您的遊戲伺服器會使用它來驗證與 Amazon GameLift 服務的通訊。將身分驗證存放在工作站運算上,以便您執行的遊戲伺服器程序可以存取。

將 Amazon GameLift 伺服器程式碼新增至 Unity 專案

您的遊戲伺服器會與 Amazon GameLift 服務通訊,以接收指示並報告持續狀態。若要達成此目的,您可以新增使用 Amazon GameLift 伺服器 SDK 的遊戲伺服器程式碼。

提供的程式碼範例說明基本的必要整合元素。它使用 MonoBehavior來說明使用 Amazon GameLift 進行簡易遊戲伺服器初始化。此範例假設遊戲伺服器在 Amazon GameLift Anywhere機群上執行以進行測試。它包含程式碼以:

  • 初始化 Amazon GameLift API 用戶端。範例使用 版本InitSDK()搭配機Anywhere群和運算的伺服器參數。使用 WebSocket URL、機群 ID、運算名稱 (主機 ID) 和身分驗證字符,如上一個主題 所定義設定 Amazon GameLift Anywhere機群進行測試

  • 實作回呼函數來回應來自 Amazon GameLift 服務的請求,包括 OnStartGameSessionOnProcessTerminateonHealthCheck

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

本主題中呈現的程式碼會建立與 Amazon GameLift 服務和 的通訊。它也會實作一組回呼函數,以回應來自 的請求。如需每個函數和程式碼功能的詳細資訊,請參閱初始化伺服器程序。如需此程式碼中使用的 SDK 動作和資料類型的詳細資訊,請參閱 適用於 C# 和 Unity 的 Amazon GameLift 伺服器 SDK 5.x -- 動作

此範例說明如何新增所有必要的元素 ,如將 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); } } }

其他資源

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