Unity 如何封装自己的事件派发类

方法 功能 备注
Watcher.on 注册监听 注册一个事件监听j
Watcher.off 移除监听 移除一个事件监听
Watcher.Dispatch 派发事件 广播事件

 

前言:

由事件类和监听接口组成

自定义一个事件前,必须提供一个事件的监听方法以及一个事件标签。

 

好处:

各个模块专心的做自己的事情,不需要过问其他模块的事情

解耦

 

代码部分~ 

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


public delegate void EventCallBack();
public delegate void EventCallBack(T arg1);
public delegate void EventCallBack(T arg1, W arg2);
public delegate void EventCallBack(T arg1, W arg2, E arg3);
public delegate void EventCallBack(T arg1, W arg2, E arg3, R arg4);
public delegate void EventCallBack(T arg1, W arg2, E arg3, R arg4, Y arg5);


public class Watcher
{

    private static Dictionary m_EventTable = new Dictionary();

    static void TryAddListen(string 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()));
        }
    }

    static void TryRemoveListen(string 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, callBack));
            }
        }
        else
        {
            throw new Exception(string.Format("移除监听错误:没有事件码{0}", eventType));
        }
    }

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

    public static void On(string eventType, EventCallBack callback)
    {
        TryAddListen(eventType, callback);
        m_EventTable[eventType] = (EventCallBack)m_EventTable[eventType] + callback;
    }

    public static void On(string eventType, EventCallBack callback)
    {
        TryAddListen(eventType, callback);
        m_EventTable[eventType] = (EventCallBack)m_EventTable[eventType] + callback;
    }

    public static void On(string eventType, EventCallBack callback)
    {
        TryAddListen(eventType, callback);
        m_EventTable[eventType] = (EventCallBack)m_EventTable[eventType] + callback;
    }

    public static void On(string eventType, EventCallBack callback)
    {
        TryAddListen(eventType, callback);
        m_EventTable[eventType] = (EventCallBack)m_EventTable[eventType] + callback;
    }


    public static void On(string eventType, EventCallBack callback)
    {
        TryAddListen(eventType, callback);
        m_EventTable[eventType] = (EventCallBack)m_EventTable[eventType] + callback;
    }

    public static void On(string eventType, EventCallBack callback)
    {
        TryAddListen(eventType, callback);
        m_EventTable[eventType] = (EventCallBack)m_EventTable[eventType] + callback;
    }


    public static void Off(string eventType, EventCallBack callback = null)
    {
        TryRemoveListen(eventType, callback);
        m_EventTable[eventType] = (EventCallBack)m_EventTable[eventType] - callback;
        OnListenRemove(eventType);
    }
    public static void Off(string eventType, EventCallBack callBack)
    {
        TryRemoveListen(eventType, callBack);
        m_EventTable[eventType] = (EventCallBack)m_EventTable[eventType] - callBack;
        OnListenRemove(eventType);

    }
    public static void Off(string eventType, EventCallBack callBack)
    {
        TryRemoveListen(eventType, callBack);
        m_EventTable[eventType] = (EventCallBack)m_EventTable[eventType] - callBack;
        OnListenRemove(eventType);
    }
    public static void Off(string eventType, EventCallBack callBack)
    {
        TryRemoveListen(eventType, callBack);
        m_EventTable[eventType] = (EventCallBack)m_EventTable[eventType] - callBack;
        OnListenRemove(eventType);
    }
    public static void Off(string eventType, EventCallBack callBack)
    {
        TryRemoveListen(eventType, callBack);
        m_EventTable[eventType] = (EventCallBack)m_EventTable[eventType] - callBack;
        OnListenRemove(eventType);
    }
    public static void Off(string eventType, EventCallBack callBack)
    {
        TryRemoveListen(eventType, callBack);
        m_EventTable[eventType] = (EventCallBack)m_EventTable[eventType] - callBack;
        OnListenRemove(eventType);
    }


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

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

    public static void Dispatch(string eventType, T arg1, W arg2)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(eventType, out d))
        {
            EventCallBack callBack = d as EventCallBack;
            if (callBack != null)
            {
                callBack(arg1, arg2);
            }
            else
            {
                throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType));
            }
        }
    }

    public static void Dispatch(string eventType, T arg1, W arg2, E arg3)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(eventType, out d))
        {
            EventCallBack callBack = d as EventCallBack;
            if (callBack != null)
            {
                callBack(arg1, arg2, arg3);
            }
            else
            {
                throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType));
            }
        }
    }

    public static void Dispatch(string eventType, T arg1, W arg2, E arg3, R arg4)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(eventType, out d))
        {
            EventCallBack callBack = d as EventCallBack;
            if (callBack != null)
            {
                callBack(arg1, arg2, arg3, arg4);
            }
            else
            {
                throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType));
            }
        }
    }

    public static void Dispatch(string eventType, T arg1, W arg2, E arg3, R arg4, Y arg5)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(eventType, out d))
        {
            EventCallBack callBack = d as EventCallBack;
            if (callBack != null)
            {
                callBack(arg1, arg2, arg3, arg4, arg5);
            }
            else
            {
                throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType));
            }
        }
    }



}

你可能感兴趣的:(unity,unity3d,c#,事件驱动,事件处理机制)