

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

# Amazon GameLift Servers 與 Unity 遊戲用戶端專案整合
<a name="integration-unity-client-sdk4"></a>

**注意**  
本主題提供 Unity 舊版Amazon GameLift Servers 外掛程式的資訊。1.x 版使用適用於 4.x Amazon GameLift Servers 或更早版本的伺服器 SDK。如需使用伺服器 SDK 5.x 並支援 Amazon GameLift Servers Anywhere 和受管容器託管等較新功能之最新外掛程式版本的文件，請參閱 [Amazon GameLift Servers Unity 的外掛程式 （伺服器 SDK 5.x)](unity-plug-in.md)。

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

將程式碼新增至後端服務專案，以允許與服務通訊Amazon GameLift Servers。後端服務會處理與 GameLift 服務的所有遊戲用戶端通訊。如需後端服務的詳細資訊，請參閱 。

 後端伺服器會處理下列遊戲用戶端任務：
+ 為您的玩家自訂身分驗證。
+ 向 Amazon GameLift Servers 服務請求有關作用中遊戲工作階段的資訊。
+ 建立新的遊戲工作階段。
+ 將玩家新增至現有的遊戲工作階段。
+ 從現有的遊戲工作階段中移除玩家。

**Topics**
+ [先決條件](#integration-unity-client-sdk4-prereq)
+ [初始化遊戲用戶端](#integration-unity-client-sdk4-initialize)
+ [在特定機群上建立遊戲工作階段](#integration-unity-client-sdk4-game-session)
+ [將玩家新增至遊戲工作階段](#integration-unity-client-sdk4-add-player)
+ [從遊戲工作階段中移除玩家](#integration-unity-client-sdk4-remove-player)

## 先決條件
<a name="integration-unity-client-sdk4-prereq"></a>

在設定與Amazon GameLift Servers用戶端的遊戲伺服器通訊之前，請先完成下列任務：
+ [設定 AWS 使用者帳戶](setting-up-aws-login.md)
+ [安裝和設定 外掛程式](unity-plug-in-sdk4.md#unity-plug-in-sdk4-install)
+ [Amazon GameLift Servers 與 Unity 遊戲伺服器專案整合](integration-unity-server-sdk4.md)
+ [部署 的託管機群 Amazon GameLift Servers](fleets-intro.md)

## 初始化遊戲用戶端
<a name="integration-unity-client-sdk4-initialize"></a>

**注意**  
本主題參考 Unity 1.0.0 版的Amazon GameLift Servers 外掛程式，其使用伺服器 SDK 4.x 或更早版本。

新增程式碼以初始化遊戲用戶端。啟動時執行此程式碼，這是其他 Amazon GameLift Servers函數的必要項目。

1. 初始化 `AmazonGameLiftClient`。`AmazonGameLiftClient` 使用預設用戶端組態或自訂組態呼叫 。如需如何設定用戶端的詳細資訊，請參閱 [設定 Amazon GameLift Servers API](gamelift-sdk-client-api.md#gamelift-sdk-client-api-initialize)。

1. 為每個玩家產生唯一的玩家 ID，以連接到遊戲工作階段。如需更多資訊，請參閱[產生玩家 IDs](player-sessions-player-identifiers.md)。

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

   ```
   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 Servers 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 Amazon GameLift Servers Client with default client configuration.
           aglc = new AmazonGameLiftClient(credentials, config); 
           
       }
   }
   ```

## 在特定機群上建立遊戲工作階段
<a name="integration-unity-client-sdk4-game-session"></a>

**注意**  
本主題參考 Unity 1.0.0 版的Amazon GameLift Servers 外掛程式，其使用伺服器 SDK 4.x 或更早版本。

加入用於在已部署的機群中啟動新遊戲工作階段並使其可供玩家加入的程式碼。在 Amazon GameLift Servers 建立新的遊戲工作階段並傳回 之後 `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;
}
```

## 將玩家新增至遊戲工作階段
<a name="integration-unity-client-sdk4-add-player"></a>

**注意**  
本主題參考 Unity 1.0.0 版的Amazon GameLift Servers 外掛程式，其使用伺服器 SDK 4.x 或更早版本。

在 Amazon GameLift Servers 建立新的遊戲工作階段並傳回 `GameSession` 物件之後，您可以將玩家新增至該工作階段。

1. 透過建立新的玩家工作階段，在遊戲工作階段中預留玩家位置。使用 `CreatePlayerSession`或 `CreatePlayerSessions`搭配遊戲工作階段 ID 和每個玩家的唯一 ID。

1. 連線至遊戲工作階段。擷取`PlayerSession`物件以取得遊戲工作階段的連線資訊。您可以使用此資訊建立與伺服器程序的直接連線：

   1. 使用指定的連接埠，以及伺服器程序的 DNS 名稱或 IP 地址。

   1. 使用機群的 DNS 名稱和連接埠。如果您的機群已啟用 TLS 憑證產生，則需要 DNS 名稱和連接埠。

   1. 參考玩家工作階段 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);
}
```

## 從遊戲工作階段中移除玩家
<a name="integration-unity-client-sdk4-remove-player"></a>

**注意**  
本主題參考 Unity 1.0.0 版的Amazon GameLift Servers 外掛程式，其使用伺服器 SDK 4.x 或更早版本。

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

1. 通知 Amazon GameLift Servers服務玩家已中斷與伺服器程序的連線。`RemovePlayerSession` 使用玩家的工作階段 ID 呼叫 。

1. 確認 `RemovePlayerSession`傳回 `Success`。然後， 會將玩家位置Amazon GameLift Servers變更為可用，Amazon GameLift Servers可指派給新的玩家。

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

```
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());
    }
}
```