Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System;
- using System.Collections;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- public class SocketClient : MonoBehaviour
- {
- // Use this for initialization
- public GameObject Target;
- private float xPos, yPos;
- public Vector2 Scale = new Vector2(1f, 1f); //MAKE SURE THIS IS NOT (0f,0f), OTHERWISE THE x and y COORDINATES WILL BE MULTIPLIED BY 0
- public Vector2 Offset = Vector2.zero;
- Thread receiveThread;
- UdpClient client;
- private int port = 5065;
- //info
- private string lastReceivedUDPPacket = "";
- private string allReceivedUDPPackets = "";
- void Start()
- {
- init();
- }
- void OnGUI()
- {
- Rect rectObj = new Rect(40, 10, 200, 400);
- GUIStyle style = new GUIStyle();
- style.alignment = TextAnchor.UpperLeft;
- GUI.Box(rectObj, "# UDPReceive\n127.0.0.1 " + port + " #\n"
- //+ "shell> nc -u 127.0.0.1 : "+port +" \n"
- + "\nLast Packet: \n" + lastReceivedUDPPacket
- //+ "\n\nAll Messages: \n"+allReceivedUDPPackets
- , style);
- }
- private void init()
- {
- print("UPDSend.init()");
- port = 5065;
- print("Sending to 127.0.0.1 : " + port);
- receiveThread = new Thread(new ThreadStart(ReceiveData));
- receiveThread.IsBackground = true;
- receiveThread.Start();
- }
- private void ReceiveData()
- {
- client = new UdpClient(port);
- while (true)
- {
- try
- {
- IPEndPoint anyIP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port);
- byte[] data = client.Receive(ref anyIP);
- string text = Encoding.UTF8.GetString(data);
- print(">> " + text);
- lastReceivedUDPPacket = text;
- allReceivedUDPPackets = allReceivedUDPPackets + text;
- xPos = GetX(text) * Scale.x;
- yPos = GetY(text) * Scale.y;
- }
- catch (Exception e)
- {
- print(e.ToString());
- }
- }
- }
- public string getLatestUDPPacket()
- {
- allReceivedUDPPackets = "";
- return lastReceivedUDPPacket;
- }
- //New funcitons ################################################################################################################
- float GetX(string txt)
- {
- string s = "";
- for (int i = 0; i < txt.Length && txt[i] != ','; i++)
- s += txt[i];
- return float.Parse(s);
- }
- float GetY(string txt)
- {
- string s = "";
- int i = 0;
- while (txt[i] != ',')
- i++;
- i++; //txt[i] is the first digit of the y coordinate now
- while (i < txt.Length)
- {
- s += txt[i];
- i++;
- }
- return float.Parse(s);
- }
- //###############################################################################################################################
- // Update is called once per frame
- void Update()
- {
- Target.transform.localPosition = new Vector2(xPos-Camera.main.pixelWidth/2 + Offset.x, yPos - Camera.main.pixelHeight/2 + Offset.y);
- }
- void OnApplicationQuit()
- {
- if (receiveThread != null)
- {
- receiveThread.Abort();
- Debug.Log(receiveThread.IsAlive); //must be false
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement