Skip to main content

Mirror

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 ourGitHub.

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:

  • Target Platform should be set to Linux;
  • Architecture should be set to x86_64;
  • Server build should be ticked (TRUE);

Then press build and select a new empty folder as the file destination. In this document, this folder will be refered as the [SERVER BUILD] folder.

Contenerizing 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 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 3389/TCP
EXPOSE [GAME PORT]/TCP
EXPOSE [GAME PORT]/UDP
COPY BUILD/ /ROOT/BUILD/
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. 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. You can 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]
  • Dockerfile
  • boot.sh
  • build folder
  • Unity generated files

Start a command prompt in the [SERVER BUILD] folder, and run the following Docker commands:

- DOCKER BUILD . -T GAME-SERVER
- DOCKER LOGIN [URL]
- WHEN PROMPTED : [USERNAME]
- WHEN PROMPTED : [TOKEN]
- DOCKER TAG GAME-SERVER [URL]/[PROJECT]/[YOUR GAME]:[TAG]
- DOCKER PUSH [URL]/[PROJECT]/[YOUR GAME]:[TAG]

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#

The client will need to attempt a connection to the external ports of the Edgegap container that are defined in the Port Mapping of your game server instance. In the example above, the client must have the port value of the Transport component of the NetworkManager set to 31887. This primarily depends on the game you are developing and will most likely be set programmatically in the game’s codebase.

You can set it to the right value in the Unity Editor for a quick test. The connection URL should also be set as the hostname of your game server’s deployed container. This URL can be found in the deployment summary on the dashboard or with the API.

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!