Hey all ![]()
I’m currently using the scripts below (found somewhere on here or somewhere haha) to send data via UDP from Max/MSP to Unity and its working perfectly.
However, i now need it to send data back to Max/MSP. What modifications do i need to do to the code to do that? I’m guessing its not so simple as to change all the words “Read” / “Receive” to “Write” / “Send” but other UDP send scripts I’ve tried, communicate with Max but I get an error that end saying the message is basically in the wrong format.
I just need the reverse of what the UDPReceive and Read scripts do, as they’re working. I’m a total newbie to Unity and C# so kind of stuck filling in many blanks in my knowledge and don’t fully understand what i’m doing so any help would be greatly appreciated.
Thanks,
Paul…
UDPReceive.cs (attached to empty “MaxSendRev” object)
using UnityEngine;
using System.Collections;
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using System.Threading;
public class UdpReceive : MonoBehaviour
{
public int port = 2002;
private UdpClient client;
private IPEndPoint RemoteIpEndPoint;
private Thread t_udp;
public float[] maxValues;
void Start()
{
client = new UdpClient(port);
RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
t_udp = new Thread(new ThreadStart(UDPRead));
t_udp.Name = "UDP thread";
t_udp.Start();
//FilterData(test);
}
public void UDPRead()
{
while (true)
{
try
{
//Debug.Log("listening UDP port " + port);
byte[] receiveBytes = client.Receive(ref RemoteIpEndPoint);
string returnData = Encoding.ASCII.GetString(receiveBytes);
// parsing //
FilterData(returnData);
}
catch (Exception e)
{
Debug.Log("Not so good " + e.ToString());
}
Thread.Sleep(20);
}
}
void OnDisable()
{
if (t_udp != null) t_udp.Abort();
client.Close();
}
public float MaxValue(int index)
{
return maxValues[index];
}
public void FilterData(string dataString)
{
string[] splitString = dataString.Split(":"[0]);
maxValues = new float[splitString.Length];
for( int i=0; i<maxValues.Length; i++ ) {
maxValues[i] = float.Parse(splitString[i]);
}
}
}
Read.cs (attached to “Pivot” parent of Handle)
using UnityEngine;
using System.Collections;
public class Read : MonoBehaviour {
private UdpReceive udpRec;
public GameObject PivotX;
public GameObject HandleX;
public GameObject ZombieX;
void Start ()
{
Application.runInBackground = true;
//using find //
// THIS FIND A SPECIFIC GAME OBJECT THAT THE RECEIVE SCRIPT IS ATTACHED TO BUT THE ACTUAL MANIPULATION HAPPENS
// TO AN OBJECT THAT THE READ SCRIPT IS ATTACHED TO..
udpRec = GameObject.Find("MaxSendRev").GetComponent(typeof(UdpReceive)) as UdpReceive;
// FIND THE RELEVANT GAMEOBJECTS..
PivotX = GameObject.Find ("Pivot");
HandleX = GameObject.Find ("Handle");
}
// Update is called once per frame
void Update ()
{
if (udpRec.maxValues.Length <= 0) {
return;
}
// COORDS TO ROTATE THE HANDLE..
float rotHX = ClampAngle(udpRec.MaxValue(0));
float rotHY = ClampAngle(udpRec.MaxValue(1));
float rotHZ = ClampAngle(udpRec.MaxValue(2));
// COORDS TO ROTATE THE DOOR..
float rotDX = ClampAngle(udpRec.MaxValue(3));
float rotDY = ClampAngle(udpRec.MaxValue(4));
float rotDZ = ClampAngle(udpRec.MaxValue(5));
// ROTATE THE HANDLE..
HandleX.transform.localEulerAngles = new Vector3(rotHX, rotHY, rotHZ);
// ROTATE THE DOOR..
PivotX.transform.localEulerAngles = new Vector3(rotDX, rotDY, rotDZ);
}
public static float ClampAngle(float angle)
{
if(angle < -360f)
angle += 360f;
if(angle > 360)
angle -= 360;
return Mathf.Clamp(angle, -360f, 360f);
}
}