Game Development Reference
In-Depth Information
Figure 3-7.
BroadcastMessage works like SendMessage, except it cascades function calls downward through the GameObject
hierarchy, rather than applying to only one GameObject
Note
If, after reading this section, you still don't feel comfortable or familiar using
SendMessage
and
BroadcastMessage
, then don't worry: both
SendMessage
and
BroadcastMessage
will be used
extensively in this topic. We'll get plenty of practice using them!
SendMessage
and
BroadcastMessage
work for both the
Component
and
GameObject
classes. When
called on GameObjects,
SendMessage
invokes a named function on
all
attached Components. For single
Components,
SendMessage
invokes a named function on only the specified Component.
Removing Listeners
The NotificationsManager, with the help of
AddListener
and
PostNotification
, can now build a list of
registered listeners for events, and further notify those listeners when their events happen. But should
we do if a Listener no longer wants to be notified about an event? What if the Listener wants to
unregister itself as a listener, removing itself from the Dictionary entirely? Right now the Notifications
Manager doesn't support this behavior. But we can easily add support for it. Consider Listing 3-11.
Listing 3-11. Removing a Listener from the Dictionary
01 //Function to remove a listener for a notification
02 public void
RemoveListener
(Component Sender, string NotificationName)
03 {
04 //If no key in dictionary exists, then exit
05 if(!Listeners.ContainsKey(NotificationName))
06 return;
07