Search code examples
c#udpunity-game-engineoscopensoundcontrol

Configuring client and server using OSC in Unity


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?


Solution

  • 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