[JavaScript] Unity3d 网络游戏的网络连接方法 →→→→→进入此内容的聊天室

来自 , 2021-02-16, 写在 JavaScript, 查看 151 次.
URL http://www.code666.cn/view/46ba59a6
  1. /*
  2. *  This file is part of the Unity networking tutorial by M2H (http://www.M2H.nl)
  3. *  The original author of this code Mike Hergaarden, even though some small parts
  4. *  are copied from the Unity tutorials/manuals.
  5. *  Feel free to use this code for your own projects, drop me a line if you made something exciting!
  6. */
  7. #pragma strict
  8. #pragma implicit
  9. #pragma downcast
  10.  
  11. var connectToIP : String = "127.0.0.1";
  12. var connectPort : int = 25001;
  13.  
  14.  
  15. //Obviously the GUI is for both client&servers (mixed!)
  16. function OnGUI ()
  17. {
  18.          // 网络连接状态   ==  断开
  19.     if (Network.peerType == NetworkPeerType.Disconnected){
  20.     //We are currently disconnected: Not a client or host
  21.         GUILayout.Label("Connection status: Disconnected");
  22.          
  23.         connectToIP = GUILayout.TextField(connectToIP, GUILayout.MinWidth(100));
  24.         connectPort = parseInt(GUILayout.TextField(connectPort.ToString()));
  25.          
  26.         GUILayout.BeginVertical();
  27.         // 作为客户端连接
  28.         if (GUILayout.Button ("Connect as client"))
  29.         {
  30.             //Connect to the "connectToIP" and "connectPort" as entered via the GUI
  31.             //Ignore the NAT for now
  32.             Network.useNat = false;
  33.             Network.Connect(connectToIP, connectPort); // 连接服务器方法,可以省略掉服务器连接密码
  34.         }
  35.         // 作为服务器端
  36.         if (GUILayout.Button ("Start Server"))
  37.         {
  38.             //Start a server for 32 clients using the "connectPort" given via the GUI
  39.             //Ignore the nat for now    
  40.             Network.useNat = false;
  41.             Network.InitializeServer(32, connectPort); // 第一个参数32是初始化这服务器能够最多连接倒的客户端数量,一但初始化后,连接数字可降,但是永远不可能超过这个数字
  42.         }
  43.         GUILayout.EndVertical();
  44.          
  45.          
  46.     }else{
  47.         //We've got a connection(s)!
  48.          
  49.         // 连接中...
  50.         if (Network.peerType == NetworkPeerType.Connecting){
  51.          
  52.             GUILayout.Label("Connection status: Connecting");
  53.              
  54.         } else if (Network.peerType == NetworkPeerType.Client){
  55.         // 已经连接上,是作为客户端
  56.             GUILayout.Label("Connection status: Client!");
  57.             GUILayout.Label("Ping to server: "+Network.GetAveragePing(  Network.connections[0] ) );    
  58.              
  59.         } else if (Network.peerType == NetworkPeerType.Server){
  60.         // 已经连接上,是作为服务器端
  61.             GUILayout.Label("Connection status: Server!");
  62.             GUILayout.Label("Connections: "+Network.connections.length); // 网络连接数显示
  63.             if(Network.connections.length>=1){
  64.                 GUILayout.Label("Ping to first player: "+Network.GetAveragePing(  Network.connections[0] ) ); // ping速度输出
  65.             }          
  66.         }
  67.  
  68.         if (GUILayout.Button ("Disconnect"))
  69.         {
  70.             Network.Disconnect(200); // 断开连接
  71.         }
  72.     }
  73.      
  74.  
  75. }
  76.  
  77. // NONE of the functions below is of any use in this demo, the code below is only used for demonstration.
  78. // First ensure you understand the code in the OnGUI() function above.
  79.  
  80. //Client functions called by Unity
  81. // 客户端函数,当连接倒服务器能够触发
  82. function OnConnectedToServer() {
  83.     Debug.Log("This CLIENT has connected to a server该客户端已连接到服务器");  
  84. }
  85. // 从服务器上断开连接
  86. function OnDisconnectedFromServer(info : NetworkDisconnection) {
  87.     // 服务器端
  88.     if (Network.isServer)
  89.     {
  90.          
  91.         Debug.Log("Local server connection disconnected本地服务器连接断开");
  92.     }
  93.     else
  94.     {
  95.         // 客户端
  96.         // 信息 == 网络断线
  97.         if (info == NetworkDisconnection.LostConnection)
  98.         {
  99.             Debug.Log("Lost connection to the server失去连接到服务器");
  100.         }
  101.         else
  102.         {
  103.             Debug.Log("Successfully diconnected from the server成功地从服务器断开连接");
  104.         }
  105.     }
  106. }
  107. // 客户端尝试连接时由于某种原因失败。
  108. function OnFailedToConnect(error: NetworkConnectionError){
  109.     Debug.Log("Could not connect to server无法连接到服务器: "+ error);
  110. }
  111.  
  112.  
  113. //Server functions called by Unity 当玩家连接
  114. function OnPlayerConnected(player: NetworkPlayer) {
  115.     Debug.Log("Player connected from: " + player.ipAddress +":" + player.port);
  116. }
  117. // 当服务器初始化
  118. function OnServerInitialized() {
  119.     Debug.Log("Server initialized and ready服务器初始化和准备中");
  120. }
  121. // 服务器断触发函数 当一个客户端从服务器端断开时触发
  122. function OnPlayerDisconnected(player: NetworkPlayer) {
  123.     //                                          玩家ip地址             玩家端口
  124.     Debug.Log("Player disconnected from: " + player.ipAddress+":" + player.port);
  125.     Network.RemoveRPCs(player);
  126.     Network.DestroyPlayerObjects(player);
  127.      
  128. }
  129.  
  130.  
  131. // OTHERS:
  132. // To have a full overview of all network functions called by unity
  133. // the next four have been added here too, but they can be ignored for now
  134.  
  135. function OnFailedToConnectToMasterServer(info: NetworkConnectionError){
  136.     Debug.Log("Could not connect to master server无法连接到主服务器: "+ info);
  137. }
  138. // 当网络实例化
  139. function OnNetworkInstantiate (info : NetworkMessageInfo) {
  140.     Debug.Log("New object instantiated by新对象实例化 " + info.sender);
  141. }
  142. // 序列化网络视图
  143. function OnSerializeNetworkView(stream : BitStream, info : NetworkMessageInfo)
  144. {
  145.     //Custom code here (your code!)
  146. }
  147.  
  148. /*
  149.  The last networking functions that unity calls are the RPC functions.
  150.  As we've added "OnSerializeNetworkView", you can't forget the RPC functions
  151.  that unity calls..however; those are up to you to implement.
  152.  
  153.  @RPC
  154.  function MyRPCKillMessage(){
  155.     //Looks like I have been killed!
  156.     //Someone send an RPC resulting in this function call
  157.  }
  158. */
  159. //javascript/6742

回复 "Unity3d 网络游戏的网络连接方法"

这儿你可以回复上面这条便签

captcha