I have two objects in a scene, one called Input Manager and the other called Indicator Manager. on Input Manager, I have the following:
I set the
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?
asked May 13 '10 at 03:17 PM
You can use SendMessageOptions.DontRequireReceiver as the last parameter of your SendMessage to allow sending to an object without a receiver
SendMessage however is pretty slow (in the area of 250 times slower than a normal function call) - i would recommend against using it for an event system if possible
Your idea about a GameObject array however would work, though again, not the fastest of implementations
answered May 13 '10 at 03:34 PM
To answer the title question:
To answer the actual question, messaging appears to be the wrong approach; the problem of phone tilt causing specific objects to tilt in a specific way sounds like a common behavior that should be attached to all of a certain object, that listens to the Input class for the individual object's behavior. As other posters have noted, Messaging in Unity is known to be slow; having your game objects be associated with a prefab that has this input-listening behavior attached, or programmatically adding the input-listening behavior on object creation are usually going to be significantly more performant and efficient means of getting these objects to respond to mechanical inputs.
For a given object to use a different piece of code on event receipt, I highly recommend using A) C#, and B) a little bit of inheritance to do it, as such:
Note: The below is untested code, as I am writing this at work! This setup may required tweaking to be workable.
TiltResponse Base Class:
This way, objects of a particular type all follow a specific set of rules that is implemented in one place in code, but could be programmatically attached to some newly-spawned prefab if that's the way you're going. As long as a component that fulfills the interface that consists of those tilt checks and the MonoBehaviour interface is on the same game object, you're golden.
Using SendMessage often is definitely not the fastest method out there. Rather than keeping an array of GameObjects, you can keep an array of functions that all have the same signature.
You see here that you can pass whatever function you'd like as long as it takes a TiltType as the argument. Then the function decides whether or not and how to handle the message.
answered May 13 '10 at 03:38 PM