I did a test connecting Arduino and Unity with the help of Processing and a Open Sound Control library. When I am pushing the Flexi Force sensor the values affect a cube within Unity. To get this setup do the following:
Arduino
Load the Standard Firmdata on to your Arduino. Check out a full Arduino + Processing tutorial to get the setup.
Processing
Download the oscP5 processing library and put it in your Processing sketch folder in the folder libraries.
Initialize oscP5 library and broadcast data to port 12000. The port Unity will listen to.
Add the remote location to listen to on port 3200. If Unity would broadcast event in this example, the broadcast port would be set to 3200. Initialise Arduino to be used later on.
myRemoteLocation = new NetAddress("127.0.0.1",3200);
arduino = new Arduino(this, Arduino.list()[0], 57600);
In the draw function we read the analoge pin 0 to get sensor values from the Flexi Force sensor. Set the OSC message to flexiforce. Unity will listen to this label. Add the message of the sensor value into the osc message with add method. This method can take numbers, strings and byte data.
OscMessage oscMess = new OscMessage("/flexiforce");
oscMess.add(flexiforceSensor);
oscP5.send(oscMess, myRemoteLocation);
The entire Processing sketch can be seen here.
Unity
In Unity you can import the Osc.cs and UDPPacketIO.cs to broadcast and listen to UDP data.
Set up the corresponding host and ports in your Unity script.
public var SendToPort : int = 3200;
public var ListenerPort : int = 12000;
Import the UDPPacketIO and Osc components and feed in the host and ports. Add your specific label of your osc message and what method to call when receiving this event from Processing.
udp.init(OSCHost, SendToPort, ListenerPort);
handler = GetComponent("Osc");
handler.init(udp);
handler.SetAddressHandler("/flexiforce", AffectObject);
In the receiving method you can print out the label and message values. In this case the sensor value from the Flexi Force. The cube will change width when getting a new value from Processing.
{
Debug.Log("Event name: " + Osc.OscMessageToString(oscMessage));
Debug.Log("Event data: " + oscMessage.Values[0]);
var myCube = GameObject.Find("Cube");
var boxWidth:int = 8 - ( oscMessage.Values[0]);
myCube.transform.localScale = Vector3(boxWidth,5,5);
}














Flexi force sensor | Arduino 10:35 on May 31, 2012 Permalink
[...] example of how to use a Flexi Force sensor with Arduino. This entry was posted in cicuits and tagged flexi force, resistor by ellen. Bookmark the [...]
Pablo 10:46 on June 5, 2012 Permalink
great tutorial! do you have the entire source code available??
Juan 13:27 on June 16, 2012 Permalink
Hi Ellen,
As Pablo said: GREAT JOB!
I’m trying to do the same, can you email me the sorce code?
Thanks a lot
Juan
Mikael 02:22 on June 22, 2012 Permalink
How can I find the Osc.cs and UDPPacketIO.cs for the Unity?
And yes I expect to have the source code also.
Thank you,
Mikael
Ellen 16:46 on June 25, 2012 Permalink
The Osc.cs and UDPPacketIO.cs files are now available for download within the post. I will post the source code to the project shortly!
Ellen 18:00 on June 26, 2012 Permalink
Source code and Processing example can be downloaded from here: https://github.com/ellensundh/Bocki_unity_processing/zipball/master
Ellen 10:13 on July 2, 2012 Permalink
Test out the demo of Unity and Processing here: http://www.sundh.com/blog/2012/07/unity-processing-demo/
between0and1 19:58 on August 6, 2012 Permalink
HI, I really like the tutorial and am excited to make it work full through. I’ve got all the processing / arduino bits working, but on moving into Unity I get a null reference exception on the line
udp.init(-args-)
I’m assuming it has to do with the way it is trying to access UDPPacketIO script, but I don’t yet have enough experience to know how to modify this to work properly.
I’ve downloaded your processing demo and worked on it from there, but still can’t quite get it.
Thanks for the demo! I’m excited to move forward wit this
Experimenting with Unity, Processing and Arduino « freetronicsblog 02:24 on September 6, 2012 Permalink
[...] more information and links to the required tutorials, visit Ellen’s page. And we’re on twitter and Google+, so follow us for news and [...]
Ellen 07:42 on September 6, 2012 Permalink
@between0and1 Which platform are you on? If you run WIndows you need to change a line UDPPacketIO.cs. On Mac you import IO like this: “using System.IO;”. On Windows you need to add .Ports so you import ” System.IO.Ports;”
shanshan 23:01 on October 6, 2012 Permalink
Hi Ellen
Thank you so much for sharing resources and knowledge! this is amazing!
Matthias 10:46 on October 24, 2012 Permalink
What is this error?
Exception in thread “Animation Thread” java.lang.IllegalAccessError: tried to access class processing.core.PApplet$RegisteredMethods from class cc.arduino.Arduino$SerialProxy
Oliver Jones 00:19 on January 27, 2013 Permalink
Nice work Ellen! Is it possible to reverse this? So I can send variables from Unity, into Processing?
False game vs. True game (pragmatism) | Physical Computing, Round 2 00:23 on February 20, 2013 Permalink
[...] Squeezy Cube [...]