Unity 无需进行装箱/拆箱的事件管理器

装箱/拆箱

概念
   由于C#中所有的数据类型都是由基类System.Object继承而来的,所以值类型和引用类型的值可以通过显式

(或隐式)操作相互转换,而这转换过程也就是装箱(boxing)和拆箱(unboxing)过程。

 
“装箱/拆箱”代码:

int a = 1;

object b = a;//这里是装箱

int c = (int)b;//这里是拆箱

装箱时,生成的是全新的引用对象,这会有时间损耗,也就是造成效率降低。

下面是一般事件管理器用到的,要用到object转其他类型,会产生性能消耗

    public void Init()
    {
        EventMgr.GetIns().RegisterEvent("testEvent", testdelegate);
    }

    public void testdelegate(params object[] args)
    {
        int a = (int)args[0];
        string b = (string)args[1];
        bool c = (bool)args[2];
    }

如何优化:

我们可以通过泛型去设置参数的类型,而不需要通过 object 类型传递参数,如下代码

    public void Init()
    {
        EventMgr.GetIns().RegisterEvent<int, string, bool>("testEvent", testdelegate, this);
    }

    public void testdelegate(int a, string b, bool c)
    {
        Debug.LogError(a);
        Debug.LogError(b);
        Debug.LogError(c);
    }

下面放出完整代码

我这里加了一个Disposable类,只是写个简单后面需要扩展

我这里的想法是能不继承MonoBehaviour尽量不继承(除了主循环要加),不过看情况

游戏里面的ui,管理器都,战斗里面是实体,继承这个Disposable,然后之间的通信用事件管理器

有空就放出一个不继承MonoBehaviour的ui框架

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

public class Disposable
{
    private string Uid;

    public Disposable()
    {
        Uid = SysUtil.GetUid();
    }

    public string GetUid()
    {
        return Uid;
    }
}
using System;
using System.Collections.Generic;

public class EventData
{
    /// <summary>
    /// 唯一id
    /// </summary>
    public string Uid;

    /// <summary>
    /// 回调
    /// </summary>
    public Delegate mDelegate;

    public EventData(string uid, Delegate d)
    {
        Uid = uid;
        mDelegate = d;
    }
}

/// <summary>
/// 事件管理器
/// </summary>
public class EventMgr
{
    /// <summary>
    /// 事件列表字典
    /// </summary>
    private Dictionary<string, List<EventData>> eventDic = new Dictionary<string, List<EventData>>();

    /// <summary>
    /// 已注册字典
    /// </summary>
    private Dictionary<string, bool> registerDic = new Dictionary<string, bool>();

    /// <summary>
    /// 单例
    /// </summary>
    private static EventMgr instance;

    public static EventMgr GetIns()
    {
        if(null == instance)
        {
            instance = new EventMgr();
        }
        return instance;
    }

    #region 注册事件
    public void RegisterEvent<T1, T2, T3, T4>(string evt, Action<T1, T2, T3, T4> callback, Disposable mDisposable)
    {
        RegisterEvent(evt, mDisposable.GetUid(), (Delegate)callback);
    }
    public void RegisterEvent<T1, T2, T3>(string evt, Action<T1, T2, T3> callback, Disposable mDisposable)
    {
        RegisterEvent(evt, mDisposable.GetUid(), (Delegate)callback);
    }
    public void RegisterEvent<T1, T2>(string evt, Action<T1, T2> callback, Disposable mDisposable)
    {
        RegisterEvent(evt, mDisposable.GetUid(), (Delegate)callback);
    }
    public void RegisterEvent<T>(string evt, Action<T> callback, Disposable mDisposable)
    {
        RegisterEvent(evt, mDisposable.GetUid(), (Delegate)callback);
    }
    public void RegisterEvent(string evt, Action callback, Disposable mDisposable)
    {
        RegisterEvent(evt, mDisposable.GetUid(), (Delegate)callback);
    }
    public void RegisterEvent(string evt, string uid, Delegate callback)
    {
        string tempEventName = evt + uid;
        if(registerDic.ContainsKey(tempEventName))
        {
            return;
        }
        registerDic[tempEventName] = true;
        if(!eventDic.ContainsKey(evt))
        {
            eventDic[evt] = new List<EventData>();
        }
        eventDic[evt].Add(new EventData(uid, callback));

    }
    #endregion

    #region 移除事件
    private void RemoveEvent(string evt, Disposable mDisposable)
    {
        RemoveEvent(evt, mDisposable.GetUid());
    }

    private void RemoveEvent(string evt,string uid)
    {
        string tempEventName = evt + uid;
        if (registerDic.ContainsKey(tempEventName))
        {
            registerDic.Remove(tempEventName);
            if (eventDic.ContainsKey(evt))
            {
                List<EventData> tempList = eventDic[evt];
                if(null != tempList)
                {
                    for (int i =tempList.Count;i-->0;)
                    {
                        if(tempList[i].Uid == uid)
                        {
                            tempList.RemoveAt(i);
                        }
                    }
                }
            }
        }
    }
    #endregion

    #region 派发事件
    public void Dispatch<T1, T2, T3, T4>(string evt, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
    {
        List<EventData> eventList = GetEventList(evt);
        if (eventList != null)
        {
            foreach (EventData m in eventList)
            {
                try
                {
                    ((Action<T1, T2, T3, T4>)m.mDelegate)(arg1, arg2, arg3, arg4);
                }
                catch (Exception e) { LogError(e); }
            }
        }
    }

    public void Dispatch<T1, T2, T3>(string evt, T1 arg1, T2 arg2, T3 arg3)
    {
        List<EventData> eventList = GetEventList(evt);
        if (eventList != null)
        {
            foreach (EventData m in eventList)
            {
                try
                {
                    ((Action<T1, T2, T3>)m.mDelegate)(arg1, arg2, arg3);
                }
                catch (Exception e) { LogError(e); }
            }
        }
    }

    public void Dispatch<T1, T2>(string evt, T1 arg1, T2 arg2)
    {
        List<EventData> eventList = GetEventList(evt);
        if (eventList != null)
        {
            foreach (EventData m in eventList)
            {
                try
                {
                    ((Action<T1, T2>)m.mDelegate)(arg1, arg2);
                }
                catch (Exception e) { LogError(e); }
            }
        }
    }

    public void Dispatch<T>(string evt, T arg)
    {
        List<EventData> eventList = GetEventList(evt);
        if (eventList != null)
        {
            foreach (EventData m in eventList)
            {
                try
                {
                    ((Action<T>)m.mDelegate)(arg);
                }
                catch (Exception e) { LogError(e); }
            }
        }
    }

    public void Dispatch(string evt)
    {
        List<EventData> eventList = GetEventList(evt);
        if (eventList != null)
        {
            foreach (EventData m in eventList)
            {
                try
                {
                    ((Action)m.mDelegate)();
                }
                catch (Exception e) { LogError(e); }
            }
        }
    }
    #endregion

    /// <summary>
    /// 获取所有事件
    /// </summary>
    /// <param name="evt"></param>
    /// <returns></returns>
    private List<EventData> GetEventList(string evt)
    {
        if (eventDic.ContainsKey(evt))
        {
            List<EventData> tempList = eventDic[evt];
            if (null != tempList)
            {
                return tempList;
            }
        }
        return null;
    }

    private static void LogError(Exception e)
    {
        UnityEngine.Debug.LogError(e);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SysUtil
{
    private static long Uid = long.MaxValue;

    public static string GetUid()
    {
        Uid--;
        return Uid.ToString();
    }
}

工程下载地址

链接:https://pan.baidu.com/s/1Axz-_6jkNXplFgv1zrjECw 
提取码:6fpa 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值