將 Amazon GameLift 與 Unity 遊戲客戶端項目集成 - Amazon GameLift

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

將 Amazon GameLift 與 Unity 遊戲客戶端項目集成

注意

本主題提供適用於 Unity 的舊版 Amazon GameLift 外掛程式的資訊。版本 1.0.0(於 2021 年發布)使用 Amazon GameLift 服務器 SDK 4.x 或更早版本。有關使用服務器 SDK 5.x 並支持 Amazon 的最新版本插件的文檔 GameLift Anywhere,請參閱Amazon GameLift 插件統一指南服務器 SDK 5.x

本主題可協助您設定遊戲用戶端,透過後端服務連線到 Amazon GameLift 託管的遊戲工作階段。使用 Amazon 啟 GameLift APIs動配對、請求遊戲工作階段配置等等。

將代碼添加到後端服務項目以允許與 Amazon GameLift 服務進行通信。後端服務會處理所有遊戲用戶端與 GameLift 服務的通訊。如需後端服務的詳細資訊,請參閱。

後端伺服器會處理下列遊戲用戶端工作:

  • 為您的玩家自定義身份驗證。

  • 從 Amazon GameLift 服務要求有關使用中遊戲工作階段的資訊。

  • 建立新的遊戲工作階段。

  • 將玩家新增至現有的遊戲工作階段。

  • 將玩家從現有的遊戲工作階段中移除。

必要條件

在設定與 Amazon GameLift 用戶端的遊戲伺服器通訊之前,請先完成以下任務:

初始化遊戲用戶端

注意

本主題指的是統一版本 1.0.0 的 Amazon GameLift 插件,它使用服務器 SDK 4.x 或更早版本。

添加代碼以初始化遊戲客戶端。在啟動時運行此代碼,其他 Amazon GameLift 功能是必需的。

  1. 初始化AmazonGameLiftClientAmazonGameLiftClient使用預設用戶端組態或自訂組態來呼叫。如需如何設定用戶端的詳細資訊,請參閱在後端服務 GameLift 上設置 Amazon

  2. 為每個玩家生成一個獨特的玩家 ID,以連接到遊戲會話。如需詳細資訊,請參閱 產生玩家 ID

    下列範例顯示如何設定 Amazon 用 GameLift 戶端。

    public class GameLiftClient { private GameLift gl; //A sample way to generate random player IDs. bool includeBrackets = false; bool includeDashes = true; string playerId = AZ::Uuid::CreateRandom().ToString<string>(includeBrackets, includeDashes); private Amazon.GameLift.Model.PlayerSession psession = null; public AmazonGameLiftClient aglc = null; public void CreateGameLiftClient() { //Access Amazon GameLift service by setting up a configuration. //The default configuration specifies a location. var config = new AmazonGameLiftConfig(); config.RegionEndpoint = Amazon.RegionEndpoint.USEast1; CredentialProfile profile = null; var nscf = new SharedCredentialsFile(); nscf.TryGetProfile(profileName, out profile); AWSCredentials credentials = profile.GetAWSCredentials(null); //Initialize GameLift Client with default client configuration. aglc = new AmazonGameLiftClient(credentials, config); } }

在特定艦隊上建立遊戲工作階段

注意

本主題指的是統一版本 1.0.0 的 Amazon GameLift 插件,它使用服務器 SDK 4.x 或更早版本。

加入用於在已部署的機群中啟動新遊戲工作階段並使其可供玩家加入的程式碼。在 Amazon GameLift 建立新的遊戲工作階段並傳回後GameSession,您可以將玩家新增至其中。

  • 提出新遊戲工作階段的請求。

    • 如果您的遊戲使用艦隊,請使CreateGameSession()用艦隊或別名 ID、工作階段名稱以及遊戲的同時玩家數目上限來呼叫。

    • 如果您的遊戲使用佇列,請撥打電話StartGameSessionPlacement()

下列範例說明如何建立遊戲工作階段。

public Amazon.GameLift.Model.GameSession() { var cgsreq = new Amazon.GameLift.Model.CreateGameSessionRequest(); //A unique identifier for the alias with the fleet to create a game session in. cgsreq.AliasId = aliasId; //A unique identifier for a player or entity creating the game session cgsreq.CreatorId = playerId; //The maximum number of players that can be connected simultaneously to the game session. cgsreq.MaximumPlayerSessionCount = 4; //Prompt an available server process to start a game session and retrieves connection information for the new game session Amazon.GameLift.Model.CreateGameSessionResponse cgsres = aglc.CreateGameSession(cgsreq); string gsid = cgsres.GameSession != null ? cgsres.GameSession.GameSessionId : "N/A"; Debug.Log((int)cgsres.HttpStatusCode + " GAME SESSION CREATED: " + gsid); return cgsres.GameSession; }

將玩家加入遊戲工作階段

注意

本主題指的是統一版本 1.0.0 的 Amazon GameLift 插件,它使用服務器 SDK 4.x 或更早版本。

在 Amazon GameLift 建立新的遊戲工作階段並傳回GameSession物件後,您可以將玩家新增至其中。

  1. 透過建立新的玩家工作階段,在遊戲工作階段中預留一個玩家角位。使用CreatePlayerSessionCreatePlayerSessions與每個玩家的遊戲會話 ID 和唯一 ID 一起使用。

  2. Connect 至遊戲工作階段。擷取PlayerSession物件以取得遊戲工作階段的連線資訊。您可以使用此資訊建立與伺服器處理序的直接連線:

    1. 使用指定的連接埠以及伺服器處理序的DNS名稱或 IP 位址。

    2. 使用艦隊的DNS名稱和連接埠。如果您的叢集已啟用TLS憑證產生功能,則需要使用DNS名稱和連接埠。

    3. 參考玩家工作階段 ID。如果您的遊戲伺服器驗證傳入的玩家連線,則需要玩家工作階段 ID。

以下範例說明如何在遊戲工作階段中保留玩家位置。

public Amazon.GameLift.Model.PlayerSession CreatePlayerSession(Amazon.GameLift.Model.GameSession gsession) { var cpsreq = new Amazon.GameLift.Model.CreatePlayerSessionRequest(); cpsreq.GameSessionId = gsession.GameSessionId; //Specify game session ID. cpsreq.PlayerId = playerId; //Specify player ID. Amazon.GameLift.Model.CreatePlayerSessionResponse cpsres = aglc.CreatePlayerSession(cpsreq); string psid = cpsres.PlayerSession != null ? cpsres.PlayerSession.PlayerSessionId : "N/A"; return cpsres.PlayerSession; }

下面的代碼說明了如何將玩家與遊戲會話聯繫起來。

public bool ConnectPlayer(int playerIdx, string playerSessionId) { //Call ConnectPlayer with player ID and player session ID. return server.ConnectPlayer(playerIdx, playerSessionId); }

從遊戲工作階段移除玩家

注意

本主題指的是統一版本 1.0.0 的 Amazon GameLift 插件,它使用服務器 SDK 4.x 或更早版本。

您可以在玩家離開遊戲時將其從遊戲工作階段中移除。

  1. 通知 Amazon GameLift 服務玩家已與服務器進程斷開連接。RemovePlayerSession使用玩家的工作階段 ID 呼叫。

  2. 驗證RemovePlayerSession返回Success。然後,Amazon 將播放器插槽 GameLift 更改為可用,Amazon GameLift 可以將其分配給新玩家。

下列範例說明如何移除玩家工作階段。

public void DisconnectPlayer(int playerIdx) { //Receive the player session ID. string playerSessionId = playerSessions[playerIdx]; var outcome = GameLiftServerAPI.RemovePlayerSession(playerSessionId); if (outcome.Success) { Debug.Log (":) PLAYER SESSION REMOVED"); } else { Debug.Log(":(PLAYER SESSION REMOVE FAILED. RemovePlayerSession() returned " + outcome.Error.ToString()); } }