Unity NGO
Last updated
Was this helpful?
Last updated
Was this helpful?
This guide will help you create a headless server on Edgegap for a Unity project using as its networking solution.
We will use the open-source sample project 2DSpaceShooter
from as an example.
This guide was written using Unity 2021.3.8f1 LTS
on Windows to produce all Unity builds. The Docker part was made using Linux Mint Cinnamon 20.3.
To begin, copy the sample project. This guide uses the project at this .
Open the project in Unity, open scene Assets/Scenes/network
. Hit play in the editor and choose Host (Server + Client)
to test the game locally.
Dedicated Game Server (DGS) mode is the mode used by Netcode when you call NetworkManager.StartServer()
.
We need to change the sample HUD provided. The idea is to automatically start Netcode server if the build is a Unity server.
Server doesn't need HUD, so in Assets/Scripts/NetworkManagerHud.cs
, enclose function void OnGUI()
with preprocessor directive #if !UNITY_SERVER
and #endif
.
Add a new Start()
method, and a call to m_NetworkManager.StartServer();
inside. Enclose the call with preprocessor directive #if UNITY_SERVER
and #endif
. We will also add a log to show that the server started.
At the end, modifed part of NetworkManagerHud.cs
should look like this:
To allow the server to receive connection from all IP, add the value 0.0.0.0
on the GameObject NetworkManager
in UnityTransport -> Connection Data -> Server Listen Address
.
If you use an older Unity version, add the variable UNITY_SERVER
in Projects Settings -> Player -> Scripting Define Symbols
, and check Headless Mode
and Server Build
.
Otherwise, simply select the Dedicated Server
platform option.
Start the executable. You might have errors and warnings about shaders not being supported, this is not an issue.
Allow Windows defender to let incoming connections from the outside
Start the editor, click on Client
to connect to 127.0.0.1 (localhost)
You should have a spaceship and be able to control it!
If you have another computer, you can build an executable, and connect to the server with its local IP.
Make a build for linux server. Our build executable name will be 2d.x86_64
, inside a Linux_server
folder.
Transfert Linux_server
folder to a Linux system, in a empty folder, along the given Dockerfile.
Start a command prompt in the new folder, then run the folowing Docker commands:
Connect to the dashboard.
Choose your image with your registry and your credential.
You have to open UDP on port 7777. When deploying the application, the port 7777 in your container will be bound to another random host port, accessible publicly. Edgegap API can give you the host IP and port binding for each deployment.
After finished creating your application, click on 🚀 Deploy
button to make your first manual deployment. Let the default settings (random players IP) and deploy.
When the deployment is ready, open the Port Mapping
tab.
We can see that we deployed on 90bcde677d11.pr.edgegap.net
, and the publicly accessible port is 30588
.
Netcode sample HUD can't resolve host IP from hostname, so we will manualy get the server IP.
In our case, IP was 45.76.225.155
.
Start a Unity client, enter IP and port, click on Client
, and your client application is connected to your Edgegap deployment!
Of course, a player won't do all these steps, so let's automate this!
Add this script to the NetworkManager
GameObject. This will provide an in-app GUI to manage your deployments.
Fill application informations in the editor
Feel free to look inside EdgegapSampleHUD.cs
.
The most important thing is to get your deployment server IP and port from Edgegap API, and then put the value automatically in your transport, before starting the client.
Build for Windows server. This build target is available since Unity 2021.2 (see ).
In this part, we will create a docker image containing the dedicated game server. You might also be interested in reading .
For ARM CPU (Mac M1, M2, etc.) users, see the .
For ARM CPU (Mac M1, M2, etc.) users, see the
For ARM CPU (Mac M1, M2, etc.) users, see the .
See if you want to use the Edgegap Registry. You can also use another private registry.
.
Import the script EdgegapSampleHUD.cs
from our .
You can get the complete project sample on our .