Advertisement
Tony999

Untitled

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