Advertisement
Tony999

Untitled

Oct 20th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading;
  8.  
  9. public class SocketClient : MonoBehaviour
  10. {
  11.  
  12. // Use this for initialization
  13.  
  14. public GameObject Target;
  15. private float xPos, yPos;
  16. 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
  17. public Vector2 Offset = Vector2.zero;
  18.  
  19. Thread receiveThread;
  20. UdpClient client;
  21. private int port = 5065;
  22.  
  23. //info
  24.  
  25. private string lastReceivedUDPPacket = "";
  26. private string allReceivedUDPPackets = "";
  27.  
  28. void Start()
  29. {
  30. init();
  31. }
  32.  
  33. void OnGUI()
  34. {
  35. Rect rectObj = new Rect(40, 10, 200, 400);
  36.  
  37. GUIStyle style = new GUIStyle();
  38.  
  39. style.alignment = TextAnchor.UpperLeft;
  40.  
  41. GUI.Box(rectObj, "# UDPReceive\n127.0.0.1 " + port + " #\n"
  42.  
  43. //+ "shell> nc -u 127.0.0.1 : "+port +" \n"
  44.  
  45. + "\nLast Packet: \n" + lastReceivedUDPPacket
  46.  
  47. //+ "\n\nAll Messages: \n"+allReceivedUDPPackets
  48.  
  49. , style);
  50.  
  51. }
  52.  
  53. private void init()
  54. {
  55. print("UPDSend.init()");
  56.  
  57. port = 5065;
  58.  
  59. print("Sending to 127.0.0.1 : " + port);
  60.  
  61. receiveThread = new Thread(new ThreadStart(ReceiveData));
  62. receiveThread.IsBackground = true;
  63. receiveThread.Start();
  64.  
  65. }
  66.  
  67. private void ReceiveData()
  68. {
  69. client = new UdpClient(port);
  70. while (true)
  71. {
  72. try
  73. {
  74. IPEndPoint anyIP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port);
  75. byte[] data = client.Receive(ref anyIP);
  76.  
  77. string text = Encoding.UTF8.GetString(data);
  78. print(">> " + text);
  79. lastReceivedUDPPacket = text;
  80. allReceivedUDPPackets = allReceivedUDPPackets + text;
  81.  
  82. xPos = GetX(text) * Scale.x;
  83. yPos = GetY(text) * Scale.y;
  84.  
  85. }
  86. catch (Exception e)
  87. {
  88. print(e.ToString());
  89. }
  90. }
  91. }
  92.  
  93. public string getLatestUDPPacket()
  94. {
  95. allReceivedUDPPackets = "";
  96. return lastReceivedUDPPacket;
  97. }
  98.  
  99. //New funcitons ################################################################################################################
  100. float GetX(string txt)
  101. {
  102. string s = "";
  103. for (int i = 0; i < txt.Length && txt[i] != ','; i++)
  104. s += txt[i];
  105. return float.Parse(s);
  106. }
  107.  
  108. float GetY(string txt)
  109. {
  110. string s = "";
  111. int i = 0;
  112. while (txt[i] != ',')
  113. i++;
  114. i++; //txt[i] is the first digit of the y coordinate now
  115.  
  116. while (i < txt.Length)
  117. {
  118. s += txt[i];
  119. i++;
  120. }
  121. return float.Parse(s);
  122. }
  123. //###############################################################################################################################
  124.  
  125. // Update is called once per frame
  126. void Update()
  127. {
  128. Target.transform.localPosition = new Vector2(xPos-Camera.main.pixelWidth/2 + Offset.x, yPos - Camera.main.pixelHeight/2 + Offset.y);
  129. }
  130.  
  131. void OnApplicationQuit()
  132. {
  133. if (receiveThread != null)
  134. {
  135. receiveThread.Abort();
  136. Debug.Log(receiveThread.IsAlive); //must be false
  137. }
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement