Advertisement
Guest User

UNetTest

a guest
May 3rd, 2017
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.64 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.Networking;
  4.  
  5. public class UNetTest : MonoBehaviour
  6. {
  7.     enum Status
  8.     {
  9.         Offline,
  10.         Online,
  11.         Connecting,
  12.     }
  13.  
  14.     private Status serverStatus = Status.Offline;
  15.  
  16.     void OnGUI()
  17.     {
  18.         using (new GUILayout.HorizontalScope())
  19.         {
  20.             GUI.enabled = serverStatus != Status.Online;
  21.             if (GUILayout.Button("Start Server"))
  22.             {
  23.                 StartServer();
  24.             }
  25.  
  26.             GUILayout.Label("Status: " + serverStatus.ToString());
  27.         }
  28.         GUI.enabled = true;
  29.         using (new GUILayout.HorizontalScope())
  30.         {
  31.             if (GUILayout.Button("Start Client"))
  32.             {
  33.                 StartClient();
  34.             }
  35.  
  36.             GUILayout.Label(string.Format("{0} clients connected", clientIDs.Count));
  37.         }
  38.     }
  39.  
  40.     static ConnectionConfig CreateConnectionConfig()
  41.     {
  42.         ConnectionConfig conConfig = new ConnectionConfig();
  43.  
  44.         conConfig.NetworkDropThreshold = 20;
  45.         conConfig.DisconnectTimeout = 5000;
  46.  
  47.         conConfig.AddChannel(QosType.ReliableSequenced);
  48.  
  49.         return conConfig;
  50.     }
  51.  
  52.     private const int port = 1234;
  53.  
  54.     void StartServer()
  55.     {
  56.         NetworkTransport.Init();
  57.  
  58.         HostTopology topology = new HostTopology(CreateConnectionConfig(), 3000);
  59.         serverHostID = NetworkTransport.AddHost(topology, port);
  60.  
  61.         serverStatus = Status.Online;
  62.  
  63.         InvokeRepeating("SendToClients", 0, 1);
  64.     }
  65.  
  66.     void SendToClients()
  67.     {
  68.         byte error = 0;
  69.         byte[] buffer = new byte[256];
  70.         foreach (int clientID in clientIDs)
  71.         {
  72.             NetworkTransport.Send(serverHostID, clientID, 0, buffer, buffer.Length, out error);
  73.         }
  74.     }
  75.  
  76.     private int serverHostID;
  77.  
  78.     private List<int> clientIDs = new List<int>();
  79.  
  80.     void StartClient()
  81.     {
  82.         NetworkTransport.Init();
  83.  
  84.         byte error;
  85.         int clientID = NetworkTransport.Connect(0, "127.0.0.1", port, 0, out error);
  86.  
  87.         Debug.LogFormat("Conn {0} err {1}", clientID, error);
  88.  
  89.         if (error == 0)
  90.         {
  91.             clientIDs.Add(clientID);
  92.         }
  93.     }
  94.  
  95.     void Update()
  96.     {
  97.         foreach (int clientID in clientIDs)
  98.             UpdateClient(clientID);
  99.     }
  100.  
  101.     void UpdateClient(int clientID)
  102.     {
  103.         int recHostId = serverHostID;
  104.         int connectionId = clientID;
  105.         int channelId = 0;
  106.         byte[] recBuffer = new byte[1024];
  107.         int bufferSize = 1024;
  108.         int dataSize;
  109.         byte error;
  110.         NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
  111.         switch (recData)
  112.         {
  113.             case NetworkEventType.Nothing: //1
  114.                 break;
  115.             case NetworkEventType.ConnectEvent: //2
  116.                 break;
  117.             case NetworkEventType.DataEvent: //3
  118.                 break;
  119.             case NetworkEventType.DisconnectEvent: //4
  120.                 break;
  121.         }
  122.  
  123.         if (recData != NetworkEventType.Nothing)
  124.             Debug.Log("Received " + recData);
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement