/* * This file is part of the Unity networking tutorial by M2H (http://www.M2H.nl) * The original author of this code Mike Hergaarden, even though some small parts * are copied from the Unity tutorials/manuals. * Feel free to use this code for your own projects, drop me a line if you made something exciting! */ #pragma strict #pragma implicit #pragma downcast var connectToIP : String = "127.0.0.1"; var connectPort : int = 25001; //Obviously the GUI is for both client&servers (mixed!) function OnGUI () { // 网络连接状态 == 断开 if (Network.peerType == NetworkPeerType.Disconnected){ //We are currently disconnected: Not a client or host GUILayout.Label("Connection status: Disconnected"); connectToIP = GUILayout.TextField(connectToIP, GUILayout.MinWidth(100)); connectPort = parseInt(GUILayout.TextField(connectPort.ToString())); GUILayout.BeginVertical(); // 作为客户端连接 if (GUILayout.Button ("Connect as client")) { //Connect to the "connectToIP" and "connectPort" as entered via the GUI //Ignore the NAT for now Network.useNat = false; Network.Connect(connectToIP, connectPort); // 连接服务器方法,可以省略掉服务器连接密码 } // 作为服务器端 if (GUILayout.Button ("Start Server")) { //Start a server for 32 clients using the "connectPort" given via the GUI //Ignore the nat for now Network.useNat = false; Network.InitializeServer(32, connectPort); // 第一个参数32是初始化这服务器能够最多连接倒的客户端数量,一但初始化后,连接数字可降,但是永远不可能超过这个数字 } GUILayout.EndVertical(); }else{ //We've got a connection(s)! // 连接中... 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] ) ); } else if (Network.peerType == NetworkPeerType.Server){ // 已经连接上,是作为服务器端 GUILayout.Label("Connection status: Server!"); GUILayout.Label("Connections: "+Network.connections.length); // 网络连接数显示 if(Network.connections.length>=1){ GUILayout.Label("Ping to first player: "+Network.GetAveragePing( Network.connections[0] ) ); // ping速度输出 } } if (GUILayout.Button ("Disconnect")) { Network.Disconnect(200); // 断开连接 } } } // NONE of the functions below is of any use in this demo, the code below is only used for demonstration. // First ensure you understand the code in the OnGUI() function above. //Client functions called by Unity // 客户端函数,当连接倒服务器能够触发 function OnConnectedToServer() { Debug.Log("This CLIENT has connected to a server该客户端已连接到服务器"); } // 从服务器上断开连接 function OnDisconnectedFromServer(info : NetworkDisconnection) { // 服务器端 if (Network.isServer) { Debug.Log("Local server connection disconnected本地服务器连接断开"); } else { // 客户端 // 信息 == 网络断线 if (info == NetworkDisconnection.LostConnection) { Debug.Log("Lost connection to the server失去连接到服务器"); } else { Debug.Log("Successfully diconnected from the server成功地从服务器断开连接"); } } } // 客户端尝试连接时由于某种原因失败。 function OnFailedToConnect(error: NetworkConnectionError){ Debug.Log("Could not connect to server无法连接到服务器: "+ error); } //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) { // 玩家ip地址 玩家端口 Debug.Log("Player disconnected from: " + player.ipAddress+":" + player.port); Network.RemoveRPCs(player); Network.DestroyPlayerObjects(player); } // OTHERS: // To have a full overview of all network functions called by unity // the next four have been added here too, but they can be ignored for now function OnFailedToConnectToMasterServer(info: NetworkConnectionError){ Debug.Log("Could not connect to master server无法连接到主服务器: "+ info); } // 当网络实例化 function OnNetworkInstantiate (info : NetworkMessageInfo) { Debug.Log("New object instantiated by新对象实例化 " + info.sender); } // 序列化网络视图 function OnSerializeNetworkView(stream : BitStream, info : NetworkMessageInfo) { //Custom code here (your code!) } /* The last networking functions that unity calls are the RPC functions. As we've added "OnSerializeNetworkView", you can't forget the RPC functions that unity calls..however; those are up to you to implement. @RPC function MyRPCKillMessage(){ //Looks like I have been killed! //Someone send an RPC resulting in this function call } */ //javascript/6742