非常基础实用的代码,演示了如何快速建立并测试一个网络连接!
弄个 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/
|