Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Net;
- using System.Net.Sockets;
- using System.IO;
- using System;
- using System.Text;
- using System.Threading;
- using UnityEngine;
- public class SpaceShip
- {
- private GameObject ship;
- public SpaceShip()
- {
- ship = null;
- ship = GameObject.Find("Cube");
- }
- public bool isReady()
- {
- return ship != null;
- }
- public void UpatePosition(float x, float y, float z)
- {
- ship.transform.position = new Vector3(x, y, z);
- }
- void UpdateRotation(float a, float b, float j)
- {
- ship.transform.rotation = Quaternion.Euler(a, b, j);
- }
- public Vector3 GetPos()
- {
- return ship.transform.position;
- }
- }
- public class ServerTCP {
- IPAddress Ip = IPAddress.Parse("127.0.0.1");
- int Port = 9090;
- NetworkStream Stream;
- TcpListener Server;
- TcpClient Client;
- Boolean Ready = false;
- float[] Data = new float[2];
- Thread thread;
- public ServerTCP() {
- thread = new Thread ( () => {
- try {
- Server = new TcpListener (Ip, Port);
- Server.Start();
- Debug.Log("Ожидание подключений... ");
- Client = Server.AcceptTcpClient();
- Debug.Log("Подключен клиент. Выполнение запроса...");
- Stream = Client.GetStream();
- Ready = true;
- while(true) {
- Read();
- }
- } catch (Exception e)
- {
- Debug.Log("Socket error: " + e);
- }
- });
- thread.Start ();
- }
- public Boolean IsReady() {
- return Ready;
- }
- public float[] GetData() {
- return Data;
- }
- public void Close() {
- thread.Abort ();
- if (Stream != null) {
- Stream.Close ();
- }
- if (Client != null) {
- Client.Close ();
- }
- if (Server != null) {
- Server.Stop ();
- }
- }
- void Read()
- {
- Byte[] bytes = new Byte[4];
- for (int i = 0; i < 2; ++i) {
- Stream.Read (bytes, 0, bytes.Length);
- Data[i] = BitConverter.ToSingle(bytes, 0);
- //Debug.Log(data[i]);
- }
- }
- }
- public class test_1 : MonoBehaviour {
- ServerTCP Server;
- SpaceShip Ship;
- GameObject Particle;
- float x, y, z = 0;
- void Start () {
- Server = new ServerTCP();
- Ship = new SpaceShip();
- Particle = GameObject.Find("Particle System");
- }
- void Update () {
- if (Server.IsReady ()) {
- float[] data = Server.GetData();
- x = data [0];
- y = data [1];
- Ship.UpatePosition(x, y, z);
- Camera.main.transform.position = new Vector3(x - 4.13f, y + 3.52f, z - -1.06f);
- Particle.transform.position = new Vector3(x - 2f, y, z -0.75f);
- }
- }
- void OnApplicationQuit() {
- Server.Close ();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement