Deploying Unity Game Server Into Playfab Cloud

Microsoft Azure PlayFab service is a great way to host your multiplayer game and manage player data. With Azure PlayFab, game developers can leverage cloud-based infrastructure and services to scale their games and provide an optimal experience for players. In this guide, I will take you through the steps involved in deploying your Unity Linux game server on the PlayFab service, all at no cost.

Prerequisites:

  • Supports both Mirror and Fishnet
  • Server is set up in a Linux container.
  • Dev machine: Windows 10/11
  • Needs a PlayFab Account with payments setup (you wont be charged)

Initial setup:

Machine and Project Setup: (Windows 10/11)

Network Manager Setup for Mirror:

  • Create a new custom network manager as a inherited child class of mirrors network manager and add the KCP transport component into the same gameobject, which inturn uses UDP protocol

  • Add a simple switch in the custom network manager like in the below image and set up all other necessary events like OnClientJoined etc in the script.

image alt text

  • Add server and client listener scripts as child of the network manager gameobject

Network Manager Setup for Fishnet:

  • Create a new custom network manager script, similar to the above mirror one.

  • Add fishnet’s network manager as the child of the above gameobject along with it’s tugboat transport component

image alt text image alt text image alt text

  • Add server and client listener scripts for playfab as child of custom network manager gameobject

Build and Deploy:

Building the headless server:

  • Switch to Dedicated-Server Build mode and Select Linux as Target Platform image alt text
  • In Custom networkManager, switch Build type to REMOTE_SERVER (which in turn enables the agent-listener script) image alt text
  • Build the game and once build complete, create docker file in the build’s parent directory Create a Dockerfile - PlayFab | Microsoft Docs Directory and file should look something like the below image and the parent directory shouldn’t have any other files/directories except the dockerfile and game directory (Note: Exclude the “ServerX_BurstDebugInformation_DoNotShip” Directory before creating the docker image of build folder.)

image alt text image alt text

Creating and Deploying the Docker Image:

  • Copy the Docker Creds from PlayFab developer account image alt text image alt text
  • In your windows machine, open the ubuntu terminal (not windows cmd),
  • move to the directory which contains the previously created Dockerfile,
    cd mnt/c/Code/Test/battle-axes/Builds/server5/
    
  • run the login command
    docker login -u <username_here> -p <password_here> <url_here>*
    
  • run the build docker image command
    docker build -t <url_here>/battleaxes_server:v1 .
    

Note: there is a . in the end of above command which means all files in the directory and increment v1 (vX) based on previously uploaded version)*

  • Now before pushing this image to the cloud, test it locally by following these steps.
  • run the push docker build to cloud command
    docker push <url_here>/battleaxes_server:v1
    

Testing the headless server:

Testing locally using playfab’s MpsLocalAgent:

The file should look something like this in the end:

image alt text

  • Now open windows terminal in the downloaded directory and run command:
    .\LocalMultiplayerAgent.exe -lcow
    

Note: lcow stands for Linux Containers On Windows

  • Once the local server goes to “active”, connect a local client with ip: 127.0.0.1 and port 56100

Using SSH into running cloud server:

  • Click on “Connect to Virtual Machine” in builds of playfab developer console
  • Open your windows terminal and Run the ssh command
    ssh <id_here>@<ip_here> -p <port_here>
    
  • Run the Docker ps to get current running container info
    sudo docker ps -a
    
  • Get current server logs by cmd:
    sudo docker logs <docker_container_id>
    

Testing locally using ParrelSync and three Unity editor instances:

Common issues:

  • If you get an error called: “You must set PlayFabSettings.TitleId before making API Calls.” you can get the title id from playfab developer console.

image alt text

  • If you get this error “network playfab not found”image alt text Open “SetupLinuxContainersOnWindows.ps1” in LocalMultiplayerAgentPublish you just downloaded and run this “docker network create playfab –driver bridge” in your linux distribution .

Connecting to our playfab game server as client:

Client Build Setup:

  • Switch back to whatever target platform you were previously in from the dedicated server platform.
  • In Custom networkManager, switch Build type to PLAYFAB_CLIENT (which in turn enables the client-startup script)

image alt text

  • In the playfab dashboard, click on the “request server”, when you have standby servers, and then Get the ip address, port and build id from the playfab console on the website.

image alt textimage alt text

  • Make sure you have the right build-id, ipAddress, port **and **region set in the custom network manager or the network manager script and save scene for all clients

image alt text

  • Now with server already running, Click on Start game from clients in UnityEditor, and this should connect them all together.

image alt text

  • Now open a second client and click on “Join Game” button, enter the configuration you received above

image alt text

Multiple instances:

Inspecting multiple instances in same VM:

image alt text

Leave a comment