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 可以是任何 基本类型、 不是类、 列表或其他集合。
- public class SpaceShip : NetworkBehaviour
 - {
 - [SyncVar]
 - public int health;
 - [SyncVar]
 - public string playerName;
 - }
 
SyncVar 的值在服务器上更改,它将发送到所有的游戏中准备好的clients 。当 生成对象 时,客户端上创建他们从服务器的所有 SyncVars 的最新状态。
Network callbacks
网络回调
有各种 网络事件的 NetworkBehaviour 脚本调用的回调函数。这些是对基类 虚函数,所以它们可以被重写在使用这样的代码:
- public class SpaceShip : NetworkBehaviour
 - {
 - public override void OnStartServer()
 - {
 - // disable client stuff
 - }
 - public override void OnStartClient()
 - {
 - // register client events, enable effects
 - }
 - }
 
当对象在服务器上被生成或当服务器启动时 对场景中的对象 调用 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 中的成员函数可以用来 自定义属性 来指定它们 作为 仅服务器或仅客户端的功能标记。例如:
- using UnityEngine;
 - using UnityEngine.Networking;
 - public class SimpleSpaceShip : NetworkBehaviour
 - {
 - int health;
 - [Server]
 - public void TakeDamage( int amount)
 - {
 - // will only work on server
 - health -= amount;
 - }
 - [Client]
 - void ShowExplosion()
 - {
 - // will only run on client
 - }
 - [ClientCallback]
 - void Update()
 - {
 - // engine invoked callback - will only run on client
 - }
 - }
 
这些属性 使函数立即返回, 如果他们在客户端或服务器未处于活动状态时调用。他们不会生成编译时错误,但如果调用错误的范围内,他们将发出警告日志消息。ServerCallback 和 ClientCallback 的属性可以用于用户代码不能控制的调用的引擎回调函数。这些属性不会导致生成的警告。
Sending Commands
发送命令
命令是为客户端请求做某事 在服务器上的方式。由于 HLAPI 是一个服务器的权威系统,客户可以只通过命令做事情。发送该命令的客户端上的player 对象对应的服务器上运行命令。这种路由会自动发生,客户端一个不同的player发送命令它是不可能的。
命令必须以 前缀"Cmd" 开头和有 [Command] 自定义属性,如以下:
- using UnityEngine;
 - using UnityEngine.Networking;
 - public class SpaceShip : NetworkBehaviour
 - {
 - bool alive;
 - float thrusting;
 - int spin;
 - [Command]
 - public void CmdThrust(float thrusting, int spin)
 - {
 - if (!alive)
 - {
 - this.thrusting = 0;
 - this.spin = 0;
 - return;
 - }
 - this.thrusting = thrusting;
 - this.spin = spin;
 - }
 - [ClientCallback]
 - void Update()
 - {
 - int spin = 0;
 - if (Input.GetKey(KeyCode.LeftArrow))
 - {
 - spin += 1;
 - }
 - if (Input.GetKey(KeyCode.RightArrow))
 - {
 - spin -= 1;
 - }
 - // this will be called on the server
 - CmdThrust(Input.GetAxis("Vertical"), spin);
 - }
 - }
 
命令被调用,通常在客户端上 。但是,而不是命令函数在客户端上运行的 ,它将在该服务器上的客户端Player对象调用。因此,命令是类型安全,有内置的安全机制和路由到player,以及使用参数有效的序列化机制,使快速调用它们。
Client RPC Calls
客户端 RPC 调用
客户端 RPC 调用是 一个服务器对象 让 客户端对象 做一些事情的方式。这是和如何用命令发送消息 方向相反,但概念是相同的。客户端 RPC 调用然而不只调用player对象,他们可以在任何 NetworkIdentity 对象上调用。必须以前缀 "Rpc" 开头,并且必须 [ClientRPC] 的自定义属性,像下面:
- using UnityEngine;
 - using UnityEngine.Networking;
 - public class SpaceShipRpc : NetworkBehaviour
 - {
 - [ClientRpc]
 - public void RpcDoOnClient(int foo)
 - {
 - Debug.Log("OnClient " + foo);
 - }
 - [ServerCallback]
 - void Update()
 - {
 - int value = UnityEngine.Random.Range(0,100);
 - if (value < 10)
 - {
 - // this will be invoked on all clients
 - RpcDoOnClient(value);
 - }
 - }
 - }
 
Networked Events
网络的事件
网络的事件就像 客户端 RPC 调用,但不是只在客户端对象上调用一个函数,客户端对象上的事件将被触发。为事件 注册的其他脚本 然后调用 - 参数在服务器上,所以这使得网络的客户端上脚本间交互。事件必须以前缀 “Event” 开头并且有 SyncEvent 的自定义属性。
事件可以用于生成功能强大的网络游戏系统,可以通过其他脚本扩展。此示例演示如何影响脚本在客户端上的可以响应由服务器上的战斗脚本生成的事件。
- using UnityEngine;
 - using UnityEngine.Networking;
 - // Server script
 - public class MyCombat : NetworkBehaviour
 - {
 - public delegate void TakeDamageDelegate(int side, int damage);
 - public delegate void DieDelegate();
 - public delegate void RespawnDelegate();
 - float deathTimer;
 - bool alive;
 - int health;
 - [SyncEvent(channel=1)]
 - public event TakeDamageDelegate EventTakeDamage;
 - [SyncEvent]
 - public event DieDelegate EventDie;
 - [SyncEvent]
 - public event RespawnDelegate EventRespawn;
 - [Server]
 - void TakeDamage(int amount)
 - {
 - if (!alive)
 - return;
 - if (health > amount) {
 - health -= amount;
 - }
 - else
 - {
 - health = 0;
 - alive = false;
 - // send die event to all clients
 - EventDie();
 - deathTimer = Time.time + 5.0f;
 - }
 - }
 - [ServerCallback]
 - void Update()
 - {
 - if (!alive)
 - {
 - if (Time.time > deathTimer)
 - {
 - Respawn();
 - }
 - return;
 - }
 - }
 - [Server]
 - void Respawn()
 - {
 - alive = true;
 - // send respawn event to all clients
 - EventRespawn();
 - }
 - }
 
Hints
- This is a base class that provides Commands and ClientRpc calls.
 
Unity5.1 新的网络引擎UNET(十五) Networking 引用--上的更多相关文章
- Unity5.1 新的网络引擎UNET(二) UNET 官方推荐Demo案例
		
http://blog.csdn.net/u010019717/article/details/46873153 视频 http://www.iqiyi.com/playlist391685502.h ...
 - Unity5.1 新的网络引擎UNET(九) UNET 官方推荐视频教程
		
孙广东 2015.7.14 在新的网络引擎出现之前,Unity提供的是 内置 Raknet网络引擎, 这一次Unity想更新UGUI一样,花了大的手笔更新了, UNET. 原来的旧的网络组件 被提示 ...
 - Unity5.1 新的网络引擎UNET(十五) Networking 引用--下
		
 孙广东 2015.7.21 本节提供了与网络系统一起使用的组件的具体信息. 10.Network Proximity Checker Suggest a change Success! Than ...
 - Unity5.1 新的网络引擎UNET(十五) Networking 引用--中
		
孙广东 2015.7.21 本节提供了与网络系统一起使用的组件的具体信息. 3.NetworkClient NetworkClient 是一个 HLAPI 类,管理网络连接到服务器 - - 相应着 U ...
 - Unity5.1 新的网络引擎UNET(八) UNET 系统概括
		
 孙广东 2015.7.12 Server and Host 在Unity 的 网络系统,游戏有 一个server和多个client. 当没有专用的server时,client之中的一个扮演s ...
 - Unity5.1 新的网络引擎UNET(四) UNET Remote Actions
		
孙广东 2015.7.12 网络系统 具有网络中执行操作actions 的方法.这些类型的actions 有时是调用远程过程调用(Remote Procedure Calls). 在网络系统中有两 ...
 - Unity5.1 新的网络引擎UNET(七)  UNET 单人游戏转换为多人
		
 单人游戏转换为多人 孙广东 2015.7.12 本文档描写叙述将单人游戏转换为使用新的网络系统的多人游戏的步骤.这里描写叙述的过程是简化,对于一个真正的游戏事实上须要更高级别版本号的实际 ...
 - centos  Linux系统日常管理2  tcpdump,tshark,selinux,strings命令, iptables ,crontab,TCP,UDP,ICMP,FTP网络知识 第十五节课
		
centos Linux系统日常管理2 tcpdump,tshark,selinux,strings命令, iptables ,crontab,TCP,UDP,ICMP,FTP网络知识 第十五节课 ...
 - 渗透测试学习 十五、 文件上传&&解析漏洞
		
大纲:文件解析漏洞 上传本地验证绕过 上传服务器验证绕过 文件解析漏洞 解析漏洞主要说的是一些特殊文件被IIS.Apache.Nginx在某些情况下解释成脚本文件格式的漏洞. IIS 5.x/6.0解 ...
 
随机推荐
- js与C++交互及C++解析json
			
转载:http://zhidao.baidu.com/link?url=LLuWzwMmpfVcQeSGv1CrAfRXpnZaetm9xypqwMW6zxLhhKES-rITAsG0-Ku-bSMA ...
 - [Objective-C]__bridge,__bridge_retained和__bridge_transfer的意思,区别与使用
			
使用ARC能帮我们减轻不少内存管理方面的负担,尤其是对用惯了Java的程序员来说.但是像我这种Java基础本身就不牢固,做了两年的iOS已经习惯手动管理内存的半吊子,使用ARC还是经常碰壁. 对于CF ...
 - 如何提高android串口kernel log等级
			
在 /device/qcom/common/rootdir/etc/init.qcom.rc write /proc/sys/kernel/printk "6 6 1 7" 第一 ...
 - 我的android学习经历20
			
WebView的使用 WebView既可以和Intent一样实现界面跳转一样,让系统浏览器打开页面,也可以在应用程序中打开页面 注意用WebView时,需要注册网络服务 代码如下: package c ...
 - C# Eval()和Bind()
			
Eval( " ")和Bind( " ") 这两种一个单向绑定,一个双向绑定,bind是双向绑定,但需数据源支持ASP.NET 2.0改善了模板中的数据绑定 ...
 - KEIL MDK 5.12帮你快速建工程模板的技巧
			
KEIL 5帮你快速建工程模板的技巧 本人使用keil mdk 5.12有一段时间了,发现keil mdk 5.12里面驱动库比较方便.这个新功能可以节省我们的时间,也可以让初学者能尽快上手和掌握这个 ...
 - 【leetcode❤python】13. Roman to Integer
			
#-*- coding: UTF-8 -*-#从前向后遍历罗马数字,#如果某个数比前一个数小,则加上该数.反之,减去前一个数的两倍然后加上该数###-----技术规则-----#----------- ...
 - BZOJ 1822 Frozen Nova 冷冻波(最大流)
			
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1822 题意:WJJ喜欢“魔兽争霸”这个游戏.在 游戏中,巫妖是一种强大的英雄,它的技能F ...
 - C#借助谷歌翻译实现翻译小工具(二)添加托盘图标
			
接上一节完善小翻译工具 设置Form的ShowInTaskbar属性为False,取消任务栏显示 设置Form的MaximizeBox属性为False,取消最大化显示 窗口添加两个控件 分别是:Con ...
 - Java float保留两位小数或多位小数
			
Java float保留两位小数或多位小数 方法1:用Math.round计算,这里返回的数字格式的. float price=89.89;int itemNum=3;float totalPr ...