找回密码
 立即注册
查看: 8197|回复: 75

[网络] Unity局域网创建network网络代码

[复制链接]
发表于 2012-12-18 22:32 | 显示全部楼层 |阅读模式

非常基础实用的代码,演示了如何快速建立并测试一个网络连接!
弄个 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:http://www.m2h.nl/unity/
转自:http://kasicass.blog.163.com/blog/static/3956192011126105440925/


发表于 2017-3-20 08:11 | 显示全部楼层
很不错
发表于 2017-3-20 07:52 | 显示全部楼层
顶顶多好
发表于 2017-3-20 07:27 | 显示全部楼层
难得一见的好帖
发表于 2017-3-20 08:03 | 显示全部楼层
说的非常好
发表于 2017-3-20 07:45 | 显示全部楼层
LZ真是人才
发表于 2017-7-5 08:32 | 显示全部楼层
很不错
发表于 2017-7-5 08:42 | 显示全部楼层
楼主是超人
发表于 2017-7-5 08:35 | 显示全部楼层
真心顶
发表于 2017-7-5 08:36 | 显示全部楼层
不错不错
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-11-22 06:53 , Processed in 0.110329 second(s), 27 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表