Game Development Reference
In-Depth Information
Using the IListener interface, we now have the ability to make a listener from
any object using only class inheritance; that is, any object can now declare itself
as a listener and potentially receive events. For example, a new MonoBehaviour
component can be turned into a listener with the following code sample 4-4. This
code, as in the previous chapters, uses multiple inheritance, that is, it inherits from
two classes. More information on multiple inheritance can be found at http://www.
dotnetfunda.com/articles/show/1185/multiple-inheritance-in-csharp :
using UnityEngine;
using System.Collections;
public class MyCustomListener : MonoBehaviour, IListener
{
// Use this for initialization
void Start () {}
// Update is called once per frame
void Update () {}
//---------------------------------------
//Implement OnEvent function to receive Events
public void OnEvent(EVENT_TYPE Event_Type, Component Sender,
Object Param = null)
{
}
//---------------------------------------
}
Creating an EventManager
Any object can now be turned into a listener, as we've seen. But still the listeners must
register themselves with a manager object of some kind. Thus, it is the duty of the
manager to call the events on the listeners when the events actually happen. Let's now
turn to the manager itself and its implementation details. The manager class will be
called EventManager , as shown in the following code sample 4-5. This class, being a
persistent singleton object, should be attached to an empty GameObject in the scene
where it will be directly accessible to every other object through a static instance
property. More on this class and its usage is considered in the subsequent comments:
001 using UnityEngine;
002 using System.Collections;
003 using System.Collections.Generic;
004 //-----------------------------------
005 //Singleton EventManager to send events to listeners
006 //Works with IListener implementations
 
Search WWH ::




Custom Search