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.
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.
You can join the game by pressing Client in the upper left. However, the game does not start because it is not currently networked.
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.
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.
After that, set Linux to Target Platform as shown below, and check "Server Build" to build.
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.
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.
You can play Ping by preparing another Client and connecting to the server in the same way.
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.
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.
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.
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
.
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.
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.
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.
After applying, you will receive your account information and other information within a few hours. Information about SSH connections is especially important.
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.
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. 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.
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.
From here on, just like you do on Ubuntu on WSL
chmod +x ./Server.x*
./Server.x86_64
You can run the build with.
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