Skip to main content

Fishnet

Unity Networking Evolved on Arbitrium#

This guide will help you create a headless server on Edgegap for a Unity project using Fishnet as its networking solution.

The core of this sample is the MatchmakingSystem script. This script communicates with the Edgegap API to find live deployments of the available server for the game. If no available servers are found, it requests the Edgegap API to deploy a new instance of the game’s server for the client based on their location. This script explains the core concepts of communicating with the Edgegap API using the Unity HTTP Requests system and utilizing the data sent by the Edgegap API for making decisions for the game’s matchmaking.

In this sample, the MatchmakingSystem will try to find an ongoing game for our player and if no games are available, it will automatically deploy a new game server.

note

This project uses a few free assets from the Unity Asset Store. At the end of this document there are links to all these assets. This Project is tested on Unity Version 2021 LTS.

Get the base project#

To begin, copy the sample project. You can find it on our GitHub.

Adapt your game to support Dedicated Game Server (DGS) mode#

Make sure that Start On Headless is checked on the ServerManager component of FishNet. This ensures that the network manager auto start server when the build mode is set to Headless.

img

Now add a port number on the default transport component (Tugboat). Note this port number as later we will have to expose the same port in our Dockerfile and also use the same port while configuring the app on Edgegap.

img

Check that the DefaultScenes component have Offline and Online scene set up properly, so the SceneManager can auto load the proper scene based on the Network Mode (Server or Client)

img

Switch the build type to Dedicated Server and the target platform to linux.

note

If you are building the game with “IL2CPP” scripting backend then you would need to install sysroot toolchain from here - Unity IL2CPP Build Support for Linux | Sysroot Base | 2.0.3

Click on the Build button, when prompted, enter the name ServerBuild as the build name. Once the build is complete you will have 2 (ServerBuild.x86_64 and UnityPlayer.so) and 1 folder (ServerBuild_Data). Move both the files and the folder inside a new folder and name the new folder as build.

Docker Build#

Create a new folder named DockerBuild and move the build folder inside it. Create a new file inside the DockerBuild and name this file as Dockerfile and make sure the file has no extension, open the file using a text editor that allows changing the Encoding Type and Line Ending of the file (Notepad++, BBEdit, etc). Once the Dockerfile is created, open it using the text editor and paste the below text in it.

FROM ubuntu
MAINTAINER Edgegap <youremail@edgegap.com>
ARG DEBIAN_FRONTEND=noninteractive
ARG DOCKER_VERSION=17.06.0-ce
RUN apt-get update && \
apt-get install -y libglu1 xvfb libxcursor1
EXPOSE 7770/udp
COPY build/ /root/build/
COPY boot.sh /boot.sh
WORKDIR /root/
ENTRYPOINT ["/bin/bash", "/boot.sh"]

Take a note of the line #10 EXPOSE 7770/udp this is the port number and protocol type we are going to configure in the Edgegap console later on. Make sure the file Encoding is set to UTF-8 and the Line Ending is set to Line Feed (LF). Now save the file without any extension (.txt, .rtf, etc)

Now create one more file in the DockerBuild folder and name it boot.sh. Open using the text editor and paste the below text in it.

#!/bin/bash
chmod +x /root/build/ServerBuild.x86_64
xvfb-run /root/build/ServerBuild.x86_64

Again make sure the Line Endings and Encoding is correct (LF and UTF-8 respectively). Save the file with the extension .sh.

After these commands, you should be able to see your uploaded image on the dashboard if you are using the Edgegap Container Registry. See this doc if you want to use the Edgegap registry. You can also use another private registry.

Deploying to Edgegap#

Navigate to the Applications & Games page of the dashboard. Click on the Create New button in the top right hand corner to access the application form. Here are the fields and how to fill them properly :

  • Application name : Can be any notable name you want to use to easily recognize your application among others.
  • Image : Can be any specific image you want to use to easily recognize your application among others.
  • Version name : You may want to use a version name to describe the scope of the version you are deploying. Examples may be “demo”, “production”, “v1”, “v2”.
  • Container :
    • Registry : “[URL]”, where [URL] is the value from the credentials you can display on the Container Repository page.
    • Image repository : “[PROJECT]/[YOUR GAME]”, where [PROJECT] and [YOUR GAME] are the values you used earlier when pushing the docker image.
    • Tag : “[TAG]”, where [TAG] is the value you used earlier when pushing the docker image.
    • Tick “Using a private repository”
    • Private registry username : “[USERNAME]”, where [USERNAME] is the value from your credentials.
    • Private registry token : “[TOKEN]”, where [TOKEN] is the value from your credentials.
    • Requirements : Left as is.

For the port number itself, use 7770 (remember this is the port we exposed in our dockerfile using the EXPOSE 7770/udp command) For the port protocol use UDP (The default FishNet transport tugboat uses only UDP so select only UDP and not TCP/UDP) And for the port name use UDP_PORT It’s crucial that you leave the Verification toggle on for our “MatchmakingSystem” to work properly.

Configure The Client Build#

In unity project navigate to Assets>SpaceEdge>Scripts>Systems Open the MatchmakingSystem.cs script and the following fields

img

AppName= “Set it to the “Application name” you used while setting up the Edgegap application”. AppVersion= “Set it to the “Version name” you used while setting up the Edgegap application”. AuthHeaderValue=”Set it to your Edgegap API token that you can get from the dashboard”.

note

Remove the "token" word from the API token because we are setting this as the Authentication Header schema during the "Awake()" method of the "MatchmakingSystem".

For the client build you can change the build type to Windows, Mac, Linux and build platform to any platform of your choice.

To test the whole system, you can run the client build (or use the editor play mode). Enter your player name (optional) and click on the start game button. The MatchmakingSystem should now search for any live deployments to connect you, and if no live deployments are found, it will request Edgegap to deploy a new instance of your game’s server and connect you to that instance once it’s ready.

img

You now have a Fishnet project available to deploy on demand!

Free Assets Used#

  • Cartoon FX Remaster Free | VFX Particles | Unity Asset Store
  • Sci-Fi Sfx | Audio Sound FX | Unity Asset Store
  • Starfield Skybox | 2D Sky | Unity Asset Store
  • Star Sparrow Modular Spaceship | 3D Space | Unity Asset Store
  • Total JSON | Input Management | Unity Asset Store