Unity事件的高级监听与广播系统

事件中心-(添加事件,移除事件,广播事件)

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EventCenter
{
    private static Dictionary<BroadCastEventType, Delegate> m_EventTable = new Dictionary<BroadCastEventType, Delegate>();//字典---用于存储对应枚举类型的委托事件

    /// <summary>
    /// 添加监听时的校验
    /// </summary>
    /// <param name="eventType">枚举类型</param>
    /// <param name="callBack">委托事件</param>
    private static void OnListenerAdding(BroadCastEventType eventType, Delegate callBack)
    {
        //添加对应的枚举类型到字典中
        if (!m_EventTable.ContainsKey(eventType))
        {
            m_EventTable.Add(eventType, null);
        }
        //获得枚举类型对应的委托事件
        Delegate d = m_EventTable[eventType];

        //如果委托不为空,委托的类型(参数个数)不等于回调类型,提示错误信息
        if (d != null && d.GetType() != callBack.GetType())
        {
            throw new Exception(string.Format("尝试为事件{0}添加不同类型的委托,当前事件所对应的委托是{1},要添加的委托类型为{2}", eventType, d.GetType(), callBack.GetType()));
        }
    }
    /// <summary>
    /// 移除监听时的校验
    /// </summary>
    /// <param name="eventType">枚举类型</param>
    /// <param name="callBack">委托事件</param>
    private static void OnListenerRemoving(BroadCastEventType eventType, Delegate callBack)

    {
        //如果字典中包含对应的枚举类型
        if (m_EventTable.ContainsKey(eventType))
        {
            Delegate d = m_EventTable[eventType];//获得枚举类型对应的委托
            if (d == null)//事件为空错误提示
            {
                throw new Exception(string.Format("移除监听错误:事件{0}没有对应的委托", eventType));
            }
            else if (d.GetType() != callBack.GetType())//类型错误提示
            {
                throw new Exception(string.Format("移除监听错误:尝试为事件{0}移除不同类型的委托,当前委托类型为{1},要移除的委托类型为{2}", eventType, d.GetType(), callBack.GetType()));
            }
        }
        else
        {
            throw new Exception(string.Format("移除监听错误:没有事件码{0}", eventType));//没有对应的枚举类型
        }
    }
    /// <summary>
    /// 移除对应的监听后,如果枚举类型下的事件为空,则移除字典里面这个枚举类型
    /// </summary>
    /// <param name="eventType"></param>
    private static void OnListenerRemoved(BroadCastEventType eventType)
    {
        if (m_EventTable[eventType] == null)
        {
            m_EventTable.Remove(eventType);
        }
    }
    //无参数的添加委托事件
    public static void AddListener(BroadCastEventType eventType, CallBack callBack)
    {
        OnListenerAdding(eventType, callBack);//校验
        m_EventTable[eventType] = (CallBack)m_EventTable[eventType] + callBack;//给字典中的委托添加方法
    }
    //一个参数的添加委托事件
    public static void AddListener<T>(BroadCastEventType eventType, CallBack<T> callBack)
    {
        OnListenerAdding(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T>)m_EventTable[eventType] + callBack;
    }
    //两个参数的添加委托事件
    public static void AddListener<T, X>(BroadCastEventType eventType, CallBack<T, X> callBack)
    {
        OnListenerAdding(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T, X>)m_EventTable[eventType] + callBack;
    }
    //三个参数的添加委托事件
    public static void AddListener<T, X, Y>(BroadCastEventType eventType, CallBack<T, X, Y> callBack)
    {
        OnListenerAdding(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T, X, Y>)m_EventTable[eventType] + callBack;
    }
    //四个参数的添加委托事件
    public static void AddListener<T, X, Y, Z>(BroadCastEventType eventType, CallBack<T, X, Y, Z> callBack)
    {
        OnListenerAdding(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T, X, Y, Z>)m_EventTable[eventType] + callBack;
    }
    //五个参数的添加委托事件
    public static void AddListener<T, X, Y, Z, W>(BroadCastEventType eventType, CallBack<T, X, Y, Z, W> callBack)
    {
        OnListenerAdding(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T, X, Y, Z, W>)m_EventTable[eventType] + callBack;
    }

    //无参数的移除委托事件
    public static void RemoveListener(BroadCastEventType eventType, CallBack callBack)
    {
        OnListenerRemoving(eventType, callBack);//移除校验
        m_EventTable[eventType] = (CallBack)m_EventTable[eventType] - callBack;//移除方法
        OnListenerRemoved(eventType);//枚举校验--为空移除枚举
    }
    //一个参数的移除委托事件
    public static void RemoveListener<T>(BroadCastEventType eventType, CallBack<T> callBack)
    {
        OnListenerRemoving(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T>)m_EventTable[eventType] - callBack;
        OnListenerRemoved(eventType);
    }
    //两个参数的移除委托事件
    public static void RemoveListener<T, X>(BroadCastEventType eventType, CallBack<T, X> callBack)
    {
        OnListenerRemoving(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T, X>)m_EventTable[eventType] - callBack;
        OnListenerRemoved(eventType);
    }
    //三个参数的移除委托事件
    public static void RemoveListener<T, X, Y>(BroadCastEventType eventType, CallBack<T, X, Y> callBack)
    {
        OnListenerRemoving(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T, X, Y>)m_EventTable[eventType] - callBack;
        OnListenerRemoved(eventType);
    }
    //四个参数的移除委托事件
    public static void RemoveListener<T, X, Y, Z>(BroadCastEventType eventType, CallBack<T, X, Y, Z> callBack)
    {
        OnListenerRemoving(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T, X, Y, Z>)m_EventTable[eventType] - callBack;
        OnListenerRemoved(eventType);
    }
    //五个参数的移除委托事件
    public static void RemoveListener<T, X, Y, Z, W>(BroadCastEventType eventType, CallBack<T, X, Y, Z, W> callBack)
    {
        OnListenerRemoving(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T, X, Y, Z, W>)m_EventTable[eventType] - callBack;
        OnListenerRemoved(eventType);
    }


    //无参数的广播事件--实现委托方法
    public static void Broadcast(BroadCastEventType eventType)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(eventType, out d))
        {
            CallBack callBack = d as CallBack;//获得对应的委托事件
            if (callBack != null)
            {
                callBack();//实现委托方法
            }
            else
            {
                throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType));
            }
        }
    }
    //一个参数的广播事件--实现委托方法
    public static void Broadcast<T>(BroadCastEventType eventType, T arg)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(eventType, out d))
        {
            CallBack<T> callBack = d as CallBack<T>;
            if (callBack != null)
            {
                callBack(arg);
            }
            else
            {
                throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType));
            }
        }
    }
    //两个参数的广播事件--实现委托方法
    public static void Broadcast<T, X>(BroadCastEventType eventType, T arg1, X arg2)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(eventType, out d))
        {
            CallBack<T, X> callBack = d as CallBack<T, X>;
            if (callBack != null)
            {
                callBack(arg1, arg2);
            }
            else
            {
                throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType));
            }
        }
    }
    //三个参数的广播事件--实现委托方法
    public static void Broadcast<T, X, Y>(BroadCastEventType eventType, T arg1, X arg2, Y arg3)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(eventType, out d))
        {
            CallBack<T, X, Y> callBack = d as CallBack<T, X, Y>;
            if (callBack != null)
            {
                callBack(arg1, arg2, arg3);
            }
            else
            {
                throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType));
            }
        }
    }
    //四个参数的广播事件--实现委托方法
    public static void Broadcast<T, X, Y, Z>(BroadCastEventType eventType, T arg1, X arg2, Y arg3, Z arg4)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(eventType, out d))
        {
            CallBack<T, X, Y, Z> callBack = d as CallBack<T, X, Y, Z>;
            if (callBack != null)
            {
                callBack(arg1, arg2, arg3, arg4);
            }
            else
            {
                throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType));
            }
        }
    }
    //五个参数的广播事件--实现委托方法
    public static void Broadcast<T, X, Y, Z, W>(BroadCastEventType eventType, T arg1, X arg2, Y arg3, Z arg4, W arg5)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(eventType, out d))
        {
            CallBack<T, X, Y, Z, W> callBack = d as CallBack<T, X, Y, Z, W>;
            if (callBack != null)
            {
                callBack(arg1, arg2, arg3, arg4, arg5);
            }
            else
            {
                throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType));
            }
        }
    }
}

创建一个用来封装所使用到的所有委托: CallBack.cs

public delegate void CallBack();//无参数的委托事件
public delegate void CallBack<T>(T arg);//一个参数的委托事件
public delegate void CallBack<T, X>(T arg1, X arg2);//两个参数的委托事件
public delegate void CallBack<T, X, Y>(T arg1, X arg2, Y arg3);//三个参数的委托事件
public delegate void CallBack<T, X, Y, Z>(T arg1, X arg2, Y arg3, Z arg4);//四个参数的委托事件
public delegate void CallBack<T, X, Y, Z, W>(T arg1, X arg2, Y arg3, Z arg4, W arg5);//五个参数的委托事件

创建一个存放事件码的类:EventType.cs

public enum BroadCastEventType
{
    
}

测试:

添加显示事件

public enum BroadCastEventType
{
    show,
}

创建Test.cs脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Test : MonoBehaviour {
 
	void Start () {
        this.gameObject.SetActive(false);
        //添加监听
        EventCenter.AddListener(BroadCastEventType.show, Show);
	}
 
    void Show()
    {
        this.gameObject.SetActive(true);
    }
 
    private void OnDestroy()
    {
        //移除监听
        EventCenter.RemoveListener(BroadCastEventType.show, Show);
    }
}

广播

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
public class Player : MonoBehaviour {
    private Button button;
 
    private void Start()
    {
        button = GetComponent<Button>();
        button.onClick.AddListener(()=> { EventCenter.Broadcast(EventType.Test); });
        
    }
 
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值