U侠 发表于 2012-12-18 22:42

Unity3D网络功能系列教程之一 创建连接

非常基础实用的教程,演示了如何快速建立并测试一个网络连接!
弄个 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));
}}if (GUILayout.Button("Disconnect")){Network.Disconnect(200);}}}// Server functions called by Unityfunction 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));}if (GUILayout.Button("Disconnect")){Network.Disconnect(200);}}}// Client functions called by Unityfunction 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/

fengyi 发表于 2013-2-2 13:14

我很懒,只想回复看看,楼主辛苦了!

limingjei 发表于 2013-5-15 17:57

感谢楼主分享了,刚接触这东西,试试好使不

voilin585 发表于 2013-11-8 21:29

只能说,好东西

wind9494 发表于 2016-7-31 03:01


不错 不错 不错

jxwan 发表于 2017-3-18 16:05

就需要补这方面

skyince 发表于 2017-5-2 16:42

很不错

wolfsky7 发表于 2017-5-2 16:11

楼主是超人

dlnuchunge 发表于 2017-5-2 16:24

顶顶多好

netghost147258 发表于 2017-5-2 16:04

真心顶
页: [1] 2 3 4 5 6 7 8
查看完整版本: Unity3D网络功能系列教程之一 创建连接