Game Development Reference
In-Depth Information
09 // Use this for initialization
10 void Start ()
11 {
12 //Register this object as a listener for keyboard notifications
13 if(Notifications!=null)
14
Notifications.AddListener(this, "OnKeyboardInput");
15 }
16
17 //This function will be called by the NotificationsManager when keyboard events occur
18
public void OnKeyboardInput(Component Sender)
19 {
20 //Print to console
21 Debug.Log("Keyboard Event Occurred");
22 }
23 }
Be sure to drag and drop the
Listener.cs
file onto the
Listener
object in the scene, and then assign
the public
Notifications
member in the Object Inspector to the NotificationsManager component,
as we did for the
Poster
object. Notice in Listing 3-15 that a correspondence exists between the two
classes,
Poster
and
Listener
, regarding the event name as a string. The Poster posts notifications
for event
OnKeyboardInput
, and the Listener registers for the same event, as well as implements a
function of that name, which will be invoked by the NotificationsManager using the
SendMessage
API
function (see Figure
3-12
).
Figure 3-12.
Remember to assign the Notifications member of the Listener to the single instance of NotificationsManager in
the scene