Hello, I am trying to send packets of data into unity via UDP. The goal is then to use this data for inputs to move a cube in the game. The packets are json files that follow the structure {“name”: (double), “name2”: (double2), etc}.
I am having trouble getting passed the UDPClient.Receive() method inside my ThreadMethod. I am wondering if this is a timing/buffer issue between unity and the data coming in (data is being sent at 50Hz if that helps). Please see attached what is returned on the console.
Please also see my code below this. I am very new to unity and would appreciate any help to get this .Receive() working. Thanks in advance.
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
public class Move : MonoBehaviour
{
// Initialise vector for the gameObject (avatar) to move on.
Vector3 Vec;
// Intitialisations UDP and data processing
//static UdpClient udp;
Thread thread;
//public string hostName = "127.0.0.1";
static readonly object lockObject = new object();
string returnData = "";
bool translateData = false;
// Start is called before the first frame update and is used to inititialise the UDP thread.
void Start()
{
thread = new Thread(new ThreadStart(ThreadMethod));
thread.Start();
}
// Update is called once per frame
public void Update()
{
if (translateData)
{
// Lock object so that multiple threads dont access data at the same time
lock (lockObject)
{
translateData = false;
UnityEngine.Debug.Log("Received: " + returnData);
Functions();
returnData = "";
}
}
}
// Handles the UDP communication from Host PC to Game PC
private void ThreadMethod()
{
//Creates a UdpClient for reading incoming data.
UdpClient udp = new UdpClient(36864);
UnityEngine.Debug.Log("Entering Thread method...");
while (true)
{
UnityEngine.Debug.Log("Updating byte array...");
Byte[] receiveBytes = new byte[0];
try
{
// The IPEndPoint will allow to read datagrams sent from any source.
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
UnityEngine.Debug.Log("Looking from any source...");
// Set up receiver
receiveBytes = udp.Receive(ref RemoteIpEndPoint);
UnityEngine.Debug.Log("receiving bytes...");
}
catch (Exception err)
{
UnityEngine.Debug.Log("UDP Client Socket Exception Error: " + err);
}
// Lock object so that multiple threads dont access data at the same time
lock (lockObject)
{
UnityEngine.Debug.Log("locked onto package...");
returnData = Encoding.ASCII.GetString(receiveBytes);
UnityEngine.Debug.Log("data is " + returnData.ToString());
handInfo items = JsonUtility.FromJson<handInfo>(returnData);
UnityEngine.Debug.Log("Array is " + items);
if (items != null) { translateData = true; }
else { UnityEngine.Debug.Log("Cannot reach data packages..."); }
}
}
}
// Class for displacement of each finger
[Serializable]
public class handInfo
{
public static double Pinky;
public static double Ring;
public static double Middle;
public static double Index;
public static double ThumbFlexExt;
public static double ThumbAddAbd;
//public static PlayerInfo CreateFromJSON(string jsonString)
//{
// return JsonUtility.FromJson<PlayerInfo>(jsonString);
//}
// Given JSON input:
// {"name":"Dr Charles","lives":3,"health":0.8}
// this example will return a PlayerInfo object with
// name == "Dr Charles", lives == 3, and health == 0.8f.
}
// Handles the movement associated with inputs
void Functions()
{
if (handInfo.Pinky >= 7)
{
moveUp();
}
if (handInfo.Pinky <= 3)
{
moveDown();
}
if (handInfo.ThumbAddAbd <= 3)
{
moveLeft();
}
if (handInfo.ThumbAddAbd >= 7)
{
moveRight();
}
}
public void moveRight()
{
Vec = transform.localPosition;
Vec.x += Time.deltaTime * 20;
transform.localPosition = Vec;
}
public void moveLeft()
{
Vec = transform.localPosition;
Vec.x -= Time.deltaTime * 20;
transform.localPosition = Vec;
}
public void moveUp()
{
Vec = transform.localPosition;
Vec.y += Time.deltaTime * 20;
transform.localPosition = Vec;
}
public void moveDown()
{
Vec = transform.localPosition;
Vec.y -= Time.deltaTime * 20;
transform.localPosition = Vec;
}
}