-
Notifications
You must be signed in to change notification settings - Fork 0
/
udp_receiver.cs
58 lines (51 loc) · 1.63 KB
/
udp_receiver.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//The following code can be used to receive pose data from udp_emitter.py and use it to track an object in unity
using UnityEngine;
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
public class udp_tracked_object : MonoBehaviour {
Thread receiveThread;
UdpClient client;
private Double[] float_array;
private int port = 8051;
// Use this for initialization
void Start () {
float_array = new Double[7];
receiveThread = new Thread(new ThreadStart(ReceiveData));
receiveThread.IsBackground = true;
receiveThread.Start();
}
// Update is called once per frame
void Update () {
transform.position = new Vector3((float) float_array[0], (float)float_array[1], (float)float_array[2]);
transform.rotation = new Quaternion((float)float_array[3], (float)float_array[4], (float)float_array[5], (float)float_array[6]);
}
void OnApplicationQuit()
{
if (receiveThread != null)
receiveThread.Abort();
client.Close();
}
// receive thread
private void ReceiveData()
{
port = 8051;
client = new UdpClient(port);
print("Starting Server");
while (true)
{
try
{
IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
byte[] data = client.Receive(ref anyIP);
for (int i = 0; i < data.Length; i++)
float_array[i] = BitConverter.ToDouble(data, i * 8);
}
catch (Exception err)
{
print(err.ToString());
}
}
}
}