Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

MIDI or OSC for Unity Indie users?

Discussion in 'Formats & External Tools' started by VJ_Anomolee, Jan 19, 2009.

  1. VJ_Anomolee

    VJ_Anomolee

    Joined:
    Dec 23, 2008
    Posts:
    40
    Does anyone have any ideas about how i can use/get MIDI or OSC control inside a unity game?

    (aside from that MIDI plugin someone made that only works in Unity Pro)
     
  2. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
  3. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
  4. ksobecka

    ksobecka

    Joined:
    Mar 25, 2009
    Posts:
    2
    Jonas, any chance you could post some example code? That would be really helpful...
    thanks
     
  5. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
  6. hai_ok

    hai_ok

    Joined:
    Jun 20, 2007
    Posts:
    193
    anyone wanna hazard a javascript port?
     
  7. olin

    olin

    Joined:
    May 23, 2008
    Posts:
    5
  8. obviousjim

    obviousjim

    Joined:
    Oct 11, 2009
    Posts:
    29
    For those who are wanting to use OSC with Unity, I am attaching an example project that shows a bare bones hello world for receiving messages.

    It is based off the above mentioned MakingThings library but has been modified to compile in unity, and comes with a project showing how to set it up.

    Hope it helps someone out there!
     

    Attached Files:

  9. jonash

    jonash

    Joined:
    Nov 4, 2009
    Posts:
    1
    Hi, that's very cool! Could you please add a OSC Send JavaScript example as well? I am trying to get it work, but still fail...

    EDIT: I was able to finally come up with the right syntax, so just add this function to the above code:

    Code (csharp):
    1.  
    2. public function sendOSC(txt : String){ 
    3. handler.Send(handler.StringToOscMessage(txt));
    4. }
    5.  
    Thanks to obviousjim
     
  10. obviousjim

    obviousjim

    Joined:
    Oct 11, 2009
    Posts:
    29
  11. rudebwoy

    rudebwoy

    Joined:
    Nov 23, 2009
    Posts:
    3
    Mhhhh i have a problem to build the osc example. if i press play in unity it works, but after building nothing happens. I use the old 2.5 Indie version, is this a problem? or should i implements something in the builded file? I'm workin on a mac...
     
  12. kjauslin

    kjauslin

    Joined:
    Jan 1, 2010
    Posts:
    9
    Thank you very much for this example. I implemented a C# starter script instead of the javascript one. And run into two funny problems
    1. The udp listener thread is not stopping and continues to run even when the game is stopped
    2. The udp port is not freed when restarting the game (play-stop-play).

    This will give also strange timeouts when pressing play a second time - having to wait for some seconds until the game really starts.

    To workaround these problems, I found the following solution (I can also post the C# script here if anyone is interested):
    1. The starter script needs to implement an object destructor that calls the function oscHandler.Cancel()
    2. The udp listener thread in Osc.cs should be a foreground thread (set ReadThread.IsBackground=false in Osc.init). This will ensure that the thread can be terminated normally (when pressing stop) and the socket can be freed.

    This way, OSC works perfectly in Unity. To test with the example code above, I can also recommend the (osc.M monitor or OSCMonitor - on Windows platforms).

    Cheers, Kai

    PS. I just realized one more thing: the c# callback function to be used with oscHandler.SetAddressHandler() should be declared static. Otherwise the solution above will not work at all because the destructor is never called. I guess the GC cannot resolve the cyclic reference.

    All this would not be a problem, if there would be a stop method for the GameObject (that is called when the game is stopped in the editor) - then we could clean up properly. Is there maybe such a method?
     
  13. dpentecost

    dpentecost

    Joined:
    Apr 3, 2009
    Posts:
    136
    Interested! Please post.
     
  14. kjauslin

    kjauslin

    Joined:
    Jan 1, 2010
    Posts:
    9
    Ok, here is the working example (the code from above, but full c# and bugfixes for init/closing udp socket). It runs now very nicely. The correct way is to implement the "OnDisable" method and close the udp socket there.

    It still gives a warning, that the listener socket is already in use, but you can ignore it and messages arrive nevertheless. I have tested with a C++ program using OSCpack (send and receive OSC messages).

    A nice addition to better work with OSCpack would be some OSC 1.1 features (e.g. boolean types)... anyone?

    Regards, Kai
     

    Attached Files:

  15. dpentecost

    dpentecost

    Joined:
    Apr 3, 2009
    Posts:
    136
    Thanks Kai!
     
  16. dpentecost

    dpentecost

    Joined:
    Apr 3, 2009
    Posts:
    136
    Thanks to obviousjim and Kai - I got both receive and send tests working with TouchOSC on the iPhone and OSCulator on the Mac.
     
  17. psydack

    psydack

    Joined:
    Sep 18, 2009
    Posts:
    8
    Hello, im with a little problem when I run that example this happens

    Code (csharp):
    1. cannot open udp client interface at port 9287
    2. UnityEngine.Debug:LogWarning(Object)
    3. UDPPacketIO:Open() (at Assets\Plugins\UDPPacketIO.cs:61)
    4. UDPPacketIO:SendPacket(Byte[], Int32) (at Assets\Plugins\UDPPacketIO.cs:108)
    5. Osc:Send(OscMessage) (at Assets\Plugins\Osc.cs:248)
    6. OSCTestSender:Update() (at Assets\scripts\OSCTestSender.cs:40)
    7.  
    And this:

    Code (csharp):
    1.  
    2. System.Net.Sockets.SocketException: Address already in use
    3.   at System.Net.Sockets.Socket.Bind (System.Net.EndPoint local_end) [0x00000]
    4.   at System.Net.Sockets.UdpClient.InitSocket (System.Net.EndPoint localEP) [0x00000]
    5.   at System.Net.Sockets.UdpClient..ctor (System.Net.IPEndPoint localEP) [0x00000]
    6.   at UDPPacketIO.Open () [0x0001c] in C:\Documents and Settings\user\Desktop\osc_example_662\osc_example\Assets\Plugins\UDPPacketIO.cs:54
    7. UnityEngine.Debug:LogWarning(Object)
    8. UDPPacketIO:Open() (at Assets\Plugins\UDPPacketIO.cs:62)
    9. UDPPacketIO:SendPacket(Byte[], Int32) (at Assets\Plugins\UDPPacketIO.cs:108)
    10. Osc:Send(OscMessage) (at Assets\Plugins\Osc.cs:248)
    11. OSCTestSender:Update() (at Assets\scripts\OSCTestSender.cs:40)
    12.  
    And I really sure what that port is open without other application running.

    I tried with other port and get same error. I sending with oscP5 (for Processing) and this app get unity3D messages.

    Someone knows what how I can fix it?

    PS: I try run in MAC and the same error appear.

    Thanks.
     

    Attached Files:

  18. dpentecost

    dpentecost

    Joined:
    Apr 3, 2009
    Posts:
    136
    I got a pm from another beginner asking how I had gotten this to work. So I have put together a project that combines the previous two (both send and receive), added a screenshot and a readme. It still just confirms send and receive in the console - no manipulation of objects in Unity yet.

    EDIT: Check my post a few posts down from this one, for a downloadable demo that does manipulate Unity objects.

    Hope it helps someone else. All the pieces were there but if you are not a coder (I am not) it can be a bit confusing.

    The readme includes links to TouchOSC, OSCulator, and oscP5.

    Good luck!

    PS psydack - My guess is that you will have to play with your IP and port settings to get it right. Check what ports Processing is using.
     

    Attached Files:

  19. psydack

    psydack

    Joined:
    Sep 18, 2009
    Posts:
    8
    Thanks, I'm really like but I'm got confused now! I'm get your way, step by step and nothing.

    Im really don't know what happens. I'm now using one PC with Processing (192.168.1.110) and other Mac using Unity (192.168.1.103). I already test with inverting the programs (PC with Unity and Mac with Processing) and both tests Processing work normally.

    I'd put a print screen with I'm using Mac with Unity don't working. I'm tried others ports and I'm tried with runs every at same local: Mac with Processing and Unity3D and PC with Unity3D and Processing.

    The other Script wich only send(with the error when try to open but send) I'm realized some tests with Processing and it works. But this don't received only send.


    Sorry for something and thanks for all!
     

    Attached Files:

  20. kjauslin

    kjauslin

    Joined:
    Jan 1, 2010
    Posts:
    9
    Hi,
    from your screenshots I can see that you are sending in processing to path '/test' (new OscMessage("/test")). But the c# address handlers you use have the paths "/1/toggle1" and "/2/toggle2". This has to match. So setting this to e.g. "/1/toggle1" in processing should make it work.

    Regards, Kai
     
  21. dpentecost

    dpentecost

    Joined:
    Apr 3, 2009
    Posts:
    136
    I have a version here of OSC_example_3 with objects (a cube and a sphere) responding to OSC toggle messages. Yay!

    In the attached example project, the Hello3 scene is the one to try. My settings in the scene, for IP and ports, override the default scripted ones. The OSCEmpty object is running things - the cube and sphere objects themselves are not scripted.

    But my code in the OSCReceiver script seems to be a very clunky way to do this. Is there a better way to do the Update loop? Or any other part?

    Code (csharp):
    1. //You can set these variables in the scene because they are public
    2. public var RemoteIP : String = "127.0.0.1";
    3. public var SendToPort : int = 57131;
    4. public var ListenerPort : int = 57130;
    5. public var controller : Transform;
    6. private var handler : Osc;
    7. private var sig1 : int = 0;
    8. private var sig2 : int = 0;
    9.  
    10.  
    11. public function Start ()
    12. {
    13.     //Initializes on start up to listen for messages
    14.     //make sure this game object has both UDPPackIO and OSC script attached
    15.    
    16.     var udp : UDPPacketIO = GetComponent("UDPPacketIO");
    17.     udp.init(RemoteIP, SendToPort, ListenerPort);
    18.     handler = GetComponent("Osc");
    19.     handler.init(udp);
    20.            
    21.     handler.SetAddressHandler("/1/toggle1", Example1);
    22.     handler.SetAddressHandler("/1/toggle2", Example2);
    23.     handler.SetAddressHandler("/1/toggle3", Example3);
    24.     handler.SetAddressHandler("/1/toggle4", Example4);
    25.    
    26. }
    27. Debug.Log("Running");
    28.  
    29. function Update () {
    30.    
    31.     var go = GameObject.Find("Cube1");
    32.     go.transform.Rotate(0, sig1, 0);
    33.     var so = GameObject.Find("Sphere1");
    34.     so.transform.Rotate(0, sig2, 0);
    35. }  
    36.  
    37. //These functions are called when messages are received
    38. //How to access values:
    39.     //oscMessage.Values[0], oscMessage.Values[1], etc
    40.  
    41. //Example 1
    42. public function Example1(oscMessage : OscMessage) : void
    43. {  
    44.    
    45.     Debug.Log("Called Example One > " + Osc.OscMessageToString(oscMessage));
    46.     Debug.Log("Message Values > " + oscMessage.Values[0]);
    47.     sig1 = oscMessage.Values[0];
    48. }
    49.  
    50. // Example 2
    51. public function Example2(oscMessage : OscMessage) : void
    52. {
    53.    
    54.     Debug.Log("Called Example Two > " + Osc.OscMessageToString(oscMessage));
    55.     Debug.Log("Message Values > " + oscMessage.Values[0]);
    56.     sig1 = - (oscMessage.Values[0]);
    57.    
    58. }
    59.  
    60. //Example 3
    61. public function Example3(oscMessage : OscMessage) : void
    62. {  
    63.    
    64.     Debug.Log("Called Example Three > " + Osc.OscMessageToString(oscMessage));
    65.     Debug.Log("Message Values > " + oscMessage.Values[0]);
    66.     sig2 = oscMessage.Values[0];
    67. }
    68.  
    69. // Example 4
    70. public function Example4(oscMessage : OscMessage) : void
    71. {
    72.    
    73.     Debug.Log("Called Example Four > " + Osc.OscMessageToString(oscMessage));
    74.     Debug.Log("Message Values > " + oscMessage.Values[0]);
    75.     sig2 = - (oscMessage.Values[0]);
    76.    
    77. }
    78.  
    Note again that I am using the first page of the TouchOSC app to test this. Works fine - 4 toggles turn off and on two rotation directions for two objects, no error messages - but I'm sure the code could be improved. Thanks in advance for any tips.

    UPDATE 11/13/2010: A number of users (particularly OS X users) found that Unity 3.0 broke this OSC implementation. I noted the same thing and filed a bug report. Going back to this thread I found the comment by elbow below which is a fix that also seems to work in 3.1 -

    "I just changed line 2 of UDPPacketIO.cs to remove the .Ports from the end so that it just says using System.IO; and it seems to work."

    You will have to edit that line in the attachment on this post. Good luck!
     

    Attached Files:

    Last edited: Nov 14, 2010
  22. psydack

    psydack

    Joined:
    Sep 18, 2009
    Posts:
    8
    I trade in OSCReceiver:
    Code (csharp):
    1.  
    2.  
    3. //You can set these variables in the scene because they are public
    4. public var RemoteIP : String = "127.0.0.1";
    5. public var SendToPort : int = 57131;
    6. public var ListenerPort : int = 57130;
    7. public var controller : Transform;
    8. private var handler : Osc;
    9.  
    10.  
    11.  
    12. public function Start ()
    13. {
    14.     //Initializes on start up to listen for messages
    15.     //make sure this game object has both UDPPackIO and OSC script attached
    16.     var udp : UDPPacketIO = GetComponent("UDPPacketIO");
    17.     udp.init(RemoteIP, SendToPort, ListenerPort);
    18.     handler = GetComponent("Osc");
    19.     handler.init(udp);
    20.            
    21.     handler.SetAddressHandler("/test", Example1);
    22.     //handler.SetAddressHandler("/test", Example2);
    23. }
    24. Debug.Log("Running");
    25. //these fucntions are called when messages are received
    26. public function Example1(oscMessage : OscMessage) : void
    27. {  
    28.     //How to access values:
    29.     //oscMessage.Values[0], oscMessage.Values[1], etc
    30.     Debug.Log("Called Example One > " + Osc.OscMessageToString(oscMessage));
    31. }
    32.  
    33. //these fucntions are called when messages are received
    34. public function Example2(oscMessage : OscMessage) : void
    35. {
    36.     //How to access values:
    37.     //oscMessage.Values[0], oscMessage.Values[1], etc
    38.     Debug.Log("Called Example Two > " + Osc.OscMessageToString(oscMessage));
    39. }
    40.  
    Oooo I don't believe, it's WORK!
    Thank's so much.

    When the project is done I'll post for you guys!
    Thank's for all for patience and replys.

    :D
     
  23. dpentecost

    dpentecost

    Joined:
    Apr 3, 2009
    Posts:
    136
    Glad to hear it, psydack.

    And I am particularly interested in seeing your Arduino and Processing code when you get all that working.
     
  24. abchappy1234

    abchappy1234

    Joined:
    Mar 15, 2010
    Posts:
    3
    Receive works fine , but when i send there is a long time suspend.

    How to fix this?
     
  25. tbryant

    tbryant

    Joined:
    Aug 12, 2009
    Posts:
    17
    Thanks Kai,
    I have been looking at your examples. When I am using my own .js receiver script, I have an error message:
    m_ThreadCheck !Thread::EqualsCurrentThreadID(m_ThreadID)

    Upon searching the forums, this is an assert that occurs when Unity notices a thread that is not the main thread, since Unity is not thread safe.

    So, my question: Is Osc.cs using threading, and is that a problem in Unity?

    -Tyler
     
  26. ibmeruu

    ibmeruu

    Joined:
    Apr 21, 2010
    Posts:
    18
    Hello,

    Thank you so much for the example. I am getting OSC messages from OSCulator and the values look great in the debug but when I apply them to a tranform object the variables sig1 and sig2 are always 1 or 0.

    Couldn't find the reason in the OSC Receiver script...

    Would you please help? :)
     
  27. thevidness

    thevidness

    Joined:
    Feb 23, 2010
    Posts:
    5
    hi,

    i got your code working in unity 2.6.1 a few months back.
    just wanted to check if it's working in unity 3.0.0f3

    the console spits:

    Assets/Plugins/UDPPacketIO.cs(2,17): error CS0234: The type or namespace name `Ports' does not exist in the namespace `System.IO'. Are you missing an assembly reference?

    any clue how to solve that?

    thanks :)
     
  28. miscellany

    miscellany

    Joined:
    Oct 26, 2005
    Posts:
    24
    Hi,

    I think your only issue may be that you have the build target platform set to 'webplayer'. If you set it to PC/Mac all should be well..

    cheers hope this helps
    andrew
     
  29. elbows

    elbows

    Joined:
    Nov 28, 2009
    Posts:
    2,502
    Does anyone have any thoughts on making OSC scripts that send events so I can trigger functions in lots of different scripts attached to game objects? In theory could this be more efficient than using ifs within update functions to continually detect whether osc values have changed? And neater/easier to follow than altering values of various gameobject parameters within the main osc script?

    So far Ive been using OSC not with the scripts on this thread, but rather the ones from OSCuMote ( http://forum.unity3d.com/threads/21273-OSCuMote-Wiimote-support-for-the-free-version )

    Im about to spend 8 hours stumbling around seeing what I can achieve, will post here if I get anywhere.
     
  30. elbows

    elbows

    Joined:
    Nov 28, 2009
    Posts:
    2,502
    Trying this for the first time, had same error. I just changed line 2 of UDPPacketIO.cs to remove the .Ports from the end so that it just says using System.IO; and it seems to work.
     
  31. elbows

    elbows

    Joined:
    Nov 28, 2009
    Posts:
    2,502
    I abandoned my attempts to use messaging/event function triggers with OSC, as performance did not seem good so I guess its not worth it and some of my assumptions about this stuff were wrong.
     
  32. UnityStudent

    UnityStudent

    Joined:
    Dec 10, 2010
    Posts:
    3
    I am trying to use this as an example to learn UDP communication. But I am limited by using OSC.
    Is there a work around?
     
  33. UnityStudent

    UnityStudent

    Joined:
    Dec 10, 2010
    Posts:
    3
    Actually to rephrase my question,
    I am trying to this project and modify it to move the limbs of a character in Unity3d.
    It seems to compile and run without errors.
    But I dont see anything happening, probably because, dpentecost TouchOSC app to test this.

    I am a windows user, is there any this project can do anything on windows, that I can tweak learn something and adapt for character control in Unity.

    Many thanks!
     
  34. thetnswe

    thetnswe

    Joined:
    Aug 23, 2010
    Posts:
    46
    I've got the following errors

    cannot open udp client interface at port 8000

    System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted.

    Anyone has it before....

    solved it... It happens because I tried to put both receiver and sender scripts where they both are opening port.. removed one of them and now it works... How about external application is sending OSC message to Unity.. How can I just listen OSC message?
     
    Last edited: Mar 13, 2011
  35. joshuabatty

    joshuabatty

    Joined:
    Apr 7, 2011
    Posts:
    1
    Does anyone have any suggestions on how to do this? I am trying to control a camera that has a look at script with smoothing to avoid jerkiness using osc... At the moment the smoothing script works with the mouse but when I try to replace the mouse input with a fader from touchosc everything seems to stop.

    It seems strange to have to build all the behavior you want to control with osc inside the OSCReceive script, instead it would be way more helpful to be able to send this data out to other scripts that are controlling objects behaviors.

    Anyone know if this is even possible? I'm running into the limit of my unity/scripting knowledge....anyhelp would be greatly appreciated.

    Thanks.
     
  36. apiffer

    apiffer

    Joined:
    Apr 28, 2011
    Posts:
    2
    Thanks for this discussion, really helpful!
    I achieved what I needed for a college project, send OSC messages to GlovePIE, then make joysticks rumble. :D

    I'ts working in Unity3; project files are the same from this post (a bit edited):
    - Osc.cs
    - UDPPacketIO.cs
    - OscSender.cs

    Using: System.IO; instead of using System.IO.Ports; as our friend posted earlier.

    And remember to set Remote IP to "localhost" to work. In my case I also set Listener Port to 0, then all errors gone. At least in my case, that I only wanted to send messages and don't receive, worked.

    I just didn't got the meaning of this; so I removed that...is there a problem?
    Code (csharp):
    1.  
    2. ~OSCTestSender()
    3.     {
    4.         if (oscHandler != null)
    5.         {            
    6.             oscHandler.Cancel();
    7.         }
    8.  
    9.         // speed up finalization
    10.         oscHandler = null;
    11.         System.GC.Collect();
    12.     }
     
  37. Bravini

    Bravini

    Joined:
    Nov 15, 2010
    Posts:
    9
    Apiffer, could you by any chance post your modified version? force feedback on joysticks is of great interest to me :razz:
     
  38. metamorphoise

    metamorphoise

    Joined:
    May 16, 2011
    Posts:
    4
    This is a very useful thread, I have been able to modify dpentecost's example (thankyou dpentecost!!) into a proof of concept example for controlling a multiplayer game running on a laptop/iPad via iOS/android phones as separate controllers

    The problem is that with only one OSCreciever thread it is listening for OSC messages only on one port.
    As a workaround, I currently have each different player sending different OSCmessages to the same port (i.e. /player1/up, /player2/left) but this is far from ideal

    It would be much more flexible to have player1 sending to port 1000, player2 to port 2000, and so on.
    I imagine this involves running the OSC.cs plugin multiple times with each instance listening on a different port.

    However, I have zero experience with OO programming and have no idea how to do this.

    Would anyone please be able provide some advice or example code?

    Thanks,
    metamorphoise~


    (attached is my modified version of dpentecost's example, configured for 4 players to move a sphere in 2 dimensions, each player using his/her iOS/android phone running an OSC app as controller. you would probably have to adjust the OSCmessages being listened for, hardcoded around lines 20-30 of the OSCreciever script.)
     

    Attached Files:

    Last edited: May 16, 2011
  39. metamorphoise

    metamorphoise

    Joined:
    May 16, 2011
    Posts:
    4
    no-one is interested in listening for OSC messages on different ports?
     
  40. femi

    femi

    Joined:
    Dec 16, 2008
    Posts:
    9
    FYI, I have an alternative minimal implementation of OSC (only i/f/s types and no bundles supported).

    The library itself only does package parsing / construction: https://github.com/artm/Osc.cs

    There is a separate project that implements UDP transport with an asynchronous listener: https://github.com/artm/Cyrus/blob/master/Assets/Standard Assets/SCClient.cs

    The SCClient implements communication with SuperCollider: when it starts, it sends ("/notify" 1) message, so that super collider knows to send notifications to it over the same socket. When working with other OSC clients you would go differently about this step (e.g. manually informing the client of address / Port unity listens to).

    Yet another example shows how to deal with data that the SCClient broadcasts: https://github.com/artm/Cyrus/blob/master/Assets/Standard Assets/SpectrumParser.cs

    It assumes a particular format "/spectrum" messages arrive in. It also assumes that there isn't much OSC traffic besides "/spectrum". In different situations you would want more involved dispatching mechanism, but not in my project.
     
  41. thevidness

    thevidness

    Joined:
    Feb 23, 2010
    Posts:
    5
  42. meiuqer

    meiuqer

    Joined:
    Jan 23, 2012
    Posts:
    12
    Don't know if anyone can help me but I'm using Synapse for the Kinect and it's an application that sends OSC messages to port 12347. But he sends its OSC messages in a different way. Like this:

    /head_pos_world <float> <float> <float>

    How do I receive this in Unity? I'm trying it with dpentecost's script but can't figure it out.
    I think it has something to do with the fact that Synapse sends 3 floats at once, and the script is listening to only one integer.
    Anyone got an idea on how to adjust the code ?
     
  43. meiuqer

    meiuqer

    Joined:
    Jan 23, 2012
    Posts:
    12
    Called Example One > /head_pos_screen 431.3883 295.0804 2068.547
    Message Values > 431.3883

    I only get the first float. Can't seem to get the second and third :/



    //You can set these variables in the scene because they are public
    public var RemoteIP : String = "127.0.0.1";
    public var SendToPort : int = 57131;
    public var ListenerPort : int = 12347;
    public var controller : Transform;
    public var jipla1 : float = x;
    public var jipla2 : float = y;
    public var jipla3 : float = z;
    private var handler : Osc;
    private var x : int = 0;
    private var y : int = 0;
    private var z : int = 0;


    public function Start ()
    {
    //Initializes on start up to listen for messages
    //make sure this game object has both UDPPackIO and OSC script attached

    var udp : UDPPacketIO = GetComponent("UDPPacketIO");
    udp.init(RemoteIP, SendToPort, ListenerPort);
    handler = GetComponent("Osc");
    handler.init(udp);

    handler.SetAddressHandler("/head_pos_screen", Example1);

    }
    Debug.Log("Running");

    function Update () {

    var go = GameObject.Find("Cube1");
    go.transform.Rotate(0, x, 0);
    var so = GameObject.Find("Sphere1");
    so.transform.Rotate(0, y, 0);
    }

    //These functions are called when messages are received
    //How to access values:
    //oscMessage.Values[0], oscMessage.Values[1], etc

    //Example 1
    public function Example1(oscMessage : OscMessage) : void
    {

    Debug.Log("Called Example One > " + Osc.OscMessageToString(oscMessage));
    Debug.Log("Message Values > " + oscMessage.Values[0]);
    x = oscMessage.Values[0];
    y = oscMessage.Values[1];
    z = oscMessage.Values[2];
    }

    This is what I did
     
  44. meiuqer

    meiuqer

    Joined:
    Jan 23, 2012
    Posts:
    12
    got it to work after all, some people also had this issue finding it so this is what I did

    //Example 1
    public function Example1(oscMessage : OscMessage) : void
    {

    Debug.Log("Called Example One > " + Osc.OscMessageToString(oscMessage));
    Debug.Log("Message Values > " + oscMessage.Values[0]);
    Debug.Log("Message Values > " + oscMessage.Values[1]);
    Debug.Log("Message Values > " + oscMessage.Values[2]);
    x = oscMessage.Values[0];
    y = oscMessage.Values[1];
    z = oscMessage.Values[2];
    }
     
  45. elbows

    elbows

    Joined:
    Nov 28, 2009
    Posts:
    2,502
    I started working on a few PlayMaker Actions that work with these OSC scripts last night, and its working pretty well. I will share soon, although someone else could probably perfect the Actions a little more than I have managed so far.

    To make it work in a more foolproof way with PlayMaker, it would be best if I could make it so that more than one simultaneously running PlayMaker action could listen to the same OSC Address Path (e.g. /slider1 ). But this requires the osc.cs code to be modified so that more than one handler can be attached to a single address. This is slightly beyond my capabilities at the moment. As I have added a function to osc.cs to enable handlers to be unregistered, the improved handler code would also need a way to recognise multiple handlers for the same address in a manner that allows them to be uniquely identified, so that the right one can be removed when required (which in this case is when the FSM state that the action is on is exited). I don't suppose anybody wants to help with this improvement once I've shared my version that has this limitation? The limitation is easy enough to workaround in PlayMaker as long as careful attention is paid if you want to listen for the same Address Path more than once, make sure there is not more than one Action listening for that path at any one time, but it would be best if this issue was solved properly.
     
  46. elbows

    elbows

    Joined:
    Nov 28, 2009
    Posts:
    2,502
    I made available the first versions of my PlayMaker actions for receiving OSC here:

    http://hutonggames.com/playmakerforum/index.php?topic=1087.0

    Consider it experimental at this point, especially as I made some changes to Osc.cs so that the a particular OSC Message Address can be handled by more than one PlayMaker Action simultaneously.
     
  47. Zardoz

    Zardoz

    Joined:
    Apr 23, 2012
    Posts:
    3
    First off: Thanks for all the work on this. Very cool stuff with lots of potential uses!

    The problem that I'm having is that I'm able run the Send OR the Recieve scripts, but when I try to implement both I get this error:
    System.Net.Sockets.SocketException: Address already in use
    at System.Net.Sockets.Socket.Bind (System.Net.EndPoint local_end) [0x00000] in <filename unknown>:0
    at System.Net.Sockets.UdpClient.InitSocket (System.Net.EndPoint localEP) [0x00000] in <filename unknown>:0
    at System.Net.Sockets.UdpClient..ctor (System.Net.IPEndPoint localEP) [0x00000] in <filename unknown>:0
    at UDPPacketIO.Open ()

    I'm guessing this is because both scripts are running the same initialization script through UDPPacketIO and trying to open the same port/address. I'll also guess that the solution may involve throwing in some conditional statement to check for the other's activity before running. Anybody have a quick answer to this before I start beating my head on the wall? (I'm not the world's greatest programmer and I'm new to this OSC/UDP thing)

    Is there any reason these can't be combined into the same script? (apart from being written in two languages- why is that, btw?)

    Thanks
     
  48. Zardoz

    Zardoz

    Joined:
    Apr 23, 2012
    Posts:
    3
    Got it! It's working smoothly with the the send and receive scripts mashed-up into one JS script. It wasn't so difficult to make the conversion.
     
  49. Helgosam

    Helgosam

    Joined:
    Feb 2, 2012
    Posts:
    12


    Hi All
    Many thanks to everyone so far- fantastic work

    I come from using Processing.org , DarwiinRemote (actually DarwiinRemoteOSC) for OSX by Andreas Schlegel, (http://www.sojamo.de), and have had success and fun getting the Wiimote talking to processing. I thought I'd try to get it to speak to Unity...

    Having spent hours trying to find out why unity was just not responding at all and not receiving any of the OSC messages, when Processing was receiving the OSC messages fine...

    I had glossed over this line in the post above: "My settings in the scene, for IP and ports, override the default scripted ones"

    and I had forgotten that in Unity a public variable means that it turns up in the GUI in the inspector, and can be overwritten. Thus I had set the listenerPort to 5600 (which is the one darwiinRemoteOSC sends to) in the script, but I didn't see that the listener port in the INSPECTOR was set to 8000.

    Finally I made the listener port Private (equally you can change the port in the inspector) and did the same to the remoteIP. Here's the code that I used which worked fine with darwiinRemoteOSC...





    //You can set these variables in the scene because they are public
    private var RemoteIP : String = "127.0.0.1";
    public var SendToPort : int = 57131;
    //public var ListenerPort : int = 57130;
    private var ListenerPort : int = 5600; // listen to darwiinRemote
    //public var controller : Transform;
    private var handler : Osc;
    private var sig1 : int = 0;
    private var sig2 : int = 0;
    //public var p1x : float = 0;
    //public var p1y : float = 0;
    //public var p2x : float = 0;
    //public var p2y : float = 0;


    public function Start ()
    {
    //Initializes on start up to listen for messages
    //make sure this game object has both UDPPackIO and OSC script attached

    var udp : UDPPacketIO = GetComponent("UDPPacketIO");
    udp.init(RemoteIP, SendToPort, ListenerPort);
    handler = GetComponent("Osc");
    handler.init(udp);

    handler.SetAddressHandler("/wii/button/left", Example1);
    handler.SetAddressHandler("/wii/button/right", Example2);
    handler.SetAddressHandler("/wii/button/up", Example3);
    handler.SetAddressHandler("/wii/button/down", Example4);


    --- rest as normal...
     
    Last edited: May 10, 2012
  50. Helgosam

    Helgosam

    Joined:
    Feb 2, 2012
    Posts:
    12
    Hi All

    Has anyone got something which will work on Android - for reading OSC into an android app?

    Any ideas? I've tried just compiling the above and running on android, but had no luck :-(

    Thanks in advance

    *EDIT
    So I got the OSC messages through to my android device - pretty sweet really:


    This image shows how I got the Wiimote connected to darwiinRemoteOSC (via bluetooth) to send OSC messages straight to the android tablet (located at 10.0.2.12: 8080 on my home-grown MBP wifi network), and the Android unity app actually responds to the wiimote controls... pretty nice!

    Note that the Unity script must change accordingly:

    private var RemoteIP : String = "10.0.2.12";
    public var SendToPort : int = 8080;
    private var ListenerPort : int = 8080; // listen to darwiinRemote

    I know that it's not necessary that both of the above are set to 8080, but I don't know which is which, so if someone wants to correct this feel free.
     
    Last edited: Jun 5, 2012