注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Code@Pig Home

喜欢背着一袋Code傻笑的Pig .. 忧美.欢笑.记忆.忘却 .之. 角落

 
 
 

日志

 
 

[unity3d] Network (1) -- Make Connection!  

2011-02-26 10:54:40|  分类: 3d_unity |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
例子的源文件:

弄个 Server.js 挂到 场景一:
---------------------------------------------------------------------------------------------------------
var connectToIP : String = "127.0.0.1";
var connectPort : int = 2224;

function OnGUI ()
{
  if (Network.peerType == NetworkPeerType.Disconnected)
  {
GUILayout.Label("Connection status: Disconnected");
connectToIP = GUILayout.TextField(connectToIP, GUILayout.MinWidth(100));
connectPort = parseInt(GUILayout.TextField(connectPort.ToString()));
  if (GUILayout.Button("Start Server"))
  {
  Network.InitializeServer(32, connectPort, false);
  }
    }
  else
  {
  if (Network.peerType == NetworkPeerType.Server)
  {
  GUILayout.Label("Connection status: Server!");
  GUILayout.Label("Connection: " + Network.connections.length);
  if (Network.connections.length >= 1)
  {
     GUILayout.Label("Ping to first player: " + Network.GetAveragePing(Network.connections[0]));
  }
  }
  if (GUILayout.Button("Disconnect"))
  {
  Network.Disconnect(200);
  }
  }
}

// Server functions called by Unity
function OnPlayerConnected(player: NetworkPlayer) {
  Debug.Log("Player connected from: " + player.ipAddress + ":" + player.port);
}

function OnServerInitialized() {
  Debug.Log("Server initialized and ready");
}

function OnPlayerDisconnected(player: NetworkPlayer) {
  Debug.Log("Player disconnected from: " + player.ipAddress + ":" + player.port);
}
---------------------------------------------------------------------------------------------------------

弄个 Client.js 挂到场景二:
---------------------------------------------------------------------------------------------------------

var connectToIP : String = "127.0.0.1";
var connectPort : int = 2224;

function OnGUI ()
{
  if (Network.peerType == NetworkPeerType.Disconnected)
  {
  GUILayout.Label("Connection status: Disconnected");
  connectToIP = GUILayout.TextField(connectToIP, GUILayout.MinWidth(100));
  connectPort = parseInt(GUILayout.TextField(connectPort.ToString()));
  if (GUILayout.Button("Connect as client"))
  {
  Network.Connect(connectToIP, connectPort);
  }
  }
  else
  {
  if (Network.peerType == NetworkPeerType.Connecting)
  {
  GUILayout.Label("Connection status: Connecting");
  }
  else if (Network.peerType == NetworkPeerType.Client)
  {
  GUILayout.Label("Connection status: Client!");
  GUILayout.Label("Ping to server: " + Network.GetAveragePing(Network.connections[0]));
  }
  if (GUILayout.Button("Disconnect"))
  {
  Network.Disconnect(200);
  }
  }
}

// Client functions called by Unity
function OnConnectedToServer()
{
  Debug.Log("This CLIENT has connected to a server");
}

function OnDisconnectedFromServer(info : NetworkDisconnection)
{
  Debug.Log("This SERVER OR CLIENT has disconnected from a server");
}

function OnFailedToConnect(error: NetworkConnectionError)
{
  Debug.Log("Could not connect to server: " + error);
}
---------------------------------------------------------------------------------------------------------

把 ClientScene 放到 webplayer 上,然后 ServerScene 在 unity editor 里面直接跑。然后就可以本机测试了。
在 ServerScene 跑之前,设置 Edit => Project Settings => Player 中的 Run In Background。因为默认处于 inactive window 状态的 Unity程序会去 sleep,如果不设置 "Run In Background",则在操作 ClientScene 时,你会发现连接不上服务器,但点一下 ServerScene,然后再看 ClientScene 就连接上了。

Unity 对网络事件的封装还是很到位的。
不过目前看起来,其不允许你用 Unity 的客户端连接自己的服务器。这个还要继续研究。

例子来源于 M2H:

2011-05-06
自己打造 u3d 网络层,给 u3d 客户端写个插件(需要Pro版支持),然后服务端爱咋整就咋整。
参考例子:
  评论这张
 
阅读(4194)| 评论(2)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017