Integrate Amazon GameLift with a Unity game server project - Amazon GameLift
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Integrate Amazon GameLift with a Unity game server project

Note

This topic provides information for an earlier version of the Amazon GameLift plugin for Unity. Version 1.0.0 (released in 2021) uses the Amazon GameLift server SDK 4.x or earlier. For documentation on the latest version of the plugin, which uses server SDK 5.x and supports Amazon GameLift Anywhere, see .

This topic helps you prepare your custom game server for hosting on Amazon GameLift. The game server must be able to notify Amazon GameLift about its status, to start and stop game sessions when prompted, and to perform other tasks. For more information, see  Add Amazon GameLift to your game server.

Prerequisites

Before integrating your game server, complete the following tasks:

Set up a new server process

Set up communication with Amazon GameLift and report that the server process is ready to host a game session.

  1. Initialize the server SDK by calling InitSDK().

  2. To prepare the server to accept a game session, call ProcessReady() with the connection port and game session location details. Include the names of callback functions that Amazon GameLift service invokes, such as OnGameSession(), OnGameSessionUpdate(), OnProcessTerminate(), OnHealthCheck(). Amazon GameLift might take a few minutes to provide a callback.

  3. Amazon GameLift updates the status of the server process to ACTIVE.

  4. Amazon GameLift calls onHealthCheck periodically.

The following code example shows how to set up a simple server process with Amazon GameLift.

//initSDK var initSDKOutcome = GameLiftServerAPI.InitSDK(); //processReady // Set parameters and call ProcessReady var processParams = new ProcessParameters( this.OnGameSession, this.OnProcessTerminate, this.OnHealthCheck, this.OnGameSessionUpdate, port, // Examples of log and error files written by the game server new LogParameters(new List<string>() { "C:\\game\\logs", "C:\\game\\error" }) ); var processReadyOutcome = GameLiftServerAPI.ProcessReady(processParams); // Implement callback functions void OnGameSession(GameSession gameSession) { // game-specific tasks when starting a new game session, such as loading map // When ready to receive players var activateGameSessionOutcome = GameLiftServerAPI.ActivateGameSession(); } void OnProcessTerminate() { // game-specific tasks required to gracefully shut down a game session, // such as notifying players, preserving game state data, and other cleanup var ProcessEndingOutcome = GameLiftServerAPI.ProcessEnding(); } bool OnHealthCheck() { bool isHealthy; // complete health evaluation within 60 seconds and set health return isHealthy; }

Start a game session

After game initialization is complete, you can start a game session.

  1. Implement the callback function onStartGameSession. Amazon GameLift invokes this method to start a new game session on the server process and receive player connections.

  2. To activate a game session, call ActivateGameSession(). For more information about the SDK, see Amazon GameLift server SDK (C#) reference: Actions.

The following code example illustrates how to start a game session with Amazon GameLift.

void OnStartGameSession(GameSession gameSession) { // game-specific tasks when starting a new game session, such as loading map ... // When ready to receive players var activateGameSessionOutcome = GameLiftServerAPI.ActivateGameSession(); }

End a game session

Notify Amazon GameLift when a game session is ending. As a best practice, shut down server processes after game sessions complete to recycle and refresh hosting resources.

  1. Set up a function named onProcessTerminate to receive requests from Amazon GameLift and call ProcessEnding().

  2. The process status changes to TERMINATED.

The following example describes how to end a process for a game session.

var processEndingOutcome = GameLiftServerAPI.ProcessEnding(); if (processReadyOutcome.Success) Environment.Exit(0); // otherwise, exit with error code Environment.Exit(errorCode);

Create server build and upload to Amazon GameLift

After you integrate your game server with Amazon GameLift, upload the build files to a fleet so that Amazon GameLift can deploy it for game hosting. For more information on how to upload your server to Amazon GameLift, see Upload a custom server build to Amazon GameLift.