Game Development Reference
In-Depth Information
//Add listener
public void AddListener(ListenerDelegate Listener)
{
Listeners.Add(Listener);
}
void PostNotification(NotificationsManager.EVENT_TYPE EType, int Param)
{
//Notify all listeners
for(int i=0; i<Listeners.Count; i++)
{
//Call delegate like function
Listeners[i](EType, Param);
}
}
void Update()
{
//Notify event system on keypress
if(Input.anyKeyDown)
PostNotification(EVENT_TYPE.ON_KEYPRESS,0);
}
}
Listing 10-9. Listener.cs: Creates a Listener with Delegates
using UnityEngine;
using System.Collections;
public class Listener : MonoBehaviour
{
private NotificationsManager NM = null;
void Start()
{
//Get notifications manager
NM = GetComponent<NotificationsManager>();
//Add as listener
NM.AddListener(OnEventCall);
}
//Function prototype matches delegate
public void OnEventCall(NotificationsManager.EVENT_TYPE EType, int Param)
{
Debug.Log("Event Called");
}
}
Search WWH ::




Custom Search