Question

I'm trying to utilize a library for Unity called UnityOSC, which allows you to receive OSC messages, presumably, from other applications. I have an application which transmits data via UDP at the following address:

host: 127.0.0.1 port: 33433

I now need to configure UnityOSC to listen for that data. The library has a script called OSCHandler.cs, and in the init function, you can set up your client and server. I'm just not sure how to set this up.

Currently, I'm attempting this:

public void Init()
    {
        CreateClient("FaceClient", IPAddress.Parse("127.0.0.1"), 33433);    
        CreateServer("FaceServer", 6666); 
    }

I've matched the client parameters to those of the application transmiting the data, and the server is just random - but honestly I'm not sure what to put in either one of these. Theoretically, if I set the client / server up properly, I should be able to register information in my update function like this:

void Update() {

    OSCHandler.Instance.UpdateLogs();
    //clients = OSCHandler.Instance.Clients;
    servers = OSCHandler.Instance.Servers;

    foreach(KeyValuePair<string, ServerLog> item in servers)
    {
        // If we have received at least one packet,
        // show the last received from the log in the Debug console
        if(item.Value.log.Count > 0) 
        {
            int lastPacketIndex = item.Value.packets.Count - 1;

            UnityEngine.Debug.Log(String.Format("SERVER: {0} ADDRESS: {1} VALUE 0: {2}", 
            item.Key, // Server name
            item.Value.packets[lastPacketIndex].Address, // OSC address
            item.Value.packets[lastPacketIndex].Data[0].ToString())); //First data value
        }
    }
}

But so far nothing is registering to the debugger. Any idea what I'm doing wrong?

Was it helpful?

Solution 2

I went a different route to get this working in Unity - based off of an older project called openTSPS from James George. You can download it here - it accepts all OSC data:

https://github.com/heaversm/UnityOSC_FS

OTHER TIPS

Two things, keeping in mind I did this from the command line and not in the Unity editor so things might be different.

First, line 50 in OSCPacket.cs throws an exception in the command line version. Not sure if Unity picks up Trace.Assert or not but nothing is hurt by removing the line:

Trace.Assert(string.IsNullOrEmpty(_address) == false);

I'm guessing the UnityOSC author meant to do something like:

Trace.Assert(string.IsNullOrEmpty(value) == false);

Otherwise I'm not sure how that assertion would pass since OSCMessage, a derived class of OSCPacket, directly calls the Address property in its ctor.

Secondly, if you're trying to test this on a local machine, your ports need to match:

    public void Init()
    {
        CreateServer("Server", 5555);
        CreateClient("Client", IPAddress.Parse("127.0.0.1"), 5555);
    }

Lastly, my manual creation:

        OSCHandler handler = new OSCHandler();
        handler.Init();
        handler.SendMessageToClient<string>("Client", "127.0.0.1", "TestMessage");
        handler.UpdateLogs();

I had to slightly modify the authors code to get it to work outside of Unity but only slightly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top