

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

# Amazon GameLift Servers を Unity プロジェクトに統合する
<a name="integration-engines-unity-using"></a>

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

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

その他のリソース: 
+ [C\$1 サーバー SDK 5.x for Amazon GameLift Servers -- アクション](integration-server-sdk5-csharp-actions.md)
+ [Amazon GameLift Servers開発用ツールを取得する](gamelift-supported.md)

## サーバー SDK for Unity をインストールする
<a name="integration-engines-unity-install"></a>

[GitHub](https://github.com/amazon-gamelift/amazon-gamelift-plugin-unity) から Unity 用のオープンソース Amazon GameLift Servers を取得します。リポジトリの readme ファイルには、前提条件とインストール手順が含まれています。

## テスト用に Amazon GameLift Servers Anywhere フリートをセットアップする
<a name="integration-engines-unity-fleet"></a>

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

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

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

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

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

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

## Unity プロジェクトに Amazon GameLift Servers サーバーコードを追加する
<a name="integration-engines-unity-code"></a>

ゲームサーバーは 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 フリートをセットアップする](#integration-engines-unity-fleet) のトピックで定義した WebSocket URL、フリート ID、コンピューティング名 (ホスト ID)、および認証トークンを使用します。
+ `OnStartGameSession`、`OnProcessTerminate`、`onHealthCheck` など、Amazon GameLift Servers サービスからのリクエストに応答するコールバック関数を実装します。
+ 指定されたポートで ProcessReady() を呼び出して、プロセスがゲームセッションをホストする準備ができたときに Amazon GameLift Servers サービスに通知します。

提供されたサンプルコードは、Amazon GameLift Servers サービスとの通信を確立します。また、Amazon GameLift Servers サービスからのリクエストに応答する一連のコールバック関数も実装されています。各関数とコードの機能について詳しくは、「[サーバープロセスの初期化](https://docs.aws.amazon.com//gameliftservers/latest/developerguide/gamelift-sdk-server-api.html#gamelift-sdk-server-initialize)」を参照してください。このコードで使用されている SDK アクションとデータ型について詳細は、「[C\$1 サーバー SDK 5.x for Amazon GameLift Servers -- アクション](integration-server-sdk5-csharp-actions.md)」を参照してください。

サンプルコードは、[「Amazon GameLift Servers をゲームサーバーに追加する」](https://docs.aws.amazon.com//gameliftservers/latest/developerguide/gamelift-sdk-server-api.html)で説明されているように、必要な機能を追加する方法を示しています。サーバー SDK アクションの詳細については、「[C\$1 サーバー SDK 5.x for Amazon GameLift Servers -- アクション](integration-server-sdk5-csharp-actions.md)」を参照してください。

### 統合サンプルコード
<a name="w2aab9c11c11b9c19c15c13b1"></a>

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

## 次の手順
<a name="integration-engines-unity-additional-resources"></a>

Amazon GameLift Servers でホスティングするために必要な最小限の機能を備えたゲームサーバービルドを準備したので、次のステップを検討してください。
+ テストと開発用の統合ゲームサーバーをデプロイします。Anywhere フリートを使用すると、ローカルマシンをホスティングリソースとして設定し、それを使用してゲームサーバーとゲームクライアントの接続をテストできます。クラウドベースのホスティングの場合は、ゲームサーバーをマネージド EC2 またはマネージドコンテナフリートにデプロイします。ガイダンスについては、以下のトピックを参照してください。
  + [Amazon GameLift Servers Anywhere を使用した反復型開発の準備](integration-dev-iteration.md)
  + [Amazon GameLift Servers Anywhere フリート](fleets-intro-anywhere.md)
  + [Amazon GameLift Serversマネージド EC2 フリート](fleets-intro-managed.md)
  + [Amazon GameLift Servers マネージドコンテナフリート](fleets-intro-containers.md)
+ オプション機能を追加して、ゲームサーバー統合をカスタマイズします。たとえば、一意のプレイヤー IDs でプレイヤーセッションを追加したり、マッチメーキングバックフィルを設定したり、他の AWS リソース (データベースやコンテンツストレージサービスなど) へのゲームサーバーアクセスを管理したりできます。ガイダンスについては、以下のトピックを参照してください。
  + [サーバー SDK を使用して、Amazon GameLift Servers をゲームサーバーに追加します。](gamelift-sdk-server-api.md)
  + [C\$1\$1 (Unreal) サーバー SDK 5.x for Amazon GameLift Servers -- アクション](integration-server-sdk5-unreal-actions.md)
+ ゲームクライアントコンポーネントをカスタマイズして、ゲームセッションをリクエストし、接続情報を受信し、ゲームサーバーに直接接続してゲームをプレイします。ガイダンスについては、以下のトピックを参照してください。
  + [Amazon GameLift Servers ゲームクライアント機能の統合する](gamelift-sdk-client-api.md)