Skip to main content

Fishnet Netcode

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

You can find the sample project on our GitHub. Depending on which version of Fishnet that you wish to use, download the appropriate version folder from the Github, then open the project in Unity.

info

The steps shown in this tutorial remain the same regardless of which project version you use. They are simply available to accommodate for the breaking changes between Fishnet versions.

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

Open the scene located at Assets/SpaceEdge/Scenes/MainMenu. Make sure that Start On Headless is checked on the ServerManager component of the NetworkManager GameObject. 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

In the build settings, 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 files (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 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 ca-certificates && \
update-ca-certificates && \
apt-get clean

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.

Open a command prompt from the DockerBuild folder, then run the following command to build the image.

warning

For ARM CPU (Mac M1, M2, etc.) user, see the dedicated page : ARM CPU

docker build . -t <IMAGE_NAME>:<VERSION_TAG>

Next, push the image to a registry with these commands.

# login, a prompt will ask the password
docker login -u '<REGISTRY_USERNAME>' <REGISTRY_URL>

# add another tag to your image corresponding to the registry
docker image tag <IMAGE_NAME>:<VERSION_TAG> <REGISTRY_URL>/<PROJECT_NAME>/<IMAGE_NAME>:<VERSION_TAG>

#push the image
docker push <REGISTRY_URL>/<PROJECT_NAME>/<IMAGE_NAME>:<VERSION_TAG>

Afterwards, 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) For the port name use UDP_PORT. It’s crucial that you leave the Verification toggle on for our “MatchmakingSystem” to work properly.

img

Configure The Client Build

Back in the Unity project, select the MatchmakingSystem gameObject and change 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".

You may also update the script itself with the same values, located under Assets/SpaceEdge/Scripts/Systems.

img

Make sure that you uncheck the Start On Headless option in the NetworkManager gameObject as well.

If you wish to create a client build, open the Build Settings and change the build type to Windows, Mac, Linux, and the build platform to any platform of your choice. Running the game in the editor will work just as well.

To test the whole system, simply run the client; 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