Game Development Reference
In-Depth Information
Note The NotificationsManager developed will be based loosely on the well-known, third-party
class NotificationsCenter , available online for free from the Unity Community web site at
http://wiki.unity3d.com/index.php?title=CSharpNotificationCenter .
However, although NotificationsManager will be based on NotificationsCenter , it will take a slightly
different approach, as well as implement additional functionality. It's worth mentioning, too, that you don't
need to be familiar with the NotificationsCenter to implement the NotificationsManager here. We'll
explore NotificationsManager creation from the very beginning.
There are two significant and distinct advantages to this event-handling system:
It is centralized. There should be one and just one instance of
NoticationsManager in memory at any time, and this instance carries the entire
responsibility for event handling between objects. Its dedicated purpose is
to broadcast all event notifications to all appropriate objects. Thus, through
centralization—by assigning the workload of event handling to a single class
in this way—we achieve two important benefits. First, we improve debugging
simplicity because, if there's ever a problem with event-handling, we
immediately know where in our code to start our bug search: specifically inside
the NoticationsManager class. And second, we simplify all other classes and
objects because none of them need concern themselves with event handling
anymore, now that we have a NoticationsManager to do that work for us.
It is abstract and recyclable. The NotificationsManager distills the concepts
of events and event handling into a single class in both a general and abstract
way that makes no specific demands or requirements upon other classes.
In short, it doesn't tell other classes how they should be implemented—their
implementation is separate and independent from the NotificationsManager.
This means the NotificationsManager can be used not just for event-handling in
CMOD specifically, but more widely in practically any kind of game. It means, in
theory, we can import NotificationsManager.cs into any other Unity project and
use it straightaway to manage events, without making any code changes. That,
in itself, saves us lots of work and time!
Getting Started with NotificationsManager
Let's now put our NotificationsManager plan into practice inside Unity. To create this class, we'll
need to start by creating a new C# source file. I'll explain how to do that here in case you need a
refresher, but for subsequent classes created throughout the rest of the topic, I'll assume you already
know how to do it. To create a new C# source file, right-click the Scripts folder of the Project panel,
and select Create C# Script from the context menu (as shown in Figure 3-3 ). Alternatively, you
can select Assets Create C# Script from the application menu. Be sure to name the file
NotificationsManager.cs .
 
Search WWH ::




Custom Search