请尊重原作者的工作,转载时请务必注明转载自:www.xionggf.com
 

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.
很多多人游戏可以使用NetworkManager去管理网络连接。但也能使用更底层的NetworkServer类和NetworkClient类去直接操作。
 
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.
当使用高层的网络API时,每一个游戏都必须有一个是同时作为服务端的客户端HOST。因此,每一个多人游戏的玩家可以是一个单独的客户端,一个单独的服务端,或者既是客户端又是服务端。这种即是客户端又是服务端的情况是一个多人 游戏中常见的情况
 
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.
对于没有独立服务端的游戏而言。其中一个玩家将会同时执行服务端的职责。同时这个玩家的客户端也被称为“local client”。local client使用和服务端相同的object和场景。内部使用一个消息队列去代替通过发送网络消息去负责通信。但是对高层API而言。local client也仅仅是另一个客户端。所以无论是remote还是local的客户端,上层的逻辑代码基本是相同的。这对于开发一个多人游戏是很便利的。
 
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”.
多人游戏的一个通用的模式是:有一个object去专门管理游戏的网络状态。在下面的代码便是NetworkManager的一个演示实现。这个脚本用来挂接到一个在游戏开始场景中的game object上。它拥有一个简单的UI和键盘输入操作函数。允许游戏以不同的网络模式输入。对于一个真实的游戏,可能有一个真正的菜单,上面有“开始单人模式”和“开始多人模式”的选项。
 

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");
        }
    }   
}

 
1
2
 
1
2
 
1
2
 
1
2