using UnityEngine;
using System.Collections.Generic;
using System.Collections;
using UnityEngine.Events;
public class EventsManager : MonoBehaviour {
private Dictionary eventsDic;
private static EventsManager eventManager;
public static EventsManager instance
{
get
{
if (!instance)
{
eventManager = FindObjectOfType(typeof(EventsManager)) as EventsManager;
if (!eventManager)
{
Debug.Log("EMr Need To Add GameObject ");
}
else
{
}
}
return eventManager;
}
}
void Init()
{
eventsDic = new Dictionary();
}
public static void StarListening(string name,UnityAction action)
{
UnityEvent thisEvent = null;
if (instance.eventsDic.TryGetValue(name, out thisEvent))
{
thisEvent.AddListener(action);
}
else
{
thisEvent = new UnityEvent();
thisEvent.AddListener(action);
instance.eventsDic.Add(name,thisEvent);
}
}
public static void StopListening(string name,UnityAction action)
{
if (eventManager == null)
{
return;
}
UnityEvent thisEvent = null;
if (instance.eventsDic.TryGetValue(name, out thisEvent))
{
thisEvent.RemoveListener(action);
}
}
public static void TriggerEvent(string name)
{
UnityEvent thisEvent = null;
if (instance.eventsDic.TryGetValue(name, out thisEvent))
{
thisEvent.Invoke();
}
}
}
网友评论