Advertisement
Tony999

Untitled

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