Game Development Reference
In-Depth Information
The term function and member function mean the same thing when
the function belongs to a class. A function that belongs to a class is
said to be a member function.
We've seen that SendMessage invokes a specified function across all components
attached to a single GameObject . BroadcastMessage incorporates the SendMessage
behavior and goes a stage further, that is, it invokes a specified function for all
components on GameObject and then repeats this process recursively for all child
objects in the scene hierarchy, cascading downwards to all children.
More information on SendMessage and BroadcastMessage can be found at
http://docs.unity3d.com/ScriptReference/GameObject.SendMessage.
html and http://docs.unity3d.com/ScriptReference/Component.
BroadcastMessage.html .
Relection
SendMessage and BroadcastMessage are effective ways
to facilitate inter-object communication and inter-component
communication, that is, it's a great way to make components
talk to one another if they need to, to synchronize behavior
and recycle functionality. However, both SendMessage and
BroadcastMessage rely internally on a C# feature known as
reflection. . By invoking a function using a string, your application
is required to look at itself at runtime (to reflect), searching its code
for the intended function to run. This process is computationally
expensive compared to running a function in the normal way.
For this reason, seek to minimize the usage of SendMessage
and BroadcastMessage , especially during Update events or
other frame-based scenarios, as the impact on performance can be
significant. This doesn't mean you should never use them. There
might be times when their use is rare, infrequent, and convenient
and has practically no appreciable impact. However, later chapters
in this topic will demonstrate alternative and faster techniques using
delegates and interfaces.
If you'd like more information on C# and its usage before proceeding further
with this topic, then I recommend the following sources:
Learning C# by Developing Games with Unity 3D Beginner's Guide , Terry
Norton , Packt Publishing
 
Search WWH ::




Custom Search