Mirror WebGL
Last updated
Was this helpful?
Last updated
Was this helpful?
This guide will help you use 's Websocket Transport and create a headless server on Edgegap for a Unity project.
This guide will use the open-source sample project Tanks
, available in the Mirror sample under Assets/Mirror/Examples/Tanks
.
You can find the final version of this sample on our
We first need to make some changes to the base scene before we're ready to build the game server.
Open Scene.unity
located under Assets/Mirror/Examples/Tanks/Scenes
;
In the NetworkManager
gameObject, remove the KcpTransport
script and replace it with the SimpleWebTransport
located under Assets/Mirror/Transports/SimpleWeb
, make sure to also update the Transport field of the NetworkManager
script component with this new transport. Make sure that the Auto Start Server Build
option is selected as well.
Depending on your version of Mirror, you might need to change/update the NetworkManagerHUD
to make it work with different transports than KCP, as well as to let you input the port value in the HUD before joining a server.
Take note of the port used for network communications, referred as the [GAME PORT]
. In this case, the port used is 7778
.
Once ready with your game, head to the Build
screen of the Unity Editor, under File -> Build Settings
in the top menus. Make sure to select the right presets depending on your version of Unity.
Prior to version 2021.2:
Set Target Platform
to Linux
;
Set Architecture
to x86_64
;
Check the Server Build
option.
Otherwise:
Set Platform
to Dedicated Server
;
Set Target Platform
to Linux
.
Then press build and select a new empty folder named linux_server
as the file destination. Transfer the linux_server
folder to a second empty folder, which will be refered as the [SERVER BUILD]
folder in this document. Add the following Dockerfile
and boot.sh
file to the [SERVER BUILD]
folder:
Make sure to replace the [YOUR GAME]
placeholders with the name of the generated file
Then, start a command prompt in the [SERVER BUILD]
folder; Run the following Docker commands to create an image of your build and push it to a private registry:
After logging in on the Edgegap Dashboard, navigate to the Applications & Games
page. 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.
Ports : Click the + Add port
link to add a new port, and add the following entries :
7778
- WS - enable TLS Upgrade (Beta)
If you use the WSS option, it is important to enable the TLS Upgrade
option. Otherwise, you will encounter errors similar to this one in your container logs on the Edgegap dashboard.
Once your application has been created, you can press the Deploy
button to proceed with deploying your game server. Once the latest status of you deployment is set to Ready
, you will be able to connect to the server with a client version of the game. Take note of the Host
url and, in the Port Mapping tab of your deployment, the external port
that's publicly available.
Back in the Unity editor in the tank scene, select the NetworkManager
gameObject and change the following settings:
In the Network Manager
component:
Set the Network Address
to the the server deployment's Host
url;
Uncheck the Auto Start Server Build
option.
In the Simple Web Transport
component:
Set the Port
value to the Edgegap deployment's external port
e.g.: 32821
;
Make sure the Client Use WSS
option is enabled.
Once this is done, click Play
in the editor, then click the Client
button; You'll get connected to the server and be able to play the game after a short moment.
To put your game client on Itch, you will need to make a client build; You will need to install the WebGL Build Support
module for your version of Unity to do that. Once that's done, head back to the Build
screen of the Unity Editor, under File -> Build Settings
, and select the following options:
Set Platform
to WebGL
;
Open up the Player Settings
. Under Player -> Publishing Settings
, set the Compression Format
to Gzip
and make sure the Decompression Fallback
option is selected. Save these settings, and close the window.
Then press build and select a new empty folder named build
as the file destination. Transfer the build
folder to a second empty folder, which will be refered as the [CLIENT BUILD]
folder in this document.
Once your game has finished building, compress the files of this build
folder into a zip folder, making sure that they are at the root of it. Then you simply need to upload the zip folder to your Itch project, and make sure to select the option for the file to play in the browser. Once you launch the game, you just need to make sure that the network address and port values are set correctly in the HUD, then click the Client
button to play.
It's even possible to host your game client on Edgegap! To do so, add the following Dockerfile
and nginx.conf
file to the [CLIENT BUILD]
folder:
Dockerfile
nginx.conf
Then, create a new application for your client on the Edgegap dashboard with the following settings:
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.
Ports : Click the + Add port
link to add a new port, and add the following entries :
80
- HTTPS
Once your application has been created, you can press the Deploy
button to proceed with deploying your game client. With both the server and client deployments set to Ready
, open the game client's Host
url at the specified external port
in your browser, and you'll be able to play the game after setting the correct values in the game's HUD!
For ARM CPU (Mac M1, M2, etc.) users, see the .
Proceed with the to build and push an image of your game client to a private repository, but from a command window opened in the [CLIENT BUILD]
folder. Make sure to use a different image name than the one for your server.
If you need more information about the Mirror integration and websocket, you can refer to their .