Unity5.1 新的网络引擎UNET(十五) Networking 引用--上

http://blog.csdn.net/u010019717/article/details/46993697

孙广东  2015.7.21

本节提供了与网络系统一起使用的组件的详细信息。

1、Network Animator

 

NetworkAnimator 用于跨网络同步动画。

Properties

Property:Function:
animator 要同步的对象上的Animator 组件。

Details  暂无

2、NetworkBehaviour


NetworkBehaviours 是特别脚本,用于处理对象上的 NetworkIdentity 组件。这些脚本都能够执行 HLAPI ,像Commands、 ClientRPCs、 SyncEvents 和 SyncVars 等功能。


与Unity Network System服务器authoritative 权威系统,网络对象的 NetworkIdentities 必须是“spawned”,由服务器使用 NetworkServer.Spawn()。这会导致他们被分配一个NetworkInstanceId,在连接到服务器的客户端上创建。

Properties

Property:Function:
isLocalPlayer True if this object is the player object for the local client.
isServer True if this object is running on the server, and has been spawned.
isClient True if this object is running on a client.
hasAuthority True if this object is the authoritative version of the object. So either on the server, or on the client with localPlayerAuthority.
assetId This is the assetId of the object’s NetworkIdentity.
netId This is the netId of the object’s NetworkIdentity.
playerControllerId This is the playerControllerId of the object’s NetworkIdentity.
connectionToServer NetworkConnection object to use to send to the server.
connectionToClient

NetworkConnection object to use to send to the client.

NetworkBehaviours 具有以下的特点,介绍如下。

• Synchronized Variables 同步变量
• Network callbacks 网络回调
• Server and Client functions服务器和客户端功能
• Sending Commands发送命令
• Client RPC Calls 客户端 RPC 调用
• Networked Events 网络事件

Synchronized Variables
同步的变量
NetworkBehaviours 成员变量可以从服务器同步到客户端。因为该服务器是有权威在此系统中,同步是仅在  服务器到客户端  的方向。 客户请求做的事情是通过命令Commands处理,不是从客户端同步的变量。


SyncVar 属性用于 标记 成员变量,如  正在同步。SyncVars 可以是任何 基本类型、 不是类、 列表或其他集合。

[csharp] view plain copy
 
 print?
  1. public class SpaceShip : NetworkBehaviour  
  2. {  
  3.     [SyncVar]  
  4.     public int health;  
  5.   
  6.     [SyncVar]  
  7.     public string playerName;  
  8. }  


SyncVar 的值在服务器上更改,它将发送到所有的游戏中准备好的clients 。当 生成对象 时,客户端上创建他们从服务器的所有 SyncVars 的最新状态。


Network callbacks
网络回调


有各种 网络事件的 NetworkBehaviour 脚本调用的回调函数。这些是对基类 虚函数,所以它们可以被重写在使用这样的代码:

[csharp] view plain copy
 
 print?
  1. public class SpaceShip : NetworkBehaviour  
  2. {  
  3.     public override void OnStartServer()  
  4.     {  
  5.         // disable client stuff  
  6.     }  
  7.   
  8.     public override void OnStartClient()  
  9.     {  
  10.         // register client events, enable effects  
  11.     }  
  12. }  


当对象在服务器上被生成或当服务器启动时 对场景中的对象   调用 OnStartServer 函数。  当对象在客户端被生成 ,或者当客户端连接到服务器以进行场景中的物体,将调用 OnStartClient 函数。这些函数是有用的, 去做那些特定于客户端或服务器的事,  例如suppressing effects 抑制效应在服务器上,或设置客户端事件。

      请注意,当使用的是local client 本地客户端,这两个函数将被同一个对象调用。

其他回调包括:
• OnSerialize - called to gather state to send from the server to clients 调用以收集状态将从服务器发送到客户端
• OnDeSerialize - called to apply state to objects on clients 调用以将状态应用于客户端上的对象
• OnNetworkDestroy - called on clients when server told the object to be destroyed 当服务器告诉要被销毁的对象在客户端上调用
• OnStartLocalPlayer - called on clients for player objects for the local client (only)
• OnRebuildObservers - called on the server when the set of observers for an object is rebuild
• OnSetLocalVisibility - called on a host when the visibility of an object changes for the local client
• OnCheckObserver - called on the server to check visibility state for a new client


Server and Client functions
服务器和客户端功能

NetworkBehaviours 中的成员函数可以用来   自定义属性  来指定它们 作为 仅服务器或仅客户端的功能标记。例如:

[csharp] view plain copy
 
 print?
  1. using UnityEngine;  
  2. using UnityEngine.Networking;  
  3.   
  4. public class SimpleSpaceShip : NetworkBehaviour  
  5. {  
  6.     int health;  
  7.   
  8.     [Server]  
  9.     public void TakeDamage( int amount)  
  10.     {  
  11.         // will only work on server  
  12.         health -= amount;  
  13.     }  
  14.   
  15.     [Client]  
  16.     void ShowExplosion()  
  17.     {  
  18.         // will only run on client  
  19.     }  
  20.   
  21.     [ClientCallback]  
  22.     void Update()  
  23.     {  
  24.         // engine invoked callback - will only run on client  
  25.     }  
  26. }  


这些属性  使函数立即返回, 如果他们在客户端或服务器未处于活动状态时调用。他们不会生成编译时错误,但如果调用错误的范围内,他们将发出警告日志消息。ServerCallback 和 ClientCallback 的属性可以用于用户代码不能控制的调用的引擎回调函数。这些属性不会导致生成的警告。


Sending Commands
发送命令


     命令是为客户端请求做某事  在服务器上的方式。由于 HLAPI 是一个服务器的权威系统,客户可以只通过命令做事情。发送该命令的客户端上的player 对象对应的服务器上运行命令。这种路由会自动发生,客户端一个不同的player发送命令它是不可能的。

命令必须以  前缀"Cmd" 开头和有  [Command] 自定义属性,如以下:

[csharp] view plain copy
 
 print?
  1. using UnityEngine;  
  2. using UnityEngine.Networking;  
  3.   
  4. public class SpaceShip : NetworkBehaviour  
  5. {  
  6.     bool alive;  
  7.     float thrusting;  
  8.     int spin;  
  9.   
  10.     [Command]  
  11.     public void CmdThrust(float thrusting, int spin)  
  12.     {     
  13.         if (!alive)  
  14.         {  
  15.             this.thrusting = 0;  
  16.             this.spin = 0;  
  17.             return;  
  18.         }  
  19.               
  20.         this.thrusting = thrusting;  
  21.         this.spin = spin;  
  22.     }  
  23.   
  24.     [ClientCallback]  
  25.     void Update()  
  26.     {  
  27.         int spin = 0;  
  28.         if (Input.GetKey(KeyCode.LeftArrow))  
  29.         {  
  30.             spin += 1;  
  31.         }  
  32.         if (Input.GetKey(KeyCode.RightArrow))  
  33.         {  
  34.             spin -= 1;  
  35.         }  
  36.         // this will be called on the server  
  37.         CmdThrust(Input.GetAxis("Vertical"), spin);  
  38.     }  
  39. }  


        命令被调用,通常在客户端上  。但是,而不是命令函数在客户端上运行的 ,它将在该服务器上的客户端Player对象调用。因此,命令是类型安全,有内置的安全机制和路由到player,以及使用参数有效的序列化机制,使快速调用它们。


Client RPC Calls
客户端 RPC 调用


      客户端 RPC 调用是   一个服务器对象 让  客户端对象 做一些事情的方式。这是和如何用命令发送消息 方向相反,但概念是相同的。客户端 RPC 调用然而不只调用player对象,他们可以在任何 NetworkIdentity 对象上调用。必须以前缀 "Rpc" 开头,并且必须 [ClientRPC] 的自定义属性,像下面:

[csharp] view plain copy
 
 print?
  1. using UnityEngine;  
  2. using UnityEngine.Networking;  
  3.   
  4. public class SpaceShipRpc : NetworkBehaviour  
  5. {  
  6.     [ClientRpc]  
  7.     public void RpcDoOnClient(int foo)  
  8.     {  
  9.         Debug.Log("OnClient " + foo);  
  10.     }  
  11.   
  12.     [ServerCallback]  
  13.     void Update()  
  14.     {  
  15.         int value = UnityEngine.Random.Range(0,100);  
  16.         if (value < 10)  
  17.         {  
  18.             // this will be invoked on all clients  
  19.             RpcDoOnClient(value);  
  20.         }  
  21.     }  
  22. }  

Networked Events
网络的事件


      网络的事件就像   客户端 RPC 调用,但不是只在客户端对象上调用一个函数,客户端对象上的事件将被触发。为事件 注册的其他脚本 然后调用 -  参数在服务器上,所以这使得网络的客户端上脚本间交互。事件必须以前缀 “Event” 开头并且有 SyncEvent 的自定义属性。


事件可以用于生成功能强大的网络游戏系统,可以通过其他脚本扩展。此示例演示如何影响脚本在客户端上的可以响应由服务器上的战斗脚本生成的事件。

[csharp] view plain copy
 
 print?
  1. using UnityEngine;  
  2. using UnityEngine.Networking;  
  3.   
  4. // Server script  
  5. public class MyCombat : NetworkBehaviour  
  6. {  
  7.     public delegate void TakeDamageDelegate(int side, int damage);  
  8.     public delegate void DieDelegate();  
  9.     public delegate void RespawnDelegate();  
  10.       
  11.     float deathTimer;  
  12.     bool alive;  
  13.     int health;  
  14.   
  15.     [SyncEvent(channel=1)]  
  16.     public event TakeDamageDelegate EventTakeDamage;  
  17.       
  18.     [SyncEvent]  
  19.     public event DieDelegate EventDie;  
  20.       
  21.     [SyncEvent]  
  22.     public event RespawnDelegate EventRespawn;  
  23.   
  24.     [Server]  
  25.     void TakeDamage(int amount)  
  26.     {  
  27.         if (!alive)  
  28.             return;  
  29.               
  30.         if (health > amount) {  
  31.             health -= amount;  
  32.         }  
  33.         else  
  34.         {  
  35.             health = 0;  
  36.             alive = false;  
  37.             // send die event to all clients  
  38.             EventDie();  
  39.             deathTimer = Time.time + 5.0f;  
  40.         }  
  41.     }  
  42.   
  43.     [ServerCallback]  
  44.     void Update()  
  45.     {  
  46.         if (!alive)  
  47.         {  
  48.             if (Time.time > deathTimer)  
  49.             {  
  50.                 Respawn();  
  51.             }  
  52.             return;  
  53.         }  
  54.     }  
  55.   
  56.     [Server]  
  57.     void Respawn()  
  58.     {  
  59.         alive = true;  
  60.         // send respawn event to all clients  
  61.         EventRespawn();  
  62.     }  
  63. }  

Hints

    • This is a base class that provides Commands and ClientRpc calls.
原文地址:https://www.cnblogs.com/alps/p/5644525.html