34
edits
m (→Processing: formatting) |
(→Unity: formatting) |
||
(2 intermediate revisions by the same user not shown) | |||
Line 73: | Line 73: | ||
<br> | <br> | ||
Public values are adjustable without any coding; just use the inspector for that. <br>< | Public values are adjustable without any coding; just use the inspector for that. <br> | ||
public var RemoteIP : String = ""; | <source lang="javascript"> | ||
public var SendToPort : int = xxxx; | public var RemoteIP : String = ""; | ||
public var ListenerPort : int = yyyy; | public var SendToPort : int = xxxx; | ||
public var controller : Transform; | public var ListenerPort : int = yyyy; | ||
public var gameReceiver = "Cube";</ | public var controller : Transform; | ||
In the Start() function all the necessary scripts and components are connected. This will happen once you press Play. <br>< | public var gameReceiver = "Cube"; | ||
public function Start () | </source> | ||
{ | In the Start() function all the necessary scripts and components are connected. This will happen once you press Play. <br> | ||
var udp : UDPPacketIO = GetComponent("UDPPacketIO"); | <source lang="javascript"> | ||
udp.init(RemoteIP, SendToPort, ListenerPort); | public function Start () | ||
handler = GetComponent("Osc"); | { | ||
handler.init(udp); | var udp : UDPPacketIO = GetComponent("UDPPacketIO"); | ||
handler.SetAllMessageHandler(AllMessageHandler); | udp.init(RemoteIP, SendToPort, ListenerPort); | ||
}</ | handler = GetComponent("Osc"); | ||
The AllMessageHandler() function is where the Osc happens. This function unpacks the Osc signals, so you can address them in other functions. In this function you can command, whenever A happens, you want FunctionB() to be executed. <br>< | handler.init(udp); | ||
public function AllMessageHandler(oscMessage: OscMessage){ | handler.SetAllMessageHandler(AllMessageHandler); | ||
var msgString = Osc.OscMessageToString(oscMessage); | } | ||
var msgAddress = oscMessage.Address; | </source> | ||
var msgValue = oscMessage.Values[0];</ | The AllMessageHandler() function is where the Osc happens. This function unpacks the Osc signals, so you can address them in other functions. In this function you can command, whenever A happens, you want FunctionB() to be executed. <br> | ||
So you typed all that code and there are no errors but that stupid thing still won't do what it should - try Debug.Logs! Just add them everywhere! They can tell you what parts of your script are accessed after you press Play. <br>< | <source lang="javascript"> | ||
Debug.Log("Stakkars meg, som har skrevet 50 Debug.Logs"); | public function AllMessageHandler(oscMessage: OscMessage){ | ||
The lines will be written in the console section of Unity once they are accessed.<br> | var msgString = Osc.OscMessageToString(oscMessage); | ||
var msgAddress = oscMessage.Address; | |||
var msgValue = oscMessage.Values[0]; | |||
</source> | |||
So you typed all that code and there are no errors but that stupid thing still won't do what it should - try Debug.Logs! Just add them everywhere! They can tell you what parts of your script are accessed after you press Play. <br> | |||
<source lang="javascript"> | |||
Debug.Log("Stakkars meg, som har skrevet 50 Debug.Logs"); | |||
</source> | |||
The lines will be written in the console section of Unity once they are accessed.<br><br> | |||
[[File:50Debug_Logs.png]] | [[File:50Debug_Logs.png]] | ||
Line 108: | Line 116: | ||
c) Add Audio variables to your script. This script will also draw a circle depending on the audio volume. <br> | c) Add Audio variables to your script. This script will also draw a circle depending on the audio volume. <br> | ||
<source lang="javascript"> | <source lang="javascript"> | ||
float x; | float x; | ||
float y; | float y; | ||
Minim minim; | |||
Minim minim; | |||
AudioInput input; | AudioInput input; | ||
</source | </source> | ||
d) In the Setup() function add the values for the circle and create the audiotoolkit <br> | d) In the Setup() function add the values for the circle and create the audiotoolkit <br> | ||
<source lang="javascript"> | <source lang="javascript"> | ||
size (320, 240); | size (320, 240); | ||
smooth(); | smooth(); | ||
stroke (255, 25); | stroke (255, 25); | ||
noFill (); | noFill (); | ||
// Set start position | |||
x = 0; | // Set start position | ||
y = 20; | x = 0; | ||
minim = new Minim (this); | y = 20; | ||
input = minim.getLineIn (Minim.STEREO, 512); | |||
minim = new Minim (this); | |||
input = minim.getLineIn (Minim.STEREO, 512); | |||
background (0); | background (0); | ||
</source | </source> | ||
e) The draw() function will be the container of the circle’s size. It will only send the values to Unity if the circle is big enough. <br> | e) The draw() function will be the container of the circle’s size. It will only send the values to Unity if the circle is big enough. <br> | ||
<source lang="javascript"> | <source lang="javascript"> | ||
void draw () { | void draw () { | ||
int i =0; | int i =0; | ||
float dim = input.mix.level () * width;<br> | float dim = input.mix.level () * width;<br> | ||
if (dim >= 80){System.out.println("Loud enough: "+dim); | |||
OscMessage myMessage = new OscMessage("/"); | if (dim >= 80){System.out.println("Loud enough: "+dim); | ||
myMessage.add(dim); | OscMessage myMessage = new OscMessage("/"); | ||
myMessage.add(true); | myMessage.add(dim); | ||
oscP5.send(myMessage, myRemoteLocation);} | myMessage.add(true); | ||
</source | oscP5.send(myMessage, myRemoteLocation);} | ||
</source> | |||
f) When you are ready, press Play. <br> <br> | f) When you are ready, press Play. <br> <br> | ||
'''2. In Unity:''' <br> | '''2. In Unity:''' <br> | ||
Line 142: | Line 154: | ||
b) Open up the Osc Receiver Script. Define a new private variable. <br><br><source lang="javascript"> | b) Open up the Osc Receiver Script. Define a new private variable. <br><br><source lang="javascript"> | ||
private var moveSpeed : int = 0; | private var moveSpeed : int = 0; | ||
</source | </source> | ||
c) Create a new public function.<br> | c) Create a new public function.<br> | ||
<source lang="javascript"> | <source lang="javascript"> | ||
Line 149: | Line 161: | ||
moveSpeed = msgValue; | moveSpeed = msgValue; | ||
} | } | ||
</source | </source> | ||
d) In the Update() function add <br> | d) In the Update() function add <br> | ||
<source lang="javascript"> | <source lang="javascript"> | ||
var go = GameObject.Find(gameReceiver); | var go = GameObject.Find(gameReceiver); | ||
go.transform.Translate(0, moveSpeed, 0); | go.transform.Translate(0, moveSpeed, 0); | ||
</source | </source> | ||
e) In the AllMessageHandler() function add <br> | e) In the AllMessageHandler() function add <br> | ||
<source lang="javascript"> | <source lang="javascript"> | ||
Line 161: | Line 173: | ||
MoveObject(msgValue); | MoveObject(msgValue); | ||
break;} | break;} | ||
</source | </source> | ||
f) When you are ready, press Play. | f) When you are ready, press Play. | ||
edits