using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class ObaseverTest
{
public delegate void EventCallback(Object args);
static Dictionary<string, List<EventCallback>> EventDic = new Dictionary<string, List<EventCallback>>();
static bool OnListener(string EventName, EventCallback Callback){
List<EventCallback> Callbacks = null;
if(!EventDic.TryGetValue(EventName,out Callbacks))
{
Callbacks = new List<EventCallback>();
EventDic.Add(EventName, Callbacks);
}
Callbacks.Add(Callback);
return true;
}
static void OffListener(string eventName, EventCallback listener)
{
List<EventCallback> listeners = null;
if (EventDic.TryGetValue(eventName, out listeners))
{
listeners.Remove(listener);
if (listeners.Count <= 0)
{
EventDic.Remove(eventName);
}
}
}
static void EmitEvent(string EventName, Object args)
{
if (!EventDic.ContainsKey(EventName)) return;
List<EventCallback> events = null;
if (EventDic.TryGetValue(EventName, out events))
{
foreach(EventCallback temp in events){
temp(args);
}
}
}
}
|