unity3d无需进行装箱/拆箱的【事件管理器】

装箱/拆箱

为了解释“为什么不用object传递参数?”先简单介绍一下“装箱/拆箱”,请看下面代码:

int a = 10;
object b = a; //装箱
a = (int)b; //拆箱

第二行,会在堆上实例化一个装箱的对象,并把a的值复制进去,b 引用的就是这个对象。
第三行,会再次进行值复制,若 b 不再引用,则需等待垃圾回收。

常见的事件管理器

我们看一些常见的事件管理器使用代码:

void Start()
{
    //注册事件
    EventManager.AddEventListener("Click", OnClick);
}

public void OnClick(object data)
{
    Debug.Log("clickBlock: " + data);
}

//派发事件
EventManager.dispatchEvent("Click", 123);

如上所述,若传递的是引用类型则不会有影响。但如果传递的是值类型,就会产生上述的性能消耗。
泛型优化
我们可以通过泛型去设置参数的类型,从而不需要通过 object 类型传递参数:

public void Dispatch

void Start()
{
    //注册事件
    EventDispatcher.global.AddListener<int>("Click", OnClick);
}

public void OnClick(int data)
{
    Debug.Log("clickBlock: " + data);
    Debug.Log(gameObject.name);
}

void OnDestroy()
{
    EventDispatcher.global.RemoveListener<int>("Click",OnClick);
}
//派发事件
EventDispatcher.global.Dispatch<int>("Click", 123);

这里要注意的是一定要GameObject销毁时调用注销委托,不然可能出现如下错误
这里写图片描述

完整代码

如下:

using System;
using System.Collections.Generic;

public class EventDispatcher

{

    public static EventDispatcher global

    {

        get;

        private set;

    }

    static EventDispatcher()

    {

        global = new EventDispatcher();

    }



    private Dictionary<string, Delegate> _listeners = new Dictionary<string, Delegate>();



    public void AddListener<T1, T2, T3, T4>(string evt, Action<T1, T2, T3, T4> callback)

    {

        AddListener(evt, (Delegate)callback);

    }

    public void AddListener<T1, T2, T3>(string evt, Action<T1, T2, T3> callback)

    {

        AddListener(evt, (Delegate)callback);

    }

    public void AddListener<T1, T2>(string evt, Action<T1, T2> callback)

    {

        AddListener(evt, (Delegate)callback);

    }

    public void AddListener<T>(string evt, Action<T> callback)

    {

        AddListener(evt, (Delegate)callback);

    }

    public void AddListener(string evt, Action callback)

    {

        AddListener(evt, (Delegate)callback);

    }

    public void AddListener(string evt, Delegate callback)

    {

        Delegate listener;

        if (_listeners.TryGetValue(evt, out listener))

        {

            _listeners[evt] = Delegate.Combine(listener, callback);

        }

        else

        {

            _listeners[evt] = callback;

        }

    }



    public void RemoveListener<T1, T2, T3, T4>(string evt, Action<T1, T2, T3, T4> callback)

    {

        RemoveListener(evt, (Delegate)callback);

    }

    public void RemoveListener<T1, T2, T3>(string evt, Action<T1, T2, T3> callback)

    {

        RemoveListener(evt, (Delegate)callback);

    }

    public void RemoveListener<T1, T2>(string evt, Action<T1, T2> callback)

    {

        RemoveListener(evt, (Delegate)callback);

    }

    public void RemoveListener<T>(string evt, Action<T> callback)

    {

        RemoveListener(evt, (Delegate)callback);

    }

    public void RemoveListener(string evt, Action callback)

    {

        RemoveListener(evt, (Delegate)callback);

    }

    private void RemoveListener(string evt, Delegate callback)

    {

        Delegate listener;

        if (_listeners.TryGetValue(evt, out listener))

        {

            listener = Delegate.Remove(listener, callback);

            if (listener == null)

            {

                _listeners.Remove(evt);

            }

            else

            {

                _listeners[evt] = listener;

            }

        }

    }



    public void Dispatch<T1, T2, T3, T4>(string evt, T1 arg1, T2 arg2, T3 arg3, T4 arg4)

    {

        Delegate[] methods = GetMethods(evt);

        if (methods != null)

        {

            foreach (Delegate m in methods)

            {

                try

                {
                    if (m.Target != null)
                        ((Action<T1, T2, T3, T4>)m)(arg1, arg2, arg3, arg4);

                }

                catch (Exception e) { LogError(e); }

            }

        }

    }



    public void Dispatch<T1, T2, T3>(string evt, T1 arg1, T2 arg2, T3 arg3)

    {

        Delegate[] methods = GetMethods(evt);

        if (methods != null)

        {

            foreach (Delegate m in methods)

            {

                try

                {
                    if (m.Target != null)
                        ((Action<T1, T2, T3>)m)(arg1, arg2, arg3);

                }

                catch (Exception e) { LogError(e); }

            }

        }

    }



    public void Dispatch<T1, T2>(string evt, T1 arg1, T2 arg2)

    {

        Delegate[] methods = GetMethods(evt);

        if (methods != null)

        {

            foreach (Delegate m in methods)

            {

                try

                {
                    if (m.Target != null)
                        ((Action<T1, T2>)m)(arg1, arg2);

                }

                catch (Exception e) { LogError(e); }

            }

        }

    }



    public void Dispatch<T>(string evt, T arg)

    {

        Delegate[] methods = GetMethods(evt);

        if (methods != null)

        {

            foreach (Delegate m in methods)

            {

                try

                {

                    if (m.Target != null)
                    {
                        ((Action<T>)m)(arg);
                    }

                }

                catch (Exception e) { LogError(e); }

            }

        }

    }



    public void Dispatch(string evt)

    {

        Delegate[] methods = GetMethods(evt);

        if (methods != null)

        {

            foreach (Delegate m in methods)

            {

                try

                {
                    if (m.Target != null)
                        ((Action)m)();

                }

                catch (Exception e) { LogError(e); }

            }

        }

    }



    private Delegate[] GetMethods(string evt)

    {

        Delegate listener;

        if (_listeners.TryGetValue(evt, out listener))

        {

            return listener.GetInvocationList();

        }

        return null;

    }



    private static void LogError(Exception e)
    {

        UnityEngine.Debug.LogError(e);

    }

}

代码分析

订阅消息时

_listeners[evt] = Delegate.Combine(listener, callback);

Delegate.Combine将指定的多路广播(可组合)委托的调用列表连接起来,相当于事件的 += 方法

发送消息时

listener.GetInvocationList()

GetInvocationList得到链式委托

取消订阅时

listener = Delegate.Remove(listener, callback);

Delegate.Remove相当于 -= 方法

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值