Version: 5.6
多人游戏大厅
主机迁移

Network Clients and Servers

Many multiplayer games will be able to use the NetworkManager to manage connections, but it is possible to use the lower level NetworkServer and NetworkClient classes directly.

When using the HLAPI, every game must have a server that hosts the game. So, each participant in a multiplayer game can be a client, a dedicated server, or a combination of server and client at the same time. This combination role is the common case of a multiplayer game with no dedicated server.

For multiplayer games with no dedicated server, one of the players running the game acts as the “Server” for that game. On that that particular player’s instance of the game would be running a “Local Client” instead of a normal remote client. The local client uses the same Unity scenes and objects as the server, and communicates internally using message queues instead of sending messages across the network. But, to HLAPI code and systems, the local client is just another client, so almost all user code is the same whether a client is local or remote. This makes it easy to make a game that works in multiplayer and stand-alone mode with the same code.

A common pattern for multiplayer games is to have an object that manages the network state of the game. Below is the start of a NetworkManager script. This script would be attached to a game object that is in the startup scene of the game. It has a simple UI and keyboard handling functions that allow the game to be started in different network modes. For a real game this might be a more visually appealing menu with options such as “Start Single Player Game” and “Start Multiplayer Game”.

using UnityEngine;
using UnityEngine.Networking;

public class MyNetworkManager : MonoBehaviour {
    
    public bool isAtStartup = true;
    
    NetworkClient myClient;

    void Update () 
    {
        if (isAtStartup)
        {
            if (Input.GetKeyDown(KeyCode.S))
            {
                SetupServer();
            }
            
            if (Input.GetKeyDown(KeyCode.C))
            {
                SetupClient();
            }
            
            if (Input.GetKeyDown(KeyCode.B))
            {
                SetupServer();
                SetupLocalClient();
            }
        }
    }
    
    void OnGUI()
    {
        if (isAtStartup)
        {
            GUI.Label(new Rect(2, 10, 150, 100), "Press S for server");     
            GUI.Label(new Rect(2, 30, 150, 100), "Press B for both");       
            GUI.Label(new Rect(2, 50, 150, 100), "Press C for client");
        }
    }   
}

This skeleton of code calls setup functions to get things going. Below are the simple setup functions for each of the scenarios. These functions create a server, or the right kind of client for each scenario. Note that the remote client assumes the server is on the same machine (127.0.0.1), for a real game this would be well known internet address or something supplied by the Match Making system.

    // Create a server and listen on a port
    public void SetupServer()
    {
        NetworkServer.Listen(4444);
        isAtStartup = false;
    }
    
    // Create a client and connect to the server port
    public void SetupClient()
    {
        myClient = new NetworkClient();
        myClient.RegisterHandler(MsgType.Connect, OnConnected);     
        myClient.Connect("127.0.0.1", 4444);
        isAtStartup = false;
    }
    
    // Create a local client and connect to the local server
    public void SetupLocalClient()
    {
        myClient = ClientScene.ConnectLocalServer();
        myClient.RegisterHandler(MsgType.Connect, OnConnected);     
        isAtStartup = false;
    }

The clients in this code registered a callback function for the connection event MsgType.Connect. This is a built-in message of the HLAPI that is invoked when a client connects to a server. In this case, the code for the handler on the client is:

    // client function
    public void OnConnected(NetworkMessage netMsg)
    {
        Debug.Log("Connected to server");
    }

This is enough to get a multiplayer application up and running. It would now be possible to send network messages using NetworkClient.Send and NetworkServer.SendToAll, however sending messages is a low level way of interacting with the system.

多人游戏大厅
主机迁移