Mirror WebGL
This guide will help you use Mirror'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 GitHub
Switch the transport
We first need to make some changes to the base scene before we're ready to build the game server.
Open
Scene.unity
located underAssets/Mirror/Examples/Tanks/Scenes
;In the
NetworkManager
gameObject, remove theKcpTransport
script and replace it with theSimpleWebTransport
located underAssets/Mirror/Transports/SimpleWeb
, make sure to also update the Transport field of theNetworkManager
script component with this new transport. Make sure that theAuto 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
.
Build the game server & Containerizing
To facilitate the containerizing and deployment process, it's possible to use the latest version of the Edgegap Unity Plugin on our GitHub to automate the process. For more information on how to use this plugin, you can check our documentation.
If you want instead, you may also follow these step-by-step instructions.
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
toLinux
;Set
Architecture
tox86_64
;Check the
Server Build
option.
Otherwise:
Set
Platform
toDedicated Server
;Set
Target Platform
toLinux
.
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:
Dockerfile
boot.sh
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:
For ARM CPU (Mac M1, M2, etc.) users, see the dedicated page.
Using Linux
Using cmd
Using Powershell
Deploying server to Edgegap
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.
Testing the client
In the editor
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'sHost
url;Uncheck the
Auto Start Server Build
option.
In the
Simple Web Transport
component:Set the
Port
value to the Edgegap deployment'sexternal 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.
On Itch.io
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
toWebGL
;Open up the
Player Settings
. UnderPlayer -> Publishing Settings
, set theCompression Format
toGzip
and make sure theDecompression 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.
Hosting the client on Edgegap
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
Proceed with the same Docker commands as before 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.
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!
If you need more information about the Mirror integration and websocket, you can refer to their documentation.
Last updated
Was this helpful?