Skip to main content

Mirror Netcode

Mirror on Arbitrium

This guide will help you create a headless server on Edgegap for a Unity project using Mirror as its networking solution.

This guide will use the open-source sample project Tanks, which is already available in your Mirror sample, location Assets/Mirror/Examples/Tanks.

The final sample can be found on our GitHub.

Build the game server

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. Transfert linux_server folder to a second empty folder, which will be refered as the [SERVER BUILD] folder in this document.

Containerizing the dedicated game server

We will create a docker image containing the dedicated game server in this part. You might also be interested in reading Unity Server in Docker.

If you need more informations about Docker with Edgegap, please refer to this documentation.

Dockerfile

FROM ubuntu:bionic
MAINTAINER <author_detail>

ARG debian_frontend=noninteractive
ARG docker_version=17.06.0-ce

RUN apt-get update && \
apt-get install -y libglu1 xvfb libxcursor1 ca-certificates && \
apt-get clean && \
update-ca-certificates

EXPOSE 3389/TCP
EXPOSE [GAME PORT]/TCP
EXPOSE [GAME PORT]/UDP

COPY linux_server/ /root/linux_server/
COPY boot.sh /boot.sh

WORKDIR /root/
ENTRYPOINT ["/bin/bash", "/boot.sh"]

Take note of the port you use for network communications, referred as the [GAME PORT]. By default, the port used is 7777. You can find this information in the Unity Editor, on the NetworkManager game object, in the Transport component.

  • Copy the above lines and paste them in your Dockerfile, placed inside [SERVER BUILD]. Modify the [GAME PORT] placeholders to your game port.

Having the [GAME PORT] opened on both TCP and UDP allow you to use any transport you prefer in the NetworkManager Mirror component. Finally, create a file named boot.sh at the root of the [SERVER BUILD] folder. It will be executed when starting the image in a container.

  • Copy the following two lines, make sure to replace the [YOUR GAME] placeholders with the name of the generated file.
xvfb-run --auto-servernum --server-args='-screen 0 640X480X24:32' /root/build/[YOUR GAME].x86_64 -batchmode -nographics

At this point, you should have the following hierarchy:

  • [SERVER BUILD] folder
    • Dockerfile
    • boot.sh
    • linux_server folder
      • Unity generated files
  • Start a command prompt in the [SERVER BUILD] folder, and run the following Docker commands:
warning

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

# build the image
docker build . -t <IMAGE_NAME>:<IMAGE_VERSION>

# 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>:<IMAGE_VERSION> <REGISTRY_URL>/<PROJECT_NAME>/<IMAGE_NAME>:<IMAGE_VERSION>

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

After these commands, you should be able to see your uploaded image on the Edgegap website 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 website. 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 :
        • [GAME PORT] - TCP/UDP - disable Verifications
        • 3389 - TCP - disable Verifications

Once your application has been created, you can press the Deploy button to proceed with deploying your game server. Choose the region you want to deploy in, and enter the number of random players you want to generate depending on your game. Check that everything is running smoothly by verifying the following:

  • Latest Status should be set to Ready.
  • In the Port Mapping tab, you should be seeing the port you set in the application creation form:

img

Add sample HUD in your client application

  • Set the Port value of the Transport component of the NetworkManager to the external port defined in the Port Mapping tab of your deployment.

In this example, the port was set to 31887. This primarily depends on the game you are developing and will most likely be set programmatically in the game’s codebase.

  • Set the value of Network Address of the Network Manager to your deployment's Host. This URL can be found in the Deployment Summary on the dashboard or with the API.

In this example, the address was set to 0ace560706a5.pr.edgegap.net. Again, this value will most likely be set programmatically during the client’s communication with the master server/API responsible for the matchmaking process.

With the correct information, you should be able to connect to the game server normally and be able to start playing right away.

img

You now have a Mirror project available to deploy on demand!