Above: juejin. Cn/post / 699293…

Introduction to the

In order to demonstrate the advantages and basic use of Mirror, let’s take a quick walk through the basic process and related features of Mirror to create a server and client game. The purpose of this chapter is simply to give you a glimpse into the general flow of using the Mirror plugin, so just follow along and get an idea of what’s going on. The following notes will cover the apis used in more detail.

The target

We’re going to use the Mirror to make a server and a client that implement these functions:

  1. With the server turned on, multiple players can connect from their client games

  2. After a successful client connection, each player can only control his or her game character (one block)

  3. After the player presses WSAD on their keyboard, the server makes them move and synchronizes their location to other clients in real time

  4. After the player presses the space bar, the server changes color randomly for their character and synchronizes the color to all clients

The first part

1. Create a project and import the Mirror plug-in

2. Add an empty object to the scene and add the NetworkManager component to it:

NetworkManager is the central component of the Mirror, controlling server/client setup and connection, as well as data transfer. Can be understood as the network GameManager. In general, every scenario that requires networking requires such a component. Of course, this will be covered in more detail in the next article.

In addition, you can see that the Mirror automatically adds a Kcp Transport component, or Kcp protocol component, to the object. This component can also be replaced by TCP Transport, Telegraphy Transport, etc. Of course, all of this will come later, but let’s leave it alone for now

3. Next, we need to make a player Prefab, which we can represent with a Cube:

4. In the Mirror, all networked game objects that need to be synchronized across multiple clients need to have a NetworkIdentity component, which gives an object a unique identity across all clients and enables the server to manage it. So we also need to add a NetworkIdentity component to our player objects:

– We don’t need to set its properties for now, but more on that in a future article

5. When you’re done, save the Prefab

6. In the Mirror, the player’s Prefab is a special object. Each time a client connects to the server, NetworkManager generates a Player Player object for that client in the scene and synchronizes the Player object’s initial location to all other clients. We need to add the Prefab we just created to NetworkManager’s Player Object property:

– Auto Create Player: specifies whether to automatically generate Player Prefab after a Player connects to the server. So let’s check – Player Spawn Method: Forget that, I’ll get to that later. To maintain the Random

7. In fact, we haven’t implemented anything yet, but at this point a very, very basic server and client is complete. If we package it now, after the server runs, every time a client connects, it will generate a player object at the origin. Let’s test it below:

Package and run the server and client

1. Until then, since we haven’t written the code to connect and start the server, we can first use the Mirror’s built-in connection function — add NetworkManagerHUD component to NetworkManager:

2. This way, after starting the game, we will have a very ugly (crossed out) UI in the upper left corner to help us connect/start the server:

3. We pack the game (PS. Build it (PPS. Of course, the general game will be dedicated to the server and client packages, but for our simple example now directly is ok (PPPS. Of course, more on that later)))

4. Start the game. We need to start a few more to simulate multiple clients. To make debugging more intuitive, let’s start Unity as well

5. It should look like above after startup. At this point there is nothing in the game because the server is not started and the client is not connected.

To facilitate debugging, we made the Unity side the Server side, click Server Only

6. You can see that our server has started successfully. Now go to the Client and click the Client button to connect to our local server as a Client (change localHost to 127.0.0.1 in the input box if the connection fails).

7. After the connection is successful, you can see that both of our clients have successfully generated players in the game as shown in the picture above!

But since we didn’t set the birthpoint position (more on that later), all the game objects added to the client are generated at the origin by default, so they are overlapped. However, we can see that there are two players in the Hierarchy on the Unity server, which means that both players are in the game:

Also notice that each player’s name has a “connId” tag, which is the unique identifier assigned by the NetworkIdentity component.

Part I Summary

While we haven’t written a single line of code so far, a very, very simple server and client have been successfully created, and players can connect to different game servers via IP.

Let’s review the goals we started with:

  1. With the server turned on, multiple players can connect from their client games

  2. After a successful client connection, each player can only control his or her game character (one block)

  3. After the player presses WSAD on their keyboard, the server makes them move and synchronizes their location to other clients in real time

  4. After the player presses the space bar, the server changes color randomly for their character and synchronizes the color to all clients

So far, we have zero code that achieves the first two goals (although the second goal is not well represented yet), and we have a basic understanding of how to use mirrors. In the next article, we’ll use the code and the APIS provided by the Mirror to accomplish the remaining two goals.