事件中心派发机制,注册,移除,已经移除的,广播机制

另一个版的的事件中心,注册,移除,已经移除的,广播机制,
总体来说比上一般更偏向商业级.
这里面贴了一版,可以直接拿去用

/// --------------------------------------------------------------
/// 回调管理 不用Action因为自带的最多4个参数
/// --------------------------------------------------------------
/// 
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);
using System;
using System.Collections.Generic;

public class EventCenter
{
    static Dictionary<ushort, Delegate> m_EventTable = new Dictionary<ushort, Delegate>();

    #region 基础方法

    static void OnListenerAdding(ushort eventType, Delegate callBack)
    {
        if (!m_EventTable.ContainsKey(eventType))
        {
            m_EventTable.Add(eventType, null);
        }
        Delegate del = m_EventTable[eventType];
        if (del != null && del.GetType() != callBack.GetType())
        {
            throw new Exception(string.Format("尝试为事件{0}添加不同类型的委托,当前事件对应的的委托是{1},要添加的委托类型是{2}", eventType, del.GetType(), callBack));
        }
    }

    static void OnListenerRemoving(ushort eventType, Delegate callBack)
    {
        if (m_EventTable.ContainsKey(eventType))
        {
            Delegate dele = m_EventTable[eventType];
            if (dele == null)
            {
                throw new Exception(string.Format("移除监听错误:事件{0}没有对应的委托", eventType));
            }
            else if (dele.GetType() != callBack.GetType())
            {
                throw new Exception(string.Format("移除监听错误:尝试为事件{0}移除不同类型的委托,当前委托类型为{1}" +
                    ",要移除的委托的类型为{2}", eventType, dele.GetType(), callBack.GetType()));
            }
        }
        else
        {
            throw new Exception(string.Format("移除监听错误:没有事件码{0}", eventType));
        }
    }

    static void OnListenerRemoved(ushort eventType)
    {
        if (m_EventTable[eventType] == null)
        {
            m_EventTable.Remove(eventType);
        }
    }

    #endregion

    #region 添加监听

    public static void AddListener(ushort eventType, CallBack callBack)
    {
        OnListenerAdding(eventType, callBack);
        m_EventTable[eventType] = (CallBack)m_EventTable[eventType] + callBack;
    }

    public static void AddListener<T>(ushort eventType, CallBack<T> callBack)
    {
        OnListenerAdding(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T>)m_EventTable[eventType] + callBack;
    }

    public static void AddListener<T, X>(ushort 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>(ushort 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>(ushort eventType, CallBack<T, X, Y, Z> callBack)
    {
        OnListenerAdding(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T, X, Y, Z>)m_EventTable[eventType] + callBack;
    }

    #endregion

    #region  移除监听
    public static void RemoveListener(ushort eventType, CallBack callBack)
    {
        OnListenerRemoving(eventType, callBack);
        m_EventTable[eventType] = (CallBack)m_EventTable[eventType] - callBack;
        OnListenerRemoved(eventType);
    }

    public static void RemoveListener<T>(ushort eventType, CallBack<T> callBack)
    {
        OnListenerRemoving(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T>)m_EventTable[eventType] - callBack;
        OnListenerRemoved(eventType);
    }

    public static void RemoveListener<T, X>(ushort 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>(ushort 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>(ushort eventType, CallBack<T, X, Y, Z> callBack)
    {
        OnListenerRemoving(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T, X, Y, Z>)m_EventTable[eventType] - callBack;
        OnListenerRemoved(eventType);
    }

    #endregion

    #region 广播方法
    public static void BroadCaster(ushort eventType)
    {
        Delegate dele;
        if (m_EventTable.TryGetValue(eventType, out dele))
        {
            CallBack callBack = dele as CallBack;
            if (callBack != null)
            {
                callBack();
            }
            else
            {
                throw new Exception(string.Format("广播事件错误:事件{0}对应的委托具有不同类型", eventType));
            }
        }
    }

    public static void BroadCaster<T>(ushort eventType, T arg)
    {
        Delegate dele;
        if (m_EventTable.TryGetValue(eventType, out dele))
        {
            CallBack<T> callBack = dele as CallBack<T>;
            if (callBack != null)
            {
                callBack(arg);
            }
            else
            {
                throw new Exception(string.Format("广播事件错误:事件{0}对应的委托具有不同类型", eventType));
            }
        }
    }

    public static void BroadCaster<T, X>(ushort eventType, T arg, X arg2)
    {
        Delegate dele;
        if (m_EventTable.TryGetValue(eventType, out dele))
        {
            CallBack<T, X> callBack = dele as CallBack<T, X>;
            if (callBack != null)
            {
                callBack(arg, arg2);
            }
            else
            {
                throw new Exception(string.Format("广播事件错误:事件{0}对应的委托具有不同类型", eventType));
            }
        }
    }

    public static void BroadCaster<T, X, Y>(ushort eventType, T arg, X arg2, Y arg3)
    {
        Delegate dele;
        if (m_EventTable.TryGetValue(eventType, out dele))
        {
            CallBack<T, X, Y> callBack = dele as CallBack<T, X, Y>;
            if (callBack != null)
            {
                callBack(arg, arg2, arg3);
            }
            else
            {
                throw new Exception(string.Format("广播事件错误:事件{0}对应的委托具有不同类型", eventType));
            }
        }
    }

    public static void BroadCaster<T, X, Y, Z>(ushort eventType, T arg, X arg2, Y arg3, Z arg4)
    {
        Delegate dele;
        if (m_EventTable.TryGetValue(eventType, out dele))
        {
            CallBack<T, X, Y, Z> callBack = dele as CallBack<T, X, Y, Z>;
            if (callBack != null)
            {
                callBack(arg, arg2, arg3, arg4);
            }
            else
            {
                throw new Exception(string.Format("广播事件错误:事件{0}对应的委托具有不同类型", eventType));
            }
        }
    }

    public static void BroadCaster<T, X, Y, Z, W>(ushort eventType, T arg, X arg2, Y arg3, Z arg4, W arg5)
    {
        Delegate dele;
        if (m_EventTable.TryGetValue(eventType, out dele))
        {
            CallBack<T, X, Y, Z, W> callBack = dele as CallBack<T, X, Y, Z, W>;
            if (callBack != null)
            {
                callBack(arg, arg2, arg3, arg4, arg5);
            }
            else
            {
                throw new Exception(string.Format("广播事件错误:事件{0}对应的委托具有不同类型", eventType));
            }
        }
    }

    #endregion

}

//简单模拟调用

using Spine.Unity;
using System.Collections.Generic;
using UnityEngine;

public class OrderPlay : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
EventCenter.AddListener(1, firtst);
EventCenter.AddListener(2, second);
EventCenter.AddListener(3, third);
}

private void firtst()
{
    Vector3 s0 = new Vector3(-3, 0, 0);
    Fight(slist[0], s0);
    Vector3 s1 = new Vector3(3, 0, 0);
    Fight(slist[1], s1);
}

private void Fight(SkeletonAnimation go, Vector3 v3)
{
        Debug.Log("Fight");
}



// Update is called once per frame
void Update()
{
    if (Input.GetKeyDown(KeyCode.Q))
    {
        EventCenter.Broadcast(1);
    }

    if (Input.GetKeyDown(KeyCode.W))
    {
        EventCenter.Broadcast(2);
    }

    if (Input.GetKeyDown(KeyCode.E))
    {
        EventCenter.Broadcast(3);
    }
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值