Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.62 KB | None | 0 0
  1. using System.Net;
  2. using System.Net.Sockets;
  3. using System.IO;
  4. using System;
  5. using System.Text;
  6. using System.Threading;
  7.  
  8. using UnityEngine;
  9.  
  10. public class SpaceShip
  11. {
  12.     private GameObject ship;
  13.  
  14.     public SpaceShip()
  15.     {
  16.         ship = null;
  17.         ship = GameObject.Find("Cube");
  18.     }
  19.  
  20.     public bool isReady()
  21.     {
  22.         return ship != null;
  23.     }
  24.  
  25.     public void UpatePosition(float x, float y, float z)
  26.     {
  27.         ship.transform.position = new Vector3(x, y, z);
  28.     }
  29.  
  30.     void UpdateRotation(float a, float b, float j)
  31.     {
  32.         ship.transform.rotation = Quaternion.Euler(a, b, j);
  33.     }
  34.  
  35.     public Vector3 GetPos()
  36.     {
  37.         return ship.transform.position;
  38.     }
  39. }
  40.    
  41. public class ServerTCP {
  42.  
  43.     IPAddress Ip = IPAddress.Parse("127.0.0.1");
  44.     int Port = 9090;
  45.  
  46.     NetworkStream Stream;
  47.     TcpListener Server;
  48.     TcpClient Client;
  49.     Boolean Ready = false;
  50.  
  51.     float[] Data = new float[2];
  52.     Thread thread;
  53.  
  54.     public ServerTCP() {
  55.         thread = new Thread ( () => {
  56.             try {
  57.                 Server = new TcpListener (Ip, Port);
  58.                 Server.Start();
  59.  
  60.                 Debug.Log("Ожидание подключений... ");
  61.  
  62.                 Client = Server.AcceptTcpClient();
  63.                 Debug.Log("Подключен клиент. Выполнение запроса...");
  64.  
  65.                 Stream = Client.GetStream();
  66.                 Ready = true;
  67.  
  68.                 while(true) {
  69.                     Read();
  70.                 }
  71.  
  72.             } catch (Exception e)
  73.             {
  74.                 Debug.Log("Socket error: " + e);
  75.             }
  76.  
  77.         });
  78.  
  79.         thread.Start ();
  80.     }
  81.  
  82.     public Boolean IsReady() {
  83.         return Ready;
  84.     }
  85.  
  86.     public float[] GetData() {
  87.         return Data;
  88.     }
  89.  
  90.     public void Close() {
  91.  
  92.         thread.Abort ();
  93.  
  94.         if (Stream != null) {
  95.             Stream.Close ();
  96.         }
  97.  
  98.         if (Client != null) {
  99.             Client.Close ();
  100.         }
  101.  
  102.         if (Server != null) {
  103.             Server.Stop ();
  104.         }
  105.    
  106.     }
  107.  
  108.     void Read()
  109.     {
  110.         Byte[] bytes = new Byte[4];
  111.  
  112.         for (int i = 0; i < 2; ++i) {
  113.             Stream.Read (bytes, 0, bytes.Length);
  114.             Data[i] = BitConverter.ToSingle(bytes, 0);
  115.             //Debug.Log(data[i]);
  116.         }
  117.  
  118.     }
  119. }
  120.  
  121. public class test_1 : MonoBehaviour {
  122.  
  123.     ServerTCP Server;
  124.     SpaceShip Ship;
  125.     GameObject Particle;
  126.  
  127.     float x, y, z = 0;
  128.  
  129.     void Start () {
  130.         Server = new ServerTCP();
  131.         Ship = new SpaceShip();
  132.         Particle = GameObject.Find("Particle System");
  133.     }
  134.  
  135.     void Update () {
  136.         if (Server.IsReady ()) {
  137.             float[] data = Server.GetData();
  138.             x = data [0];
  139.             y = data [1];
  140.  
  141.             Ship.UpatePosition(x, y, z);
  142.  
  143.             Camera.main.transform.position = new Vector3(x - 4.13f, y + 3.52f, z - -1.06f);
  144.             Particle.transform.position = new Vector3(x - 2f, y, z -0.75f);
  145.         }
  146.     }
  147.  
  148.     void OnApplicationQuit() {
  149.         Server.Close ();
  150.     }
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement