C#版消息全局监听与通知

45 篇文章 0 订阅
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 消息传递
/// </summary>
public class EventDispatcher
{
    public delegate void EventHandler(params object[] objs);
    private Dictionary<EventEnum, EventHandler> listeners = new Dictionary<EventEnum, EventHandler>();

    private static EventDispatcher globalDispatcher = new EventDispatcher();
    public static EventDispatcher Global
    {
        get
        {
            return globalDispatcher;
        }
    }

    void AddEventListener(EventEnum evt, EventHandler handler)
    {
        if (handler == null)
        {
            Debug.LogWarningFormat("Cannot bind a null handler with message {0}", evt);
            return;
        }

        if (listeners.ContainsKey(evt))
            listeners[evt] += handler;
        else
            listeners.Add(evt, handler);
    }

    void RemoveEventListener(EventEnum evt, EventHandler handler)
    {
        if (handler == null)
        {
            Debug.LogWarningFormat("Cannot unbind a null handler with event {0}", evt);
            return;
        }

        if (listeners.ContainsKey(evt))
        {
            listeners[evt] -= handler;
            if (listeners[evt] == null)
                listeners.Remove(evt);
        }
        else
        {
            Debug.LogWarningFormat("There is no handler {0} bind with event {1}", handler.ToString(), evt);
        }
    }

    private const int MAX_STACK_DEEP = 8;
    private int stackDeep = 0;
    private readonly string szErrorMessage = "DispatchEvent Error, Event:{0}, Error:{1}\n{2}";

    public void DispatchEvent(EventEnum evt, params object[] objs)
    {

        try
        {
            stackDeep++;
            if (stackDeep > MAX_STACK_DEEP) // 避免形成消息环
                throw new System.StackOverflowException("Event stack overflow");

            if (listeners.ContainsKey(evt))
            {
                listeners[evt](objs);
            }
        }
        catch (System.Exception e)
        {
            Debug.LogErrorFormat(szErrorMessage, evt, e.Message, e.StackTrace);
        }
        finally
        {
            stackDeep--;
        }
    }

    public void Regist(EventEnum evt, EventHandler handler)
    {
        AddEventListener(evt, handler);
    }

    public void Unregist(EventEnum evt, EventHandler handler)
    {
        RemoveEventListener(evt, handler);
    }

    public bool HasEvent(EventEnum evt)
    {
        return listeners.ContainsKey(evt);
    }

    public void ClearEvent(EventEnum evt)
    {
        if (HasEvent(evt))
        {
            listeners[evt] = null;
            listeners.Remove(evt);
        }
    }
}
public enum EventEnum
{
    TestA,
    TestB,


}




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值