Amazon GameLift
Developer Guide (Version )

Add Amazon GameLift to a Unity Game Server Project

This topic helps you set up the Amazon GameLift C# Server SDK and integrate Amazon GameLift into your Unity game server projects. If you're unsure whether Amazon GameLift supports the operating systems you're using, see For Game Servers.

Set up the C# Server SDK for Unity

Follow these steps to build the Amazon GameLift Server SDK for C# and add it to your Unity game server projects.

To set up the Amazon GameLift Server SDK for Unity

  1. Download the Amazon GameLift Server SDK. To verify that your game system requirements are supported, see Amazon GameLift SDKs. The Server SDK includes the following two solutions:

    • GameLiftServerSDKNet35.sln for .Net framework 3.5

    • GameLiftServerSDKNet45.sln for .Net framework 4.5

    Only the .Net 3.5 solution is compatible with Unity.

  2. Build the C# SDK libraries. See the file for the C# Server SDK for minimum requirements and additional build options. In an IDE, load the .Net 3.5 solution file. Restore the Nuget packages and build the solution to generate the SDK libraries.

  3. Check the API compatibility setting. In the Unity Editor, go to File, Build Settings, Player Settings. In the Other Settings section, under Optimization, make sure the API compatibility level is set to .Net 2.0.

  4. Add the Amazon GameLift libraries to Unity. In the Unity Editor, import the following libraries into the Assets/Plugins directory:

    • EngineIoClientDotNet.dll

    • GameLiftServerSDKNet35.dll

    • log4net.dll

    • Newtonsoft.Json.dll

    • protobuf-net.dll

    • SocketIoClientDotNet.dll

    • System.Threading.Tasks.NET35.dll

    • WebSocket4Net.dll

Add Amazon GameLift Server Code

For more information on adding Amazon GameLift functionality, see these topics:

The following code sample uses a MonoBehavior to illustrate a simple game server initialization with Amazon GameLift.

using UnityEngine; using Aws.GameLift.Server; using System.Collections.Generic; public class GameLiftServerExampleBehavior : MonoBehaviour { //This is an example of a simple integration with GameLift server SDK that makes game server processes go active on Amazon GameLift public void Start() { var listeningPort = 7777; //InitSDK establishes a local connection with Amazon GameLift's agent to enable further communication. var initSDKOutcome = GameLiftServerAPI.InitSDK(); if (initSDKOutcome.Success) { ProcessParameters processParameters = new ProcessParameters( (gameSession) => { //When a game session is created, GameLift sends an activation request to the game server and passes along the game session object containing game properties and other settings. //Here is where a game server should take action based on the game session object. //Once the game server is ready to receive incoming player connections, it should invoke GameLiftServerAPI.ActivateGameSession() GameLiftServerAPI.ActivateGameSession(); }, () => { //OnProcessTerminate callback. GameLift invokes this callback before shutting down an instance hosting this game server. //It gives this game server a chance to save its state, communicate with services, etc., before being shut down. //In this case, we simply tell GameLift we are indeed going to shutdown. GameLiftServerAPI.ProcessEnding(); }, () => { //This is the HealthCheck callback. //GameLift invokes this callback every 60 seconds or so. //Here, a game server might want to check the health of dependencies and such. //Simply return true if healthy, false otherwise. //The game server has 60 seconds to respond with its health status. GameLift will default to 'false' if the game server doesn't respond in time. //In this case, we're always healthy! return true; }, listeningPort, //This game server tells GameLift that it will listen on port 7777 for incoming player connections. new LogParameters(new List<string>() { //Here, the game server tells GameLift what set of files to upload when the game session ends. //GameLift uploads everything specified here for the developers to fetch later. "/local/game/logs/myserver.log" })); //Calling ProcessReady tells GameLift this game server is ready to receive incoming 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.Destroy() when the application quits. This resets the local connection with GameLift's agent. GameLiftServerAPI.Destroy(); } }