事件的监听和广播

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_33950757/article/details/102156043
/// <summary>
/// 事件类型
/// </summary>
public enum EventType
{
    ShowText,
}
public delegate void CallBack();
public delegate void CallBack<T>(T t);
public delegate void CallBack<T,X>(T t,X x);
public delegate void CallBack<T, X,Y>(T t, X x,Y y);
using System;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 时间处理中心
/// </summary>
public class EventCenter
{
    public static Dictionary<EventType,Delegate> eventTable=new Dictionary<EventType, Delegate>();


    #region 添加监听

    private static void OnAddListener(EventType eventType, Delegate callBack)
    {
        if (!eventTable.ContainsKey(eventType))
        {
            eventTable.Add(eventType, null);
        }

        if (eventTable[eventType] != null && eventTable[eventType].GetType() != callBack.GetType())
        {
            throw new Exception(eventType + "事件添加不了" + callBack);
        }
    }

    /// <summary>
    /// 添加监听
    /// </summary>
    public static void AddListener(EventType eventType,CallBack callBack)
    {
        OnAddListener(eventType, callBack);
        eventTable[eventType] = (CallBack)eventTable[eventType]+callBack;
    }


    /// <summary>
    /// 带一个参数监听
    /// </summary>
    public static void AddListener<T>(EventType eventType, CallBack<T> callBack)
    {
        OnAddListener(eventType, callBack);
        eventTable[eventType] = (CallBack<T>)eventTable[eventType] + callBack;
    }

    /// <summary>
    /// 带两个参数监听
    /// </summary>
    public static void AddListener<T,X>(EventType eventType, CallBack<T,X> callBack)
    {
        OnAddListener(eventType, callBack);
        eventTable[eventType] = (CallBack<T,X>)eventTable[eventType] + callBack;
    }
    #endregion


    #region 移除监听
    private static void OnRemoveListener(EventType eventType, Delegate callBack)
    {
        if (eventTable.ContainsKey(eventType))
        {
            Delegate d = eventTable[eventType];
            if (d == null)
            {
                throw new Exception(d + "不存在移除不了");
            }
            else if (d.GetType() != callBack.GetType())
            {
                throw new Exception("移除类型不一致");
            }

        }
        else
        {
            throw new Exception(eventType + "不存在移除不了");
        }
    }


    private static void OnRemoveListenered(EventType eventType)
    {
        if (eventTable[eventType] == null)
        {
            eventTable.Remove(eventType);
        }
    }

    /// <summary>
    /// 移除监听
    /// </summary>
    /// <param name="eventType"></param>
    /// <param name="callBack"></param>
    public static void RemoveListener(EventType eventType, CallBack callBack)
    {
        OnRemoveListener(eventType, callBack);
        eventTable[eventType] = (CallBack)eventTable[eventType] - callBack;
        OnRemoveListenered(eventType);
    }


    /// <summary>
    /// 移除一个参数的监听
    /// </summary>
    /// <param name="eventType"></param>
    /// <param name="callBack"></param>
    public static void RemoveListener<T>(EventType eventType, CallBack<T> callBack)
    {
        OnRemoveListener(eventType, callBack);
        eventTable[eventType] = (CallBack<T>)eventTable[eventType] - callBack;
        OnRemoveListenered(eventType);
    }

    /// <summary>
    /// 移除两个参数的监听
    /// </summary>
    /// <param name="eventType"></param>
    /// <param name="callBack"></param>
    public static void RemoveListener<T,X>(EventType eventType, CallBack<T,X> callBack)
    {
        OnRemoveListener(eventType, callBack);
        eventTable[eventType] = (CallBack<T,X>)eventTable[eventType] - callBack;
        OnRemoveListenered(eventType);
    }
    #endregion


    #region 广播监听

    /// <summary>
    /// 广播监听
    /// </summary>
    public static void Broadcast(EventType eventType)
    {
        Delegate d = null;
        if (eventTable.TryGetValue(eventType, out d))
        {
           CallBack callBack= d as CallBack;
           if (callBack!=null)
           {
               callBack();
           }
           else
           {
               throw new Exception( "广播不了");
           }
        }
    }

    /// <summary>
    /// 广播带一个参数的监听
    /// </summary>
    public static void Broadcast<T>(EventType eventType,T arg)
    {
        Delegate d = null;
        if (eventTable.TryGetValue(eventType, out d))
        {
            CallBack<T> callBack = d as CallBack<T>;
            if (callBack != null)
            {
                callBack(arg);
            }
            else
            {
                throw new Exception("广播不了");
            }
        }
    }

    /// <summary>
    /// 广播带两个参数的监听
    /// </summary>
    public static void Broadcast<T,X>(EventType eventType, T arg,X arg1)
    {
        Delegate d = null;
        if (eventTable.TryGetValue(eventType, out d))
        {
            CallBack<T,X> callBack = d as CallBack<T,X>;
            if (callBack != null)
            {
                callBack(arg,arg1);
            }
            else
            {
                throw new Exception("广播不了");
            }
        }
    }
    #endregion

}
using UnityEngine;
using UnityEngine.UI;

public class ShowButton : MonoBehaviour 
{
    void Awake()
    {
        GetComponent<Button>().onClick.AddListener(delegate() { EventCenter.Broadcast<string,int>(EventType.ShowText,"这个是监听",100); });
    }
}
using UnityEngine;
using UnityEngine.UI;

public class ShowText : MonoBehaviour 
{
    void Awake()
    {
        EventCenter.AddListener<string,int>(EventType.ShowText, Show);
    }

    private void Show(string str,int a)
    {
       
        this.GetComponent<Text>().text = str+a;
    }

   
    private void OnDestroy()
    {
        EventCenter.RemoveListener<string,int>(EventType.ShowText, Show);
    }

}

猜你喜欢

转载自blog.csdn.net/weixin_33950757/article/details/102156043