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:
Load the Standard Firmdata on to your Arduino. Check out a full Arduino + Processing tutorial to get the setup.
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.
oscP5 = new OscP5(this,12000);
myRemoteLocation = new NetAddress("127.0.0.1",3200);
arduino = new Arduino(this, Arduino.list(), 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 =
The entire Processing sketch can be seen here.
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 OSCHost : String = "127.0.0.1";
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.
var udp : UDPPacketIO = GetComponent("UDPPacketIO");
udp.init(OSCHost, SendToPort, ListenerPort);
handler = GetComponent("Osc");
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.
public function AffectObject(oscMessage : OscMessage) : void
Debug.Log("Event name: " + Osc.OscMessageToString(oscMessage));
Debug.Log("Event data: " + oscMessage.Values);
var myCube = GameObject.Find("Cube");
var boxWidth:int = 8 - ( oscMessage.Values);
myCube.transform.localScale = Vector3(boxWidth,5,5);