查看: 3223|回复: 0
打印 上一主题 下一主题

[Unity 组件参考手册]桌面:网络参考指南之网络关卡加载

[复制链接]
.    

3797

主题

11

听众

5万

积分

首席设计师

Rank: 8Rank: 8

纳金币
32328
精华
41

活跃会员 优秀版主 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2013-2-19 17:04:15 |只看该作者 |倒序浏览
Below is a simple example of a way to load a level in a multiplayer game. It makes sure no network messages are being processed while the level is being loaded. It also makes sure no messages are sent, until everything is ready. Lastly, when the level is loaded it sends a message to all scripts so that they know the level is loaded and can react to that. The SetLevelPrefix function helps with keeping unwanted networks updates out of a new loaded level. Unwanted updates might be updates from the previous level for example. The example also uses groups to separate game data and level load communication into groups. Group 0 is used for game data traffic and group 1 for level loading. Group 0 is blocked while the level is being loaded but group 1 kept open, it could also ferry chat communication so that can stay open during level loading.下面的例子是一个在多人游戏中加载关卡的简单途径。确认在加载关卡时没有网络信息被处理。也确认没有消息被发送,直到每件事情都准备好的。最后,当关卡加载完成,会发送一个消息到所有脚本,以便让这些脚本知晓关卡已经加载了,能够发挥作用了。SetLevelPrefix这个函数帮助避免和上一个关卡有关不必要的更新带入新的关卡。例如不必要的更新发生在上一个关卡。本例使用组来分离游戏数据和关卡加载的通信。0组被用来游戏数据传输,组1被用来做关卡加载。当关卡正在加载时组1保持打开组0被阻止,因此当关卡加载期间聊天通信依然可以保持通畅。var supportedNetworkLevels : String[] = [ "mylevel" ];
var disconnectedLevel : String = "loader";
private var lastLevelPrefix = 0;function Awake ()
{
// Network level loading is done in a separate channel.
//在单独的通道,网络关卡加载完成
DontDestroyOnLoad(this);
networkView.group = 1;
Application.LoadLevel(disconnectedLevel);
}function OnGUI ()
{
if (Network.peerType != NetworkPeerType.Disconnected)
{
  GUILayout.BeginArea(Rect(0, Screen.height - 30, Screen.width, 30));
  GUILayout.BeginHorizontal();  for (var level in supportedNetworkLevels)
  {
   if (GUILayout.Button(level))
   {
    Network.RemoveRPCsInGroup(0);
    Network.RemoveRPCsInGroup(1);
    networkView.RPC( "LoadLevel", RPCMode.AllBuffered, level, lastLevelPrefix + 1);
   }
  }
  GUILayout.FlexibleSpace();
  GUILayout.EndHorizontal();
  GUILayout.EndArea();
}
}@RPC
function LoadLevel (level : String, levelPrefix : int)
{
lastLevelPrefix = levelPrefix;  // There is no reason to send any more data over the network on the default channel,
  //在缺省通道不能发送任何数据
  // because we are about to load the level, thus all those objects will get deleted anyway
  //因为我们正在加载关卡,所有当前的对象都会被移除
  Network.SetSendingEnabled(0, false);   // We need to stop receiving because first the level must be loaded first.
  //我们需要停止接受操作,因为关卡首先需要完成加载
  // Once the level is loaded, rpc's and other state update attached to objects in the level are allowed to fire
  //当关卡完成加载后,rpc的状态更新和其他关卡中与对象绑定的状态更新才允许进行
  Network.isMessageQueueRunning = false;  // All network views loaded from a level will get a prefix into their NetworkViewID.
  //所有新加载关卡中的网络视图会添加一个前缀到他们的网络视图ID
  // This will prevent old updates from clients leaking into a newly created scene.
  //这会阻止来自其他客户端的旧的更新进入本地新的
  Network.SetLevelPrefix(levelPrefix);
  Application.LoadLevel(level);
  yield;
  yield;  // Allow receiving data again
  //允许再次接收数据
  Network.isMessageQueueRunning = ***e;
  // Now the level has been loaded and we can start sending out data to clients
  //现在关卡已经完成加载,我们能开始发送数据到其他客户端
  Network.SetSendingEnabled(0, ***e);
  for (var go in FindObjectsOfType(GameObject))
   go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);
}function OnDisconnectedFromServer ()
{
Application.LoadLevel(disconnectedLevel);
}@script RequireComponent(NetworkView)
【来源:互联网】
更多精彩教程,尽在web3D纳金网http://www.narkii.com/college/
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2024-11-23 09:45 , Processed in 0.104949 second(s), 32 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部