Unity可以传任意参数的事件中心工具类

使用方法

public class Test : MonoBehaviour
{
    void Start()
    {
        MsgManager.AddMsgDelegate(123, MsgDelegate);
        MsgManager.PostMsg(123, MsgParam.newStrings("哈哈哈", "你好啊").Floats(1.2f,55.666666f).Ints(123654,6549).Bools(false,true));
    }

    void OnDestroy()
    {
        MsgManager.RemoveMsgDelegate(123, MsgDelegate);
    }
    void MsgDelegate(MsgParam param)
    {
        if (param.StringCount <= 0) return;
        if (param.IntCount <= 0) return;
        if (param.FloatCount <= 0) return;
        if (param.BoolCount <= 0) return;
        Debug.Log(string.Format("收到String参数1:{0}  参数2::{1}",param.StringVal[0],param.StringVal[1]));
        Debug.Log(string.Format("收到Int参数1:{0}  参数2::{1}",param.IntVal[0],param.IntVal[1]));
        Debug.Log(string.Format("收到Float参数1:{0}  参数2::{1}", param.FloatVal[0],param.FloatVal[1]));
        Debug.Log(string.Format("收到Bool参数1:{0}  参数2::{1}", param.BoolVal[0],param.BoolVal[1]));
    }
}

测试效果:

 

下面是源码

MsgParam类

using System.Collections.Generic;
/// <summary>
/// 消息参数类,为了减少内存垃圾请优先使用newInts、newUInts、newFloats、newObjects、newStrings、newBools等函数
/// </summary>
public class MsgParam
{
    public static MsgParam Empty = new MsgParam();
    private List<int> m_ints = new List<int>();
    private List<float> m_floats = new List<float>();
    private List<string> m_strings = new List<string>();
    private List<bool> m_bools = new List<bool>();
    private List<object> m_objects = new List<object>();
    private List<uint> m_uints = new List<uint>();

    public List<int> IntVal
    {
        get { return m_ints; }
    }

    public List<float> FloatVal
    {
        get { return m_floats; }
    }

    public List<string> StringVal
    {
        get { return m_strings; }
    }

    public List<bool> BoolVal
    {
        get { return m_bools; }
    }

    public List<object> ObjectVal
    {
        get { return m_objects; }
    }

    public List<uint> UIntVal
    {
        get { return m_uints; }
    }

    public int IntCount
    {
        get { return m_ints == null ? 0 : m_ints.Count; }
    }

    public int FloatCount
    {
        get { return m_floats == null ? 0 : m_floats.Count; }
    }

    public int StringCount
    {
        get { return m_strings == null ? 0 : m_strings.Count; }
    }

    public int BoolCount
    {
        get { return m_bools == null ? 0 : m_bools.Count; }
    }

    public int ObjCount
    {
        get { return m_objects == null ? 0 : m_objects.Count; }
    }

    public int UIntCount
    {
        get { return m_uints == null ? 0 : m_uints.Count; }
    }

    public MsgParam()
    {

    } 
    public MsgParam Ints(params int[] _ints)
    {
        m_ints.Clear();
        for (int i = 0; i < _ints.Length; i++)
        {
            m_ints.Add(_ints[i]);
        }
        return this;
    }

    public static MsgParam newInts(params int[] _ints)
    {
        return Empty.Ints(_ints);
    }

    public MsgParam Floats(params float[] _floats)
    {
        m_floats.Clear();
        for (int i = 0; i < _floats.Length; i++)
        {
            m_floats.Add(_floats[i]);
        }
        return this;
    }
    public static MsgParam newFloats(params float[] _floats)
    {
        return Empty.Floats(_floats);
    }

    public MsgParam Strings(params string[] _strings)
    {
        m_strings.Clear();
        for (int i = 0; i < _strings.Length; i++)
        {
            m_strings.Add(_strings[i]);
        }
        return this;
    }
    public static MsgParam newStrings(params string[] _strings)
    {
        return Empty.Strings(_strings);
    }

    public MsgParam Bools(params bool[] _bools)
    {
        m_bools.Clear();
        for (int i = 0; i < _bools.Length; i++)
        {
            m_bools.Add(_bools[i]);
        }
        return this;
    }

    public static MsgParam newBools(params bool[] _bools)
    {
        return Empty.Bools(_bools);
    }


    public MsgParam Objs(params object[] _objs)
    {
        m_objects.Clear();
        for (int i = 0; i < _objs.Length; i++)
        {
            m_objects.Add(_objs[i]);
        }
        return this;
    }

    public static MsgParam newObjs(params object[] _objs)
    {
        return Empty.Objs(_objs);
    }
    public MsgParam UInts(params uint[] _uints)
    {
        m_uints.Clear();
        for (int i = 0; i < _uints.Length; i++)
        {
            m_uints.Add(_uints[i]);
        }
        return this;
    }

    public static MsgParam newUInts(params uint[] _uints)
    {
        return Empty.UInts(_uints);
    }
}

MsgManager类

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

public class MsgManager : MonoBehaviour
{
    public delegate void MsgDelegate(MsgParam _mp);

    private static Dictionary<int, List<MsgDelegate>> s_msgDelegates = new Dictionary<int, List<MsgDelegate>>();
    struct MsgEntity
    {
        public MsgParam mp;
        public int msg;
        public MsgEntity(int _msg, MsgParam _mp)
        {
            msg = _msg;
            mp = _mp;
        }
    }
    private static List<MsgEntity> s_msgQueue = new List<MsgEntity>();
    public static bool AddMsgDelegate(int _msg, MsgDelegate _del)
    {
        List<MsgDelegate> delegates = null;
        if (s_msgDelegates.ContainsKey(_msg))
            delegates = s_msgDelegates[_msg];
        else
            delegates = s_msgDelegates[_msg] = new List<MsgDelegate>();

        if (delegates.Contains(_del))
            return false;

        delegates.Add(_del);

        return true;
    }
    public static bool RemoveMsgDelegate(int _msg, MsgDelegate _del)
    {
        if (!s_msgDelegates.ContainsKey(_msg))
            return false;

        List<MsgDelegate> delegates = s_msgDelegates[_msg];
        if (!delegates.Contains(_del))
            return false;

        delegates.Remove(_del);
        if (delegates.Count == 0)
            s_msgDelegates.Remove(_msg);

        return true;
    }
    bool SendMsg(int _msg, MsgParam _mp)
    {
        if (!s_msgDelegates.ContainsKey(_msg))
            return false;

        List<MsgDelegate> delegates = s_msgDelegates[_msg];
        for (int nIndex = 0; nIndex < delegates.Count; nIndex++)
        {
            if (delegates[nIndex] != null)
            {
                try
                {
                    delegates[nIndex](_mp);
                }
                catch (Exception e)
                {
                    Debug.Log("MsgManager::SendMsg() delegate function run error : " + e.ToString());
                }
            }

        }

        return true;
    }
    public static bool PostMsg(int _msg)
    {
        return PostMsg(_msg, MsgParam.Empty);
    }
    public static bool PostMsg(int _msg, MsgParam _mp)
    {
        s_msgQueue.Add(new MsgEntity(_msg, _mp));

        return true;
    }
    void LateUpdate()
    {
        try
        {
            while (s_msgQueue.Count > 0)
            {
                SendMsg(s_msgQueue[0].msg, s_msgQueue[0].mp);
                s_msgQueue.RemoveAt(0);
            }
        }
        catch (Exception e)
        {
            Debug.Log("CEMsgManager::LateUpdate Exception: " + e.ToString());
        }
    }
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值