Network System Concepts

Server and Host
In the unity networking system, games have a Server and multiple Clients. When there is no dedicated server, one of the clients plays the role of the server - we call this client the “host”. 在Unity3D的网络系统中,游戏拥有一个服务端和多个客户端。如果没有一个专用的服务端的话,其中一个客户端将同时扮演服务端的角色。我们把这个客户端叫做“host”。
The host is a server and a client in the same process. The host uses a special kind of client called the LocalClient, while other clients are RemoteClients. The LocalClient communicates with the (local) server through direct function calls and message queues, since it is in the same process. It actually shares the scene with the server. RemoteClients communicate with the server over a regular network connection. Host即是在同一个进程空间中的一个客户端和一个服务端的集合体。Host的客户端称为LocalClient,其他的客户端则称为RemoteClient。LocalClient和与之同一个进程空间的服务端是通过直接的函数调用(direct function call)和消息队列(message queues)进行通信的。RemoteClient和服务端则是通过一定的网络连接进行通信。
One of the aims of the networking system is for the code for LocalClients and RemoteClients to be the same, so that developers only have to think about one type of client most of the time. 设计一个网络系统的目标之一,就是让上层逻辑看起来,对LocalClient和RemoteClient的代码设计是一样。所以开发者在绝大部分时间中,只需要考虑一种客户端类型即可
Instantiate and Spawn
In Unity, GameObject.Instantiate creates new Unity game objects. But with the networking system, objects must also be “spawned” to be active on the network. This can only be done on the server, and causes the objects to be created on connected clients. Once objects are spawned, the Spawning System uses distributed object life-cycle management and state-synchronization principles.For more details see Spawning. GameObject.Instantiate方法创建一个新的Unity game object。但对于网络系统而言,这些对象同时也需要在网络中“产生并激活”(be spawned to be active)。这个操作仅仅需要在服务器中进行,然后向所有连接到本服务器上的客户端发送一个创建object的通知。一旦一个object被产生了,spawning系统将使用分布式的对象生命期管理方式,以及状态同步原则,更多的细节可参照Spawning章节。
Players, Local Players and Authority
In the network system, player objects are special. There is a player object associated with each person playing the game, and commands are routed to that object. A person cannot invoke a command on another person’s player object - only their own. So there is a concept of “my” player object. When a player is added and the association is made with a connection, that player object becomes a “local player” object on the client of that player. There is a property isLocalPlayer that is set to true, and a callback OnStartLocalPlayer() that is invoked on the object on the client. The diagram below shows two clients and their local players. 在网络系统中个,“player object”是特有的。在游戏中,一个游戏玩家对应一个与之关联的player object。一个玩家不能对别的玩家的player object直接发布“命令”,而只能操作他自己拥有的那个player object。因此,这会有一个“我的player object”的概念。当一个玩家连入游戏时,玩家本地的这个player object将称为一个local player。NetworkBehaviour组件中的isLocalPlayer属性将会被设置为true,对应客户端中的OnStartLocalPlayer()回调方法将会被调用,下图展示了两个玩家和他们各自对应的local player。
Only the player object that is “yours” will have the isLocalPlayer flag set. This can be used to filter input processing, to handle camera attachment, or do any other client side things that should only be done for your player. 只有是你本地自己的player object才会isLocalPlayer标志设置为true。这个标志可以用来过滤用户输入操作;绑定摄像机;以及一些只能作用到你自己的player object上的客户端操作
In addition to isLocalPlayer, a player object can have “local authority”. This means that the player object on its owner’s client is responsible for the object - it has authority. This is used most commonly for controlling movement, but can be used for other things also. The NetworkTransform component understands this and will send movement from the client if this is set. The NetworkIdentity has a checkbox for setting LocalPlayerAuthority. 除了isLocalPlayer之外,一个player object还有一个“local authority”属性。这个属性为true的时候,表示在本地客户端的那个player object是可以“被负责的”(responsible).大多数情况下,这个标志为true时,player object才能被移动。除了移动之外,还能做一些别的操作。NetworkTransform组件能感知到这个属性为true,并且从客户端中发送移动命令。NetworkIdentity组件上有这个LocalPlayerAuthority的勾选项。
For non-player objects such as enemies, there is no associated client, so authority resides on the server. 对于非player object,比如敌人怪物等等。他们没有对应的客户端,所以他们的相关权限由服务端控制。
There is a property “hasAuthority” on the NetworkBehaviour that can be used to tell if an object has authority. So non-player objects have authority on the server, and player objects with localPlayerAuthority set have authority on their owner’s client. NetworkBehaviour组件中有一个“hasAuthority”属性。这个属性将决定一个object是否有权限。所以一个非player object在服务端中有对应权限,player object如果localPlayerAuthority属性设置为true的话,在它本地客户端有权限。
Client Authority for Non-Player Objects
Starting with Unity release 5.2, it is possible to have client authority over non-player objects. There are two ways to do this. One is to spawn the object using NetworkServer.SpawnWithClientAuthority and pass the network connection of the client to take ownership. The other is to use NetworkIdentity.AssignClientAuthority with the network connection of the client to take ownership. 从5.2版本开始。可以给非player object设置客户端权限。可以有两种方法去设置。一个是通过调用NetworkServer.SpawnWithClientAuthority方法,传递该客户端和服务端的网络连接信息,去产生object。另一种方式则是使用NetworkIdentity.AssignClientAuthority方法去设置。
Assigning authority to a client causes OnStartAuthority() to be called on NetworkBehaviours on the object, and the property hasAuthority will be true. On other clients the hasAuthority property will still be false. Non-player objects with client authority can send commands, just like players can. These commands are run on the server instance of the object, NOT on the player associated with the connection. 给一个客户端赋值权限将会引起NetworkBehaviours组件的OnStartAuthority()方法的调用。同时hasAuthority属性将会被设置为true。其他客户端的hasAuthority属性则会继续保持为false。被赋值了权限的非player object,可以像player那样子发送命令。这些命令将在服务器中的object实例中运行,而并不是在关联的player上运行.
Non-player objects that are to have client authority must have LocalPlayerAuthority checked in their NetworkIdentity. 如果一个非player object有客户端权限的话,必须把它的NetworkIdentity组件中的LocalPlayerAuthority勾选为true
This example below spawns an object and assigns authority to the client of the player that spawned it. 下面是如何产生一个object,并且给它赋值上权限的示例代码:
void CmdSpawn()
    var go = (GameObject)Instantiate(otherPrefab, transform.position + new Vector3(0,1,0),       Quaternion.identity);
    NetworkServer.SpawnWithClientAuthority(go, connectionToClient);
Network Context Properties

There are properties on the NetworkBehaviour class that allow scripts to know what the network context is of a networked object at any time. isServer - true if the object is on a server (or host) and has been spawned.

isClient - true if the object is on a client, and was created by the server.
isLocalPlayer - true if the object is a player object for this client.
hasAuthority - true if the object is owned by the local process
These properties can be seen in the preview window of the object in the Inspector window of the editor.


isServer - object在一个server中,并且已经被产生时,为true
isClient - object在一个client中,并且已经被服务端产生时,为true
isLocalPlayer - 某object在本客户端中,如果是一个play object的话,为true
hasAuthority - 如果某object时被本地进程所拥有的话,为true