Game Development Reference
In-Depth Information
The following code sample 4-3 demonstrates a sample Listener interface:
01 using UnityEngine;
02 using System.Collections;
03 //-----------------------------------------------------------
04 //Enum defining all possible game events
05 //More events should be added to the list
06 public enum EVENT_TYPE {GAME_INIT,
07 GAME_END,
08 AMMO_EMPTY,
09 HEALTH_CHANGE,
10 DEAD};
11 //-----------------------------------------------------------
12 //Listener interface to be implemented on Listener classes
13 public interface IListener
14 {
15 //Notification function invoked when events happen
16 void OnEvent(EVENT_TYPE Event_Type, Component Sender, Object Param
= null);
17 }
18 //-----------------------------------------------------------
The following are the comments on the code sample 4-3:
Lines 06-10 : This enumeration should define a complete list of all possible
game events that could be raised. The sample code lists only five game
events: GAME_INIT , GAME_END , AMMO_EMPTY , HEALTH_CHANGE , and DEAD .
Your game will presumably have many more. You don't actually need to
use enumerations for encoding events; you could just use integers. But I've
used enumerations to improve event readability in code.
Lines 13-17 : The Listener interface is defined as IListener using the C#
interfaces. It supports just one event, namely OnEvent . This function will
be inherited by all derived classes and will be invoked by the manager
whenever an event occurs for which the listener is registered. Notice that
OnEvent is simply a function prototype; it has no body.
More information on C# interfaces can be found at http://msdn.
microsoft.com/en-us/library/ms173156.aspx .
 
Search WWH ::




Custom Search