Mirror Common Components

Mirror Common Components

Mirror is a high-performance, lightweight network library for Unity, which can help developers quickly build online games and applications. Mirror provides multiple network components, the following is a detailed introduction of five commonly used components.

NetworkManager

introduce

The NetworkManager component is responsible for managing network connections. It can start servers and clients, and handle operations such as network connection, disconnection, and reconnection. At the same time, NetworkManager also provides some event callback functions, such as OnServerConnect, OnClientDisconnect, etc., which can handle network events conveniently.

method

  • StartServer(): Start the server.

  • StartClient(): Start the client.

  • StopServer(): Stop the server.

  • StopClient(): Stop the client.

for example

Here are a few common code examples that demonstrate how to use the NetworkManager component:

Example 1: Start server and client

using UnityEngine;
using Mirror;

public class MyNetworkManager : NetworkManager
{
    public void StartServer()
    {
        base.StartServer();
    }

    public void StartClient()
    {
        base.StartClient();
    }

    public void StopServer()
    {
        base.StopServer();
    }

    public void StopClient()
    {
        base.StopClient();
    }
}

In this example, we create a class called MyNetworkManager and inherit the NetworkManager component. We start the server and client by calling the base class's StartServer and StartClient functions, and stop the server and client by calling the base class's StopServer and StopClient functions.

NetworkIdentity

introduce

The NetworkIdentity component is used to identify network game objects, which can synchronize the state and properties of game objects in the network. When a game object is identified as a NetworkIdentity, it automatically synchronizes its position, rotation, scale, variables, etc. properties between the server and client.

method

  • AssignClientAuthority(NetworkConnection conn): Assigns ownership of the network object to the specified client connection.

  • RemoveClientAuthority(NetworkConnection conn): Removes ownership of the network object from the specified client connection.

for example

Here's a common example that demonstrates how to use the NetworkIdentity component:

Example: Identifying Network Objects

using UnityEngine;
using Mirror;

public class MyNetworkObject : NetworkBehaviour
{
    // 在网络中同步的变量
    [SyncVar]
    public int score;

    void Start()
    {
        // 检查是否是本地客户端
        if (isLocalPlayer)
        {
            // 分配客户端所有权
            CmdAssignClientAuthority();
        }
    }

    [Command]
    void CmdAssignClientAuthority()
    {
        // 将网络对象的所有权分配给本地客户端
        if (connectionToClient != null)
        {
            AssignClientAuthority(connectionToClient);
        }
    }
}

In this example, we create a class called MyNetworkObject that inherits the NetworkBehaviour component. We use the [SyncVar] attribute to synchronize the score variable over the network, and check whether it is a local client in the Start function. In case of a local client, we assign ownership of the network object to the client using the CmdAssignClientAuthority function.

NetworkBehaviour

introduce

The NetworkBehaviour component is used to handle network messages, which can synchronize the state and properties of game objects between the server and the client. When a game object is identified as a NetworkIdentity, it automatically synchronizes its position, rotation, scale, variables, etc. properties between the server and client.

method

  • Send<T>(T message, int channelId = Channels.DefaultReliable): Send a network message to the server or client.

  • [Command]: Used to mark a remote procedure call (RPC) function called by a client.

  • [ClientRpc]: A remote procedure call (RPC) function used to mark a server call.

for example

Here is a common example demonstrating how to use the NetworkBehaviour component:

Example: Synchronizing the position and rotation of game objects

using UnityEngine;
using Mirror;

public class MyNetworkObject : NetworkBehaviour
{
    // 在网络中同步的变量
    [SyncVar]
    public Vector3 position;

    [SyncVar]
    public Quaternion rotation;

    void Update()
    {
        if (isLocalPlayer)
        {
            // 更新本地游戏对象的位置和旋转
            position = transform.position;
            rotation = transform.rotation;
        }
        else
        {
            // 同步远程游戏对象的位置和旋转
            transform.position = position;
            transform.rotation = rotation;
        }
    }
}

In this example, we create a class called MyNetworkObject that inherits the NetworkBehaviour component. We use the [SyncVar] attribute to synchronize the position and rotation variables across the network, and in the Update function to update the local game object's position and rotation, and to synchronize the remote game object's position and rotation.

NetworkTransform

introduce

The NetworkTransform component is used to synchronize the position, rotation and scale of the game object, and it can synchronize the state of the game object between the server and the client. When a game object is identified as a NetworkIdentity, and a NetworkTransform component is added, it will automatically synchronize its position, rotation, scaling and other properties between the server and the client.

method

  • SetDirtyBit(int dirtyBit): Set the specified dirty bit as a dirty flag for synchronization in the network.

for example

Here is a common example that demonstrates how to use the NetworkTransform component:

Example: Synchronizing the position and rotation of game objects

using UnityEngine;
using Mirror;

public class MyNetworkObject : NetworkBehaviour
{
    // 在网络中同步的变量
    [SyncVar]
    public Vector3 position;

    [SyncVar]
    public Quaternion rotation;

    // 添加 NetworkTransform 组件
    [SerializeField]
    NetworkTransform networkTransform;

    void Update()
    {
        if (isLocalPlayer)
        {
            // 更新本地游戏对象的位置和旋转
            position = transform.position;
            rotation = transform.rotation;

            // 设置 NetworkTransform 组件的脏标志
            networkTransform.SetDirtyBit(1 << 0);
        }
    }
}

In this example, we create a class called MyNetworkObject that inherits the NetworkBehaviour component. We use the [SyncVar] attribute to synchronize the position and rotation variables across the network, and add a NetworkTransform component to synchronize the game object's position, rotation and scale properties. In the Update function, we update the local game object's position and rotation, and set the NetworkTransform component's dirty flag to 1 using the SetDirtyBit function.

Guess you like

Origin blog.csdn.net/qq_20179331/article/details/130632943