Amazon GameLift Servers を Unity プロジェクトに統合する - Amazon GameLift Servers

Amazon GameLift Servers を Unity プロジェクトに統合する

Amazon GameLift Servers SDK for Unity をゲームプロジェクトに統合してサーバー SDK 機能セット全体にアクセスする方法について説明します。

ヒント

デプロイを高速化するには、Unity 用の Amazon GameLift Servers スタンドアロンプラグインを試してください。最小限のセットアップでゲームサーバーをすばやくデプロイするためのガイド付き UI ワークフローを提供するため、ゲームコンポーネントを実際に試すことができます。「Unity 用 Amazon GameLift Servers プラグイン (サーバー SDK 5.x)」を参照してください。

その他のリソース:

サーバー SDK for Unity をインストールする

GitHub から Unity 用のオープンソース Amazon GameLift Servers を取得します。リポジトリの readme ファイルには、前提条件とインストール手順が含まれています。

テスト用に Amazon GameLift Servers Anywhere フリートをセットアップする

開発ワークステーションを Amazon GameLift Servers Anywhere ホスティングフリートとしてセットアップして、Amazon GameLift Servers 統合を繰り返しテストできます。この設定では、ワークステーションでゲームサーバープロセスを開始し、プレイヤー参加リクエストまたはマッチメーキングリクエストを Amazon GameLift Servers に送信してゲームセッションを開始し、クライアントを新しいゲームセッションに接続できます。独自のワークステーションをホスティングサーバーとしてセットアップすると、Amazon GameLift Servers とのゲーム統合のあらゆる側面をモニタリングできます。

ワークステーションの設定方法については、「Amazon GameLift Servers Anywhereでローカルテストを設定する」を参照して次の手順を完了してください。

  1. ワークステーション用のカスタムロケーションを作成します。

  2. 新しいカスタムロケーションを使用して Amazon GameLift Servers Anywhere フリートを作成します。成功すると、このリクエストはフリート ID を返します。この値をメモしておきます。これは後で必要になります。

  3. ワークステーションを新しい Anywhere フリートにコンピューティングとして登録します。一意のコンピューティング名を入力し、ワークステーションの IP アドレスを指定します。成功すると、このリクエストはサービス SDK エンドポイントを WebSocket URL の形式で返します。この値をメモしておきます。これは後で必要になります。

  4. ワークステーションコンピューティング用の認証トークンを生成します。この短期間の認証には、トークンと有効期限が含まれます。ゲームサーバーは、これを使用して Amazon GameLift Servers サービスとの通信を認証します。実行中のゲームサーバープロセスがアクセスできるように、認証をワークステーションのコンピューティングに保存します。

Unity プロジェクトに Amazon GameLift Servers サーバーコードを追加する

ゲームサーバーは Amazon GameLift Servers サービスと通信して、指示を受け取り、進行中のステータスを報告します。これを実現するには、Amazon GameLift Servers サーバー SDK を使用するゲームサーバーコードを追加します。

提供されているコード例は、必要とされる基本的な統合要素を示しています。ここでは、MonoBehavior を使用した Amazon GameLift Servers でゲームサーバーのシンプルな初期化について示します。この例では、ゲームサーバーがテスト用に Amazon GameLift Servers Anywhere フリート上で動作していることを前提としています。これには以下のためのコードが含まれています。

  • Amazon GameLift Servers API クライアントを初期化します。このサンプルでは、Anywhere フリートとコンピューティングのサーバーパラメータを含む InitSDK() のバージョンを使用しています。前の テスト用に Amazon GameLift Servers Anywhere フリートをセットアップする のトピックで定義した WebSocket URL、フリート ID、コンピューティング名 (ホスト ID)、および認証トークンを使用します。

  • OnStartGameSessionOnProcessTerminateonHealthCheck など、Amazon GameLift Servers サービスからのリクエストに応答するコールバック関数を実装します。

  • 指定されたポートで ProcessReady() を呼び出して、プロセスがゲームセッションをホストする準備ができたときに Amazon GameLift Servers サービスに通知します。

提供されたサンプルコードは、Amazon GameLift Servers サービスとの通信を確立します。また、Amazon GameLift Servers サービスからのリクエストに応答する一連のコールバック関数も実装されています。各関数とコードの機能について詳しくは、「サーバープロセスの初期化」を参照してください。このコードで使用されている SDK アクションとデータ型について詳細は、「C# サーバー SDK 5.x for Amazon GameLift Servers -- アクション」を参照してください。

サンプルコードは、「Amazon GameLift Servers をゲームサーバーに追加する」で説明されているように、必要な機能を追加する方法を示しています。サーバー SDK アクションの詳細については、「C# サーバー SDK 5.x for Amazon GameLift Servers -- アクション」を参照してください。

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 Servers 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 an Amazon GameLift Servers Anywhere fleet. //They are not required for an Amazon GameLift Servers managed EC2 fleet. ServerParameters serverParameters = new ServerParameters( webSocketUrl, processId, hostId, fleetId, authToken); //InitSDK establishes a local connection with an Amazon GameLift Servers agent //to enable further communication. var initSDKOutcome = GameLiftServerAPI.InitSDK(serverParameters); if (initSDKOutcome.Success) { //Implement callback functions ProcessParameters processParameters = new ProcessParameters( //Implement OnStartGameSession callback (gameSession) => { //Amazon GameLift Servers 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) => { //Amazon GameLift Servers 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 //Amazon GameLift Servers 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 Amazon GameLift Servers it is shutting down. GameLiftServerAPI.ProcessEnding(); }, () => { //Implement callback function OnHealthCheck //Amazon GameLift Servers 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 Amazon GameLift Servers 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 Amazon GameLift Servers where to find game session log files. //At the end of a game session, Amazon GameLift Servers 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 Amazon GameLift Servers 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 Servers 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); } } }

次のステップ

Amazon GameLift Servers でホスティングするために必要な最小限の機能を備えたゲームサーバービルドを準備したので、次のステップを検討してください。