I have two objects in a scene, one called Input Manager and the other called Indicator Manager. on Input Manager, I have the following:
//TiltManager.js - Acts as a message controller for all tilt events
private var horizontalOrientation : boolean = true;
private var accelerator : Vector3 = iPhoneInput.acceleration;
static var turn : float;
static var tilt : float;
var receiver : GameObject;
var sensitivity : float = 1.0;
var tiltDeadStick : int = 0;
var zeroAngle : int = 0;
function Awake () {
if (horizontalOrientation)
Screen.SetResolution(480, 320, true);
else
Screen.SetResolution(320, 480, true);
} //end Awake
function Update () {
turn = ((iPhoneInput.acceleration.z * -1) * sensitivity);
tilt = ((iPhoneInput.acceleration.x - zeroAngle) * sensitivity);
if (turn > tiltDeadStick){
receiver.gameObject.SendMessage ("TiltRight");
} //end if
if (turn < -(tiltDeadStick)){
receiver.gameObject.SendMessage ("TiltLeft");
} //end if
if (tilt > tiltDeadStick){
receiver.gameObject.SendMessage ("TiltForward");
} //end if
if (tilt < -(tiltDeadStick)){
receiver.gameObject.SendMessage ("TiltBack");
} //end if
} //end Update
I set the `receiver` to the Indicator Manager object. My understanding is that doing this will allow me to send a message from the Tilt Manager (ex. if I tilt the iPhone forward, sends a message "TiltForward") to Indicator Manager, which will then look for a method (function) called TiltForward and execute the contents of that method. This seems to be working so far.
How do I make is so I don't need a specific receiver? I would like to have objects 'subscribe' to events and listen for them; this way I can have multiple objects listen for say a "TiltForward" message and execute different code when they get it. (Ex. Indicator Manager gets the message, fades a graphic in on the screen, while the player GO gets the message and rotates on the X axis along with the movement) As a note, I have looked at the messaging stuff on the wiki; I am looking for an easier to configure solution. Can I just use a GO array for the receiver? Would Unity then iterate through the objects in the array, sending the message to each one? Is this efficient at all or terribly slow?