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.
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. Open the project in Unity.
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
.
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.
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)
In the build settings, switch the build type to Dedicated Server
and the target platform to linux.
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
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.
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.
- Linux
- cmd
- Powershell
# 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>
# 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>
# 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 transporttugboat
uses only UDP so select only UDP and not TCP/UDP) For the port name useUDP_PORT
. It’s crucial that you leave theVerification
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 change the following fields:
- 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.
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. Make sure that you uncheck Start On Headless
beforehand.
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.
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