Amazon GameLift Servers と Unity ゲームクライアントプロジェクトを統合する - Amazon GameLift Servers

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

Amazon GameLift Servers と Unity ゲームクライアントプロジェクトを統合する

注記

このトピックでは、Unity 用 Amazon GameLift Servers プラグインの前のバージョンについて説明します。バージョン 1.x では、サーバー SDK for Amazon GameLift Servers 4.x 以前が使用されます。サーバー SDK 5.x を使用し、Amazon GameLift Servers Anywhere やマネージドコンテナホスティングなどの新しい機能をサポートする最新のプラグインバージョンのドキュメントについては、「Unity 用 Amazon GameLift Servers プラグイン (サーバー SDK 5.x)」を参照してください。

このトピックは、バックエンドサービスを通じて Amazon GameLift Servers がホストするゲームセッションに接続するゲームクライアントをセットアップするのに役立ちます。Amazon GameLift Servers API を使用して、マッチメーキングの開始、ゲームセッションプレイスメントのリクエストなどを行います。

Amazon GameLift Serversサービスと通信できるようにするコードをバックエンドサービスプロジェクトに追加します。バックエンドサービスは GameLift サービスとのすべてのゲームクライアント通信を処理します。バックエンドサービスの詳細については、「」を参照してください。

バックエンドサーバーは次のゲームクライアントタスクを処理します。

  • プレイヤーの認証をカスタマイズします。

  • Amazon GameLift Servers サービスからのアクティブなゲームセッションについての情報をリクエストします。

  • 新しいゲームセッションを作成する。

  • 既存のゲームセッションにプレーヤーを追加する。

  • 既存のゲームセッションからプレイヤーを削除する。

前提条件

Amazon GameLift Serversクライアントとのゲームサーバー通信をセットアップする前に、次のタスクを実行します。

ゲームクライアントを初期化する

注記

このトピックでは、サーバー SDK 4.x 以前を使用する Unity バージョン 1.0.0 用の Amazon GameLift Servers プラグインについて説明します。

ゲームクライアントを初期化するコードを追加する。このコードを起動時に実行する。Amazon GameLift Servers の他の機能にも必要です。

  1. AmazonGameLiftClient を初期化する。デフォルトのクライアント設定またはカスタム設定のいずれかで AmazonGameLiftClient を呼び出す。クライアントの設定方法に関する詳細については、「Amazon GameLift Servers APIをセットアップします。」を参照してください。

  2. ゲームセッションに接続するための一意のプレイヤー ID を生成する。詳細については、「プレイヤー ID を生成する」を参照してください。

    次の例は、 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); } }

特定のフリートにゲームセッションを作成する

注記

このトピックでは、サーバー SDK 4.x 以前を使用する Unity バージョン 1.0.0 用の Amazon GameLift Servers プラグインについて説明します。

デプロイ済みフリートで新しいゲームセッションを起動し、それらのセッションをプレイヤーが使用できるようにするコードを追加します。Amazon GameLift Servers が新しいゲームセッションを作成して GameSession を返したら、そのゲームセッションにプレイヤーを追加できます。

  • 新しいゲームセッションをリクエストします。

    • ゲームでフリートを使用する場合は、フリートまたはエイリアス ID、セッション名、およびゲームの最大同時プレイヤー数を指定して CreateGameSession() を呼び出します。

    • ゲームがキューを使用している場合は、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; }

プレイヤーをゲームセッションに追加する

注記

このトピックでは、サーバー SDK 4.x 以前を使用する Unity バージョン 1.0.0 用の Amazon GameLift Servers プラグインについて説明します。

Amazon GameLift Servers が新しいゲームセッションを作成して GameSession オブジェクトを返したら、そのゲームセッションにプレイヤーを追加できます。

  1. 新しいプレイヤーセッションを作成して、ゲームセッションにプレイヤースロットを予約します。ゲームセッション ID と各プレイヤーの固有の ID を指定して、CreatePlayerSession または CreatePlayerSessions を使用します。

  2. ゲーム セッションに接続します。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); }

ゲームセッションからプレイヤーを削除する

注記

このトピックでは、サーバー SDK 4.x 以前を使用する Unity バージョン 1.0.0 用の Amazon GameLift Servers プラグインについて説明します。

プレイヤーがゲームを離れると、そのプレイヤーをゲームセッションから削除できます。

  1. プレイヤーがサーバープロセスから切断されたことをAmazon GameLift Servers サービスに通知します。プレイヤーのセッション ID で RemovePlayerSession を呼び出します。

  2. RemovePlayerSessionSuccess を返すことを確認します。それに応じて、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()); } }