Integrating the PlayFab GSDK into Unity

Multiplayer Servers platform provides a GSDK library that you can integrate into your Unity game server. The library is open source and can be found on the GSDK repository on GitHub.

Installation

You can copy the Assets/PlayFabSdk folder into your Unity project. After that, you need to enable the scripting directive ENABLE_PLAYFABSERVER_API on your Unity Build settings (example). Alternatively, you can use the provided Unity package file. You can find sample code in the MultiplayerServerSample project.

Usage

At a minimum, you need to implement PlayFabMultiplayerAgentAPI.Start() method and start a coroutine for the PlayFabMultiplayerAgentAPI.ReadyForPlayers() method, like in the code below

//...
StartCoroutine(ReadyForPlayers());
//...

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

You would also want to register for when the game server becomes transitions to Active using the PlayFabMultiplayerAgentAPI.OnServerActiveCallback, like in the example below:

PlayFabMultiplayerAgentAPI.OnServerActiveCallback += OnServerActive;
// ...
private void OnServerActive()
{
    Debug.Log("Server Started From Agent Activation");
    // players can now connect to the server
}

Note

For more information regarding game server states, check Basics of a PlayFab game server section here

Moreover, you can implement the following callbacks on your game server:

  • PlayFabMultiplayerAgentAPI.OnMaintenanceCallback triggered when Azure needs to perform maintenance on the VM
  • PlayFabMultiplayerAgentAPI.OnShutDownCallback triggered when a termination notification is received
  • PlayFabMultiplayerAgentAPI.OnAgentErrorCallback triggered if there's any error between game server and PlayFab VM Agent communication

Note

For more GSDK samples, you can take a look at our MPS Samples repository here