Unity Mirror - General
Unity Mirror - General
Mirror is a system for creating multiplayer capabilities for Unity games. The low-level communication is built on top of a real-time communication layer, handling many common tasks required for multiplayer games. While the communication layer supports all types of network topologies, Mirror is a server authoritative system. It allows a participant to be both client and server at the same time, so it does not require a dedicated server process. By working together with internet services, it allows these multiplayer games to be played over the internet with little effort from developers.Mirror is focused on ease of use and iterative development, providing useful functionalities for multiplayer games such as:
- Message handlers
- General-purpose high-performance serialization
- Distributed object management
- State synchronization
- Network classes: Server, Client, Connection, etc.
Mirror consists of a series of layers that add functionality.
Mirror multiplayer games include:
Server
The server is an instance of the game to which other players who want to gather and play together connect. The server often manages various aspects of the game, such as keeping scores, and transfers this data by sending it to the client.
Clients
Clients are usually instances of the game connecting to the server from different computers.
Clients can connect via local network or online.
The client is an instance of the game connecting to the server to play the game with other people.
The server can be either a "dedicated server" or a "host server".
Dedicated server
This is a game instance running solely to act as a server.
Host server
When there is no dedicated server, one of the clients also assumes the role of server. This client is called the "host server". The host server creates a single game instance (host) that acts as both server and client.
The diagram below represents three players in a multiplayer game.
In this game, one client is also acting as the host server, which means the client itself is the "local client". The local client connects to the host server and both run on the same computer. The other two players are remote clients - meaning they are connected to the host server from different computers.
The host is a single instance of the game that acts as both server and client at the same time. While the host uses a special type of internal client for local client communication, other clients are remote clients. The local client communicates with the server directly via function calls and message queues because it is in the same process. In fact, it shares the scene with the server. Remote clients communicate with the server over a regular network connection. When using Mirror, all this is managed automatically.
One of the purposes of multiplayer systems is to have the same code for local and remote clients, so that most of the time, while developing your game, you only need to think about one type of client. In most cases, Mirror manages this difference automatically, so you rarely need to think about whether your code will run on a local client or a remote client.
Instantiate and Spawn
When making a single-player game in Unity, you often use the GameObject.Instantiate method to create new game objects at runtime. However, in a multiplayer system, the server must "spawn" the game objects for them to be active in the networked game. When the server spawns game objects, it triggers the creation of those game objects on the connected clients. The spawning system manages the game object's lifecycle and synchronizes the game object's state depending on how you have set up the game object.
For more details about networked object instantiation and spawning, you can refer to the "Spawning GameObjects" documentation.
Players and Local Players
Mirror handles player game objects differently from other game objects. When a new player joins (when a new client connects to the server), the player's game object becomes the "local player" game object on that player's client, and Mirror associates the player's connection with the player's game object. Mirror associates a player game object for each person playing, and routes network commands to the relevant player game object. A player can only invoke a command on their own game object, not on someone else's.
For more details, you can check out the "Player GameObjects" documentation.
Authority
The server and clients can manage the behavior of a game object. The concept of "authority" is about how and where a game object is managed. Mirror is built on "server authority" by default, meaning the server has authority over all game objects. Player game objects are a special case and are handled with "local authority". You may want to build your game using a different authority system - for more details, see the "Network Authority" documentation.




Yorum Gönder