Host the network library Mirror for Unity on a Linux server

Introduction

Mirror is a well-known network library for Unity. I also use it for multiplayer VR apps on my local network, but I can also host it on a remote server. It may seem difficult at first glance, but I could make it even if I had little knowledge of networks, so maybe everyone is okay.

Let's introduce Mirror.

The recommended Unity version is 2018.3.6. The author strongly recommends using the LTS version, but it will work with Unity 2019.3 etc. After downloading and importing the Mirror assets (free), open the Scene in the Assets / Mirror / Examples / Pong / Scenes folder. It's a so-called Pong game scene.

image.png

You can join the game by pressing Client in the upper left. However, the game does not start because it is not currently networked.

Prepare a server-side build

Therefore, we will prepare the server. "Server Build" has been added from Unity 2018.3, and it is possible to operate in Headless Mode that does not display images. Place this on the server and let the client communicate.

In Mirror, if you check "Start on Headless" of the Network Manager component as shown below, StartServer () will be automatically executed on the Server Build application to start the server.

image.png

Now, following the official recommendations, we'll take the method of running the server on Linux and the client on Windows. To do that, you first need to build for Linux. If you did not include Linux in your target when you installed Unity, add the module from Unity Hub.

image.png

After that, set Linux to Target Platform as shown below, and check "Server Build" to build.

image.png

The generated files are as follows. If you copy these to Linux by the method shown in the section below and execute it, it will work on the server.

image.png

While the server side application is running on Linux, play Unity Editor on the Windows side, enter the IP address of the server (localhost on the same machine) and press the Client button on the upper left of the screen, as shown below Enter the game as a player.

image.png

You can play Ping by preparing another Client and connecting to the server in the same way.

Preparing the test host

Learn how to prepare a server for your host. The explanation of the introduction method by the author is here. According to the author, Azure, Amazon, and Google instances have troublesome firewall settings, so you should avoid them for the time being. The author is working on VPS hosting for a company called namecheap. I want you to use Linux (Ubuntu) as the OS.

Although namecheap is cheap to set up a server, it's still not free, so I'll think of a way to prepare it in a local environment for practice.

Try using Windows Subsystem for Linux (WSL)

Therefore, we will utilize Windows Subsystem for Linux (WSL), which can create a Linux instance on Windows 10. You can enable WSL, download Ubuntu, etc. by the method of this article. Ubuntu to be introduced is 18.04LTS which is also used in namecheap etc.

image.png

When you start Ubuntu, you will be asked to enter the UNIX Username and password after the installation process. Decide the user name and password and enter them. This will bring you to the command prompt.

However, since the user does not have root privileges as it is, set the root password with this method and root Please log in with.

From here on, follow the "Installing our requirements" in How to install by author. First,

apt update
apt -y dist-upgrade
apt -y install screen tmux sudo zip unzip

After this, the work to add a user is written, but that is unnecessary (because it has already been added above). Please end the work as root with ʻexit`.

After this, in the explanation of the introduction method by the author, the story of file transfer using WinSCP will come out, but if you want to exchange files locally, enter \\ wsl $ in the path in Explorer, Ubuntu-18.04 You can see the folder of.

image.png

The user's directory will be \\ wsl $ \ Ubuntu-18.04 \ home \ username.

Here, you can build Linux as a target with Unity and copy the resulting files directly. Here, the app name is Server.x86_64.

image.png

Next, as described in the installation method by the author, execute the following command to start the server.

sudo chmod +x ./Server.x*
./Server.x86_64

Hopefully you'll see a screen like this.

image.png

Press Ctrl + C to quit.

Once you're ready, connect the client as you did above. If you run the client on the same machine as WSL, the address is localhost (or 127.0.0.1), if you use another machine on the same local network, ifconfig will use the IP address of the machine running WSL (not 127.0.0.1). Please check and use. If that doesn't work, close Ubuntu and try again.

Rent and use a remote server

Once you're confident with WSL, rent a remote server. Let's use namecheap introduced above. The cheapest VPS Hosting course seems to be $ 11.88 per month.

image.png

After applying, you will receive your account information and other information within a few hours. Information about SSH connections is especially important.

image.png

From here, you can follow the procedure of Configuring the Linux Server (one time setup) in Explanation of installation method by the author. I have Ubuntu on WSL, so I can use the method for Linux. In other words

ssh root@your-server-ip

Please connect with and enter the root password (yes is fine if a warning appears on the way). If all goes well, you should be able to log in to ubuntu.

image.png

After that, follow the instructions of Installing our requirements in Explanation of installation method by the author and execute the following instructions.

apt update
apt -y dist-upgrade
apt -y install screen tmux sudo zip unzip

Then add a non-root user.

adduser your-user-name

For your-user-name, enter the user name of your choice. Make a note of this user name. Then, you will be asked to enter information such as a password as shown below. For Full Name etc., press the Enter key to enter the default value. image.png You have now created a user. To give more root privileges

adduser your-user-name sudo

Please also execute. Once this is done, you're done with root, so enter logout.

Now, let's log in as the created user.

ssh your-user-name@your-server-ip

your-user-name is the name of the user you just created, and your-server-ip is the IP address of the remote server. When you run it, you will be asked for a password, so you can log in by entering the one you set earlier (not the root).


Now, let's upload the built file next, but since Explorer cannot be used, Explanation of installation method by the author Copying our built game to the Use WinSCP recommended by the server. Please install from here. When you start the app, you will be asked to set up the connection as shown below.

image.png

The transfer protocol is SFTP, enter the IP address of the remote server in the host name, the port number is 22, and the user name and password should be those of the user account created above. If the connection is successful, the screen below will appear. Drag and drop the file to the right to transfer the file.

image.png

From here on, just like you do on Ubuntu on WSL

chmod +x ./Server.x*
./Server.x86_64

You can run the build with.

in conclusion

For a network technology layman like myself, preparing a server for multiplayer games seemed like a huge hurdle, but the setup with Mirror was very simple and surprising. I haven't used it yet, so I may have some problems, but it seems to be just right for studying networks.

Recommended Posts

Host the network library Mirror for Unity on a Linux server
Create a QR code for the URL on Linux
Build a simple WebDAV server on Linux
Make Unity Accelerator a service on Linux
Build a Samba server on Arch Linux
[CentOS 7.3] Build an FTP server on the ESXi host
Build a server on Linux and local network with Raspberry Pi NextCloud and desktop sharing
[Part 2] Let's build a web server on EC2 Linux
[Part 1] Let's set up a Minecraft server on Linux
[Linux] How to install a package on a server that does not have a network environment (standalone)
Avoiding the pitfalls of using a Mac (for Linux users?)
Search for large files on Linux from the command line
Access the host SQL Server with python27 / pyodbc on the container
Library for specifying a name server and dig with python
Install the latest version of Git on your Linux server
Build a python environment on CentOS 7.7 for your home server
Get the host name of the host PC with Docker on Linux
Install the JDK on Linux
Paste the link on linux
A note on how to check the connection to the license server port
Build a Selenium environment on Amazon Linux 2 in the shortest time
On Linux, the time stamp of a file is a little past.
Script for backing up folders on the server to Google Drive
A command to easily check the speed of the network on the console
What I stumbled upon when using CodeIgniter on a Linux server
Install Arch Linux on DeskMini A300
Create a Linux environment on Windows 10
Run TensorFlow2 on a VPS server
pykintone on Windows Subsystem for Linux
Notes on using matplotlib on the server
Randomly play the movie on ChromeCast for a certain period of time
Procedure for building a kube environment on amazon linux2 (aws) ~ (with bonus)
A memo for utilizing the unit test mechanism KUnit of the Linux kernel
I measured the run queue wait time of a process on Linux
Hook to Shared Library on Linux to interrupt the behavior of existing binaries
Install the python module with pip on a server without root privileges
A note about the functions of the Linux standard library that handles time
Try using a Linux server as a backup destination for Time Machine (Ver. 2020)
Verification of how to periodically execute a script on a Linux server on Windows
Rock-paper-scissors with Python Let's run on a Windows local server for beginners