EventManage

作者: ysl176 | 来源:发表于2016-02-02 17:59 被阅读78次

    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();

    }

    }

    }

    相关文章

      网友评论

        本文标题:EventManage

        本文链接:https://www.haomeiwen.com/subject/yytekttx.html