Unity自定义事件(自定义回调函数参数个数)

首先实现的功能很简单,就是点击按钮,文本显示(内心吐槽:这么简单还自定义干嘛啊!!!当然是为了学习QAQ) 

Unity自定义事件(自定义回调函数参数个数)_第1张图片

 第一步:创建一个枚举类型(测试就写一个类型了)

public enum EventType
{
    eventText
}

第二步:创建delegate类(用做泛型)

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 EventCenten
{
    private static Dictionary m_EventTable = new Dictionary();

    //提取相同的代码段,使用Delegate就可以让方法通用(无论函数是否带参)
    static void AddingListen(EventType 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 RemovingListen(EventType 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(EventType eventType)
    {
        if (m_EventTable[eventType] == null)
        {
            m_EventTable.Remove(eventType);
        }
    }
    /// 
    /// 添加事件
    /// 
    /// 
    /// 

    //无参的添加方法
    public static void AddListen(EventType eventType, EventCallBack callBack)
    {
        AddingListen(eventType, callBack);
        //多播,当键相同后会产生多重广播(调用多个事件)
        m_EventTable[eventType] = (EventCallBack)m_EventTable[eventType] + callBack;
    }

    //一个参的添加方法
    public static void AddListen(EventType eventType, EventCallBack callBack)
    {
        AddingListen(eventType, callBack);
        //多播,当键相同后会产生多重广播(调用多个事件)
        m_EventTable[eventType] = (EventCallBack)m_EventTable[eventType] + callBack;
    }
    //两个参的添加方法
    public static void AddListen(EventType eventType, EventCallBack callBack)
    {
        AddingListen(eventType, callBack);
        //多播,当键相同后会产生多重广播(调用多个事件)
        m_EventTable[eventType] = (EventCallBack)m_EventTable[eventType] + callBack;
    }
    //三个参的添加方法
    public static void AddListen(EventType eventType, EventCallBack callBack)
    {
        AddingListen(eventType, callBack);
        //多播,当键相同后会产生多重广播(调用多个事件)
        m_EventTable[eventType] = (EventCallBack)m_EventTable[eventType] + callBack;
    }
    //四个参的添加方法
    public static void AddListen(EventType eventType, EventCallBack callBack)
    {
        AddingListen(eventType, callBack);
        //多播,当键相同后会产生多重广播(调用多个事件)
        m_EventTable[eventType] = (EventCallBack)m_EventTable[eventType] + callBack;
    }
    //五个参的添加方法
    public static void AddListen(EventType eventType, EventCallBack callBack)
    {
        AddingListen(eventType, callBack);
        //多播,当键相同后会产生多重广播(调用多个事件)
        m_EventTable[eventType] = (EventCallBack)m_EventTable[eventType] + callBack;
    }


    /// 
    /// 移除事件
    /// 
    /// 
    /// 
    //无参的移除方法
    public static void RemoveListen(EventType eventType, EventCallBack callBack)
    {
        AddingListen(eventType, callBack);
        m_EventTable[eventType] = (EventCallBack)m_EventTable[eventType] - callBack;
        OnListenRemove(eventType);
    }
    //一个参的移除方法
    public static void RemoveListen(EventType eventType, EventCallBack callBack)
    {
        AddingListen(eventType, callBack);
        m_EventTable[eventType] = (EventCallBack)m_EventTable[eventType] - callBack;
        OnListenRemove(eventType);
    }
    //两个参的移除方法
    public static void RemoveListen(EventType eventType, EventCallBack callBack)
    {
        AddingListen(eventType, callBack);
        m_EventTable[eventType] = (EventCallBack)m_EventTable[eventType] - callBack;
        OnListenRemove(eventType);
    }
    //三个参的移除方法
    public static void RemoveListen(EventType eventType, EventCallBack callBack)
    {
        AddingListen(eventType, callBack);
        m_EventTable[eventType] = (EventCallBack)m_EventTable[eventType] - callBack;
        OnListenRemove(eventType);
    }
    //四个参的移除方法
    public static void RemoveListen(EventType eventType, EventCallBack callBack)
    {
        AddingListen(eventType, callBack);
        m_EventTable[eventType] = (EventCallBack)m_EventTable[eventType] - callBack;
        OnListenRemove(eventType);
    }
    //五个参的移除方法
    public static void RemoveListen(EventType eventType, EventCallBack callBack)
    {
        AddingListen(eventType, callBack);
        m_EventTable[eventType] = (EventCallBack)m_EventTable[eventType] - callBack;
        OnListenRemove(eventType);
    }

    /// 
    /// 广播事件
    /// 
    /// 

    //无参广播事件
    public static void BroadCast(EventType 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 BroadCast(EventType 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 BroadCast(EventType 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 BroadCast(EventType 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 BroadCast(EventType 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 BroadCast(EventType 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));
            }
        }
    }
}

第四步(开始使用了):挂载到text上

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

public class UseText : MonoBehaviour
{

    public static UseText _instance;

    void Awake()
    {
        _instance = this;
    }
    // Use this for initialization
    void Start()
    {
        gameObject.SetActive(false);
        EventCenter.AddListen(EventType.eventText,show);
    }

    // Update is called once per frame
    void Update()
    {

    }
    void OnDestroy()
    {
        EventCenter.RemoveListen(EventType.eventText,show);
    }
    public void show(string str,string str1,int i,float j,float b)
    {
        gameObject.SetActive(true);
        transform.GetComponent().text =str+str1+i+j+b;
    }
}

第五步:挂载到button上

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

public class ButtonClick : MonoBehaviour
{

    // Use this for initialization
    void Start()
    {
        transform.GetComponent

点击按钮弹出消息,功能已经实现,如果有不懂点击下这个网址http://www.sikiedu.com/my/course/304

你可能感兴趣的:(Unity,Unity学习)