NetWorkServer.cs
1 using UnityEngine; 2 using System.Collections; 3 4 public class NetWorkServer : MonoBehaviour 5 { 6 // Use this for initialization 7 // 定义远程连接IP地址 8 private string remoteIP = "127.0.0.1"; 9 // 定义远程的端口号 10 private int remotePort = 10000; 11 // 定义本地侦听的端口号 12 private int listenPort = 10000; 13 // 限制连接数量为15个用户 14 private int connectCount = 15; 15 // 是否启用网络地址转换器 16 private bool useNAT = false; 17 18 void OnGUI () 19 { 20 switch (Network.peerType) { 21 case NetworkPeerType.Disconnected: 22 // 服务器未开启时,开启服务器 23 StartConnect (); 24 break; 25 case NetworkPeerType.Server: 26 // 成功连接服务器 27 OnServer (); 28 break; 29 case NetworkPeerType.Client: 30 break; 31 case NetworkPeerType.Connecting: 32 break; 33 } 34 } 35 36 void StartConnect () 37 { 38 remoteIP = GUI.TextField (new Rect (10, 30, 100, 20), remoteIP); 39 40 if (GUI.Button (new Rect (10, 50, 100, 30), "创建服务器")) { 41 42 Network.incomingPassword = "UnityNetwork"; 43 NetworkConnectionError error = Network.InitializeServer (connectCount, remotePort, useNAT); 44 Debug.Log (error); 45 } 46 } 47 48 void OnServer () 49 { 50 GUILayout.Label (" 服务端创建成功。等待连接····"); 51 52 // 得到的 IP 与端口 53 string ip = Network.player.ipAddress; 54 int port = Network.player.port; 55 GUILayout.Label (" ip地址:" + ip + ". 端口号码:" + port); 56 57 // 连接到服务器的所有客户端 58 int connectLength = Network.connections.Length; 59 60 // 遍历所有客户端并获取IP与端口号 61 for (int i = 0; i < connectLength; i++) { 62 GUILayout.Label (" 连接的IP:" + Network.connections [i].ipAddress); 63 GUILayout.Label (" 连接的端口:" + Network.connections [i].port); 64 } 65 66 if (GUI.Button (new Rect (10, 140, 100, 30), "断开连接")) { 67 // 从服务器上断开连接 68 Network.Disconnect (200); 69 } 70 } 71 72 void OnPlayerConnected (NetworkPlayer player) 73 { 74 75 GetComponent<NetworkView> ().RPC ("OnNetworkLoaded", RPCMode.Others, player); 76 77 } 78 79 void OnPlayerDisconnected (NetworkPlayer player) 80 { 81 Debug.Log ("清除对象: " + player); 82 Network.RemoveRPCs (player); 83 Network.DestroyPlayerObjects (player); 84 } 85 86 }
NetWorkColient.cs
1 using UnityEngine; 2 using System.Collections; 3 4 public class NetWorkColient : MonoBehaviour 5 { 6 7 // Use this for initialization 8 // 定义远程 IP 地址 9 private string remoteIP = "127.0.0.1"; 10 // 定义侦听端口 11 private int listenPort = 10000; 12 // 是否开启网络 IP 转换器 13 private bool useNAT = false; 14 15 private string ChatMessage = "Client Ready"; 16 17 void OnGUI () 18 { 19 20 switch (Network.peerType) { 21 case NetworkPeerType.Disconnected: 22 StartConnect (); 23 break; 24 case NetworkPeerType.Client: 25 ClientTo (); 26 break; 27 case NetworkPeerType.Connecting: 28 Debug.Log ("Connecting..."); 29 break; 30 } 31 } 32 33 void StartConnect () 34 { 35 if (GUI.Button (new Rect (10, 50, 100, 30), "Connect Server")) { 36 NetworkConnectionError error = Network.Connect (remoteIP, listenPort, "UnityNetwork"); 37 Debug.Log (error); 38 } 39 } 40 41 void ClientTo () 42 { 43 44 GUILayout.Label ("Connected"); 45 46 } 47 48 void OnConnectedToServer () 49 { 50 // 通知场景中的物体已经与服务端连接 51 foreach (GameObject go in FindObjectsOfType(typeof(GameObject))) { 52 go.SendMessage ("OnNetworkLoaded", SendMessageOptions.DontRequireReceiver); 53 } 54 55 } 56 57 void OnDisconnectedFromServer (NetworkDisconnection info) 58 { 59 60 Network.RemoveRPCs (Network.player); 61 Network.DestroyPlayerObjects (Network.player); 62 Application.LoadLevel (Application.loadedLevel); 63 64 } 65 66 }