Game Development Reference
In-Depth Information
Listing 10-3. Loading Levels Additively
//Loads level 2 into existing level
Application.LoadLevelAdditive(2);
Event Handling
The event-handling system created in Chapter 3 was termed the NotificationsManager . It was
created as a Singleton object. Using this class, a Poster may dispatch a notification about an
event as it happens, such as an enemy-destroyed event, and the NotificationsManager responds
by immediately invoking event calls in any registered Listeners for that event, allowing them to act
as required. By centralizing event handling through a single managerial class, every other class
has an independent and effective way to respond to almost any event without directly affecting
the implementation of any other class. In Chapter 3, the event framework was implemented by
using the SendMessage function of GameObject . This function can be called on a per object basis
and invokes any functions of a matching name on any components attached to the object. See
Chapter 3 for a refresher if required, and also the relevant Unity documentation at
http://docs.unity3d.com/Documentation/ScriptReference/GameObject.SendMessage.html .
The SendMessage function works like the BroadcastMessage function, except that BroadcastMessage
cascades invocation downward through all child objects in the scene hierarchy.
In many situations, as with CMOD, you can get away with using SendMessage and BroadcastMessage
without any problems. But both of these functions rely on deeper underlying code that can be
computationally expensive, leading to performance issues. This primarily (though not exclusively)
results from an internal process known as reflection . If your game makes frequent and extensive
use of either SendMessage or BroadcastMessage , and you're experiencing performance issues, then
it's time to seriously rethink your code and to consider alternatives. And indeed, there are many
alternatives.
The biggest problems that we faced when coding the event-handling system was how to invoke
events on listeners that could potentially be of any data type. A Listener could be any kind of class,
and because we can't know in advance which type it is, then we don't know which functions it
supports and which functions we can call. The SendMessage and BroadcastMessage methods allow
us to easily get around this problem, because they simply require us to provide a function name by
string , and they execute that function for us across all components in an object, regardless of object
data types—provided the function exists in the first place. So, when considering alternatives for
event handling, we need solutions that will allow us to achieve a similar kind of behavior. There are at
least two possibilities in C: interfaces and delegates .
Interfaces
Interfaces are a powerful feature native to C#, and not Unity specifically. They let you create a
special kind of runtime polymorphism and they offer a solution to effective event handling. Let's see
how in practice. In short, with a C# interface you start by defining one or more functions together, as
shown in Listing 10-4.
 
Search WWH ::




Custom Search