Integrating GSDK with Unity

You can find the Unity GSDK here. This folder contains the Game Server SDK for the Unity game engine. The GSDK files are in the Assets/PlayFabSdk/MultiplayerAgent folder. The entire GSDK is usable by class PlayFabMultiplayerAgentAPI.

What do I need to do in order to use the Unity GSDK in a new project?

You can drag the PlayFabSDK folder to your Unity project or you can use the provided unitypackage file. After that, you need to enable the scripting directive ENABLE_PLAYFABSERVER_API on your Build settings, like in this screenshot. This is useful if your game server and client share the same project files.

Usage

The minimum work you need to is call the PlayFabMultiplayerAgentAPI.Start() when your server is initializing and then call the PlayFabMultiplayerAgentAPI.ReadyForPlayers(); when your server is ready for players to connect.

StartCoroutine(ReadyForPlayers());
...
}

IEnumerator ReadyForPlayers()
{
    yield return new WaitForSeconds(.5f);
    PlayFabMultiplayerAgentAPI.ReadyForPlayers();
}

To create your container image to use on Kubernetes, you should build a “Linux Dedicated Server” and then use a Dockerfile similar to the following:

FROM ubuntu:18.04
WORKDIR /game
ADD . .
CMD ["/game/UnityServer.x86_64", "-nographics", "-batchmode", "-logfile"]

Samples

For a more robust sample integrating Unity with the popular Mirror networking library, check the MpsSamples repository here.

Testing with LocalMultiplayerAgent

You can use LocalMultiplayerAgent to test your GSDK integration of your game server before uploading to Thundernetes.

Other GSDK methods and callbacks

There are some other GSDK methods you can use from your game server process:

  • GetGameServerConnectionInfo: Returns the connection information for the GameServer. Usually, it should be the port that you have already defined in your Pod specification. It is required use this method if you want to use the hostNetwork option.
  • GetInitialPlayers: Returns the IDs of the players that are expected to connect to the GameServer when it starts. It is set during the call to the allocation service API.
  • UpdateConnectedPlayers: It updates the currently connected players to the GameServer. On the backend, Thundernetes updates the GameServerDetail Custom Resource with the new number and IDs of connected players.
  • GetConfigSettings: Returns the current configuration settings for the GameServer. You can retrieve the associated GameServerBuild metadata with this method.
  • GetLogsDirectory: Returns the path to the directory for the GameServer logs. It is recommended to just send the logs to standard output/standard error streams, where you can use a Kubernetes-native logging solution to grab them.
  • LogMessage: Writes an entry to the log file. As mentioned, it is recommended to send your logs to standard output/standard error streams
  • GetSharedContentDirectory: Not used in Thundernetes
  • RegisterMaintenanceCallback (name might be slightly different depending on your environment): Used to determine the Health status of the GameServer
  • RegisterMaintenanceCallback (name might be slightly different depending on your environment): Not used in Thundernetes
  • RegisterShutdownCallback (name might be slightly different depending on your environment): Not used in Thundernetes