Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.37 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using UnityEngine;
  5. using UnityEngine.Networking;
  6. using UnityEngine.UI;
  7.  
  8. public class Player
  9. {
  10.     public string playerName;
  11.     public GameObject avatar;
  12.     public int connectionId;
  13. }
  14.  
  15. public class NetworkManagerClient : MonoBehaviour
  16. {
  17.  
  18.     const int MAX_CONNECTIONS = 20;
  19.  
  20.     int hostId;
  21.     int ourClientId;
  22.     int connectionId;
  23.     int myReliableChannelId;
  24.     int myUnreliableChannelId;
  25.  
  26.     int port = 5701;
  27.  
  28.     float connectionTime;
  29.  
  30.     string playerName;
  31.  
  32.     bool isConnected = false;
  33.     bool isStarted = false;
  34.     byte error;
  35.  
  36.     public GameObject playerPrefab;
  37.     Dictionary<int, Player> players = new Dictionary<int, Player>();
  38.  
  39.     // Use this for initialization
  40.     public void Connect()
  41.     {
  42.         string pName = GameObject.Find("ClientName").GetComponent<InputField>().text;
  43.         if (pName == "")
  44.         {
  45.             Debug.Log("Player must enter a name!");
  46.             return;
  47.         }
  48.  
  49.         playerName = pName;
  50.  
  51.         NetworkTransport.Init();
  52.  
  53.         ConnectionConfig config = new ConnectionConfig();
  54.         myReliableChannelId = config.AddChannel(QosType.Reliable);
  55.         myUnreliableChannelId = config.AddChannel(QosType.Unreliable);
  56.  
  57.         HostTopology topology = new HostTopology(config, MAX_CONNECTIONS);
  58.  
  59.         hostId = NetworkTransport.AddHost(topology, 0);
  60.         connectionId = NetworkTransport.Connect(hostId, "127.0.0.1", port, 0, out error);
  61.  
  62.         connectionTime = Time.time;
  63.         isConnected = true;
  64.     }
  65.  
  66.     void Update()
  67.     {
  68.         if (!isConnected)
  69.             return;
  70.  
  71.         int recHostId;
  72.         int connectionId;
  73.         int channelId;
  74.         byte[] recBuffer = new byte[1024];
  75.         int bufferSize = 1024;
  76.         int dataSize;
  77.         byte error;
  78.         NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
  79.         switch (recData)
  80.         {
  81.             case NetworkEventType.DataEvent:
  82.                 string msg = Encoding.Unicode.GetString(recBuffer, 0, dataSize);
  83.                 Debug.Log("Receiving: " + msg);
  84.  
  85.                 string[] splitData = msg.Split('|');
  86.  
  87.                 switch (splitData[0])
  88.                 {
  89.                     case "ASKNAME":
  90.                         OnAskName(splitData);
  91.                         break;
  92.  
  93.                     case "CNN":
  94.                         SpawnPlayer(splitData[1], int.Parse(splitData[2]));
  95.                         break;
  96.  
  97.                     case "DC":
  98.                         PlayerDisconnected(int.Parse(splitData[1]));
  99.                         break;
  100.  
  101.                     case "ASKPOSITION":
  102.                         OnAskPosition(splitData);
  103.                         break;
  104.  
  105.                     default:
  106.                         Debug.Log("Invalid message: " + msg);
  107.                         break;
  108.                 }
  109.  
  110.                 break;
  111.         }
  112.     }
  113.  
  114.     void OnAskName(string[] data)
  115.     {
  116.         //Set client's ID
  117.         ourClientId = int.Parse(data[1]);
  118.  
  119.         //send our name to the server
  120.         Send("NAMEIS|" + playerName, myReliableChannelId);
  121.  
  122.         //create all the other players
  123.         for (int i = 2; i < data.Length - 1; i++)
  124.         {
  125.             string[] d = data[i].Split('%');
  126.             SpawnPlayer(d[0], int.Parse(d[1]));
  127.         }
  128.     }
  129.  
  130.     void SpawnPlayer(string playerName, int cnnId)
  131.     {
  132.         GameObject go = Instantiate(playerPrefab) as GameObject;
  133.  
  134.         //Is this ours?
  135.         if (cnnId == ourClientId)
  136.         {
  137.             //Add mobility
  138.             go.AddComponent<PlayerMovement>();
  139.             //Remove Canvas
  140.             isStarted = true;
  141.             GameObject.Find("Canvas").SetActive(false);
  142.         }
  143.  
  144.         Player p = new Player();
  145.         p.avatar = go;
  146.         p.playerName = playerName;
  147.         p.connectionId = cnnId;
  148.         p.avatar.GetComponentInChildren<TextMesh>().text = playerName;
  149.         players.Add(cnnId, p);
  150.     }
  151.  
  152.     void Send(string message, int channelId)
  153.     {
  154.         Debug.Log("Sending: " + message);
  155.         byte[] msg = Encoding.Unicode.GetBytes(message);
  156.         NetworkTransport.Send(hostId, connectionId, channelId, msg, message.Length * sizeof(char), out error);
  157.     }
  158.  
  159.     private void PlayerDisconnected(int cnnId)
  160.     {
  161.         Destroy(players[cnnId].avatar);
  162.         players.Remove(cnnId);
  163.     }
  164.  
  165.     void OnAskPosition(string[] data)
  166.     {
  167.         if (!isStarted)
  168.             return;
  169.        
  170.         //Update everyone else
  171.         for (int i = 1; i < data.Length; i++)
  172.         {
  173.             string[] d = data[i].Split('%');
  174.  
  175.             //Prevent the server form updating us
  176.             if (ourClientId != int.Parse(d[0]))
  177.             {
  178.                 Vector3 position = Vector3.zero;
  179.                 position.x = float.Parse(d[1]);
  180.                 position.y = float.Parse(d[2]);
  181.                 //position.z = float.Parse(d[3]);
  182.                 players[int.Parse(d[0])].avatar.transform.position = position;
  183.             }
  184.         }
  185.  
  186.         //Send our own position
  187.         Vector3 myPosition = players[ourClientId].avatar.transform.position;
  188.         string m = "MYPOSITION|" + myPosition.x.ToString() + "|" + myPosition.y.ToString();
  189.         Send(m, myUnreliableChannelId);
  190.     }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement