PlayFab’s Integration with Mirror Unity

Angad Lamba
7 min readJan 18, 2021

This is a detail step by step explanation of how to integrate PlayFab’s multiplayer hosting service with Mirror in Unity. The goal is to provide a one-stop reference to my future self and for anyone else whom this might help.

Step 1: Install PlayFab SDK

Download and Import the PlayFab Unity Editor Extensions Asset Package into the Unity Editor. (From: PlayFab document source)

Fill your PlayFab developer account information in extension and Log In.

Install SDK and Set your Title Settings, Studio entry and Title ID.

You are now ready for Step-2.

Step 2: Install GSDK

Download Game Serve SDK (GSDK) from here : github download link

Once added to your Unity project, you need to enable the scripting directive ENABLE_PLAYFABSERVER_API on your build settings, like this:

This should remove any error from the console.

Step 3: Integrating GSDK with Mirror

Create a new script, name it Configuration.cs and add the following code to it:

Create a new script, name it NetworkMessages.cs and add the following code to it:

Create another script, name it UnityNetworkConnection.cs and add the following code to it:

And final two scripts are ServerStartup.cs and ClientStartup.cs, as the name suggests on will only runs on the server and the other only runs on the client. (Note: If you have a separate projects for server and client then you only need to put Server Startup script in your server project and Client Startup script in your client project.)

If your client and server are in the same scene then do the scene hierarchy as shown:

Put Configuration.cs on Configuration gameobject, ServerStartup.cs on ServerStartup gameobject and ClientStartup.cs on ClientStartup gameobject.

(Note: if you have separate scenes or projects for client and server make appropriate changes but you would need Configuration gameobject for both the client and server so create Configuration gameobject in both scenes or both projects.)

Also code for ServerStartup.cs and ClientStartup.cs :

Note that Room is a singleton reference to your NetworkManager; replace its type with your version of NetworkManager. For example:

protected MyNetworkManager _room;protected MyNetworkManager Room {     get {        if (_room != null) { return _room; }         return _room = NetworkManager.singleton as MyNetworkManager;     }}

Also, In ClientStartup.cs there is a List of Regions in RequestMutliplayerServer function, in this example this is EastUs, based on where you choose to host your servers you will list all your regions here.

So what are we doing here:

In short, we are starting the server on the PlayFab hosting service and registering for certain callbacks that the PlayFab Agent provide like Server Shutdown, Maintenance etc. and performing certain task on client and server when those callbacks trigger like disconnecting all players when server shutdown, etc.

You can read more about it in detail here : PlayFab’s documentation link

Finally add these code snippets in your NetworkManager script:

You will need to get the PlayFabAuthService.cs script from here: github link for the script

Now your ready for deploying your game on PlayFab server.

Before going to next step here are some other helpful links you might wanna checkout if your facing some issues or want to see more examples:

  1. GSDK Samples
  2. Natepac’s Playfab mirror game example
  3. PlayFab Multiplayer Server Documentation

Step 4: Deployment

To deploy go to your PlayFab Dashboard at developer.playfab.com, select a project or create a new one.

You will need to enable your client to start games. When you ship your game you’ll disable this, but for now this is just an easy way to see something working.

  1. Go to your project on the dashboard
  2. Click on the Settings gear

3. In Settings, click on the API Features tab and enable “Allow Client to Start Games”

4. In Configuration gameobject set Build type to “REMOTE_SERVER”, build your project for windows or linux.

5. For Windows Build:

  • Select all the FILES generated from build (not the folder) and zip them up. We will be uploading the zip to PlayFab.
  • Go to your PlayFab Dashboard and click on the Multiplayer tab on the left. Click New Build button(if its on showing up you need to enter your credit card number)
  • Fill out the configuration as shown:
  • Also, instead of TCP you need to set UDP for your port if your are using UDP Transport protocol in your Mirror project.(Check mirror documentation for that, in the scripts above I have used KcpTransport which is UDP)
  • Region you are selecting here also needs to be included in the ClientStartup.cs script in RequestMultiplayerServer function as mentioned above.

6. For Linux Build:

  • You need to create a docker container and publish to the Azure Docker Registry. So you need to have docker installed on your device.
  • In your Build folder add a docker file and add these lines to it :
FROM ubuntu:18.04WORKDIR /MyBuildFolderADD . .CMD ["/MyBuildFolder/MyGame.x86_64", "-nographics", "-batchmode"]
  • Replace MyBuildFolder with your Build Folder name and MyGame.x89_64 Linux executable file name with your Linux executable filename.
  • Open Command Prompt in your Build Folder and write following commands one by one:
docker build -t myregistry.io/mygame:0.1 .

You can name it anything you want and with whatever version number(here 0.1)

docker login --username customer5555555 --password HRDFOdIebJkvBAS+usa55555555 customer5555555.azurecr.io

You can find this command and actual values for your username, password and Azure Registry location from your PlayFab’s Dashboard Multiplayer Tab and when your select Platform Linux instead of Windows, it will give you your version of this command.(You can directly copy it from there and run it in the command prompt)

docker tag myregistry.io/mygame:0.1 customer5555555.azurecr.io/pvp_gameserver:v1docker push customer5555555.azurecr.io/pvp_gameserver:v1

Replace the placeholder with your values. This will create a docker container and publish it to the Azure Cloud Registry.

Now you will be able to see your Linux Container as an option in the drop-down in your Multiplayer Dashboard.

Now Press Save and wait for the server to be deployed(This can take a few minutes)

Step 5: Test Client with Deployed Server

  1. In your Configuration gameobject change Build Type to “REMOTE_CLIENT” or “LOCAL_CLIENT”.
  2. Select the Configuration gameobject and make sure the IP and Port are blank (they should be by default). However, we do need the BuildId of your server that you uploaded and deployed to PlayFab. Select the server build in the PlayFab Multiplayer Dashboard. It looks like this:

3. In your scene add a button on the UI and make it run this line of code(This will only run if you can included the PlayFabAuthService script from the link mentioned above):

PlayFab.Helpers.PlayFabAuthService.Instance.Authenticate (PlayFab.Helpers.Authtypes.Silent);

4. Hit play in the editor and click on the button. If everything was setup correctly this to log the IP and Port of your hosted Server in the console.

5. Exit Play mode and copy the IP and Port and paste it in your Configuration gameobject in the hierarchy.

You have successfully connected to your hosted server and now can start experimenting further with Playfab.

Also you should check on how to locally setup environment similar to your hosted server so that you can test your games more quickly on your local device instead of deploy each time. Check these links depending on if your build is for Windows or Linux:

Windows :
https://github.com/PlayFab/gsdkSamples/blob/master/UnityMirror/README.md

Linux:
https://github.com/PlayFab/LocalMultiplayerAgent/blob/master/linuxContainersOnWindows.md

Note: Local testing is not available if you have Windows 10 Home or Basic operating system.

Also, I recommend checking this out as well to get a more better understanding if you have any doubts or problems (https://github.com/natepac/playfabmirrorgameexample)

If you find any problems or think I missed something or should add anything else let me know in the comments below or you can pm me on twitter: angadlamba

Hope this was helpful to you and have a good day.

--

--

Angad Lamba

Co-Founder GodLabs | Game Developer | Learning Art | Made CCG: Cricket Card Game