Unity 应用消息中心-MessageCenter

Ps:主要解决耦合问题,把脚本之间的联系通过不同消息类型事件形式进行贯通

1.MessageCenter主脚本

2.DelegateEvent消息类型脚本

3.MC_Default_Data具体接收类脚本

cs 复制代码
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 事件管理
/// </summary>
public class MessageCenter : Singleton<MessageCenter>
{
    // 消息委托
    public delegate void messageDelHandle(MessagDeta message);
    // 消息字典
    public Dictionary<MessageCenterEventType, messageDelHandle> messageMap = new Dictionary<MessageCenterEventType, messageDelHandle>();

    /// <summary>
    /// 注册监听
    /// </summary>
    public void RegisterListener(MessageCenterEventType messageType, messageDelHandle handle)
    {
        if (handle == null) return;

        // 把事件添加到对应的委托中
        messageDelHandle myHandle = null;
        messageMap.TryGetValue(messageType, out myHandle);

        if (myHandle != null)
        {
            Delegate[] HandleList = myHandle.GetInvocationList();

            foreach (messageDelHandle item in HandleList)
            {
                if (item == handle)
                {
                    Debug.LogError($"MessageCenter RegisterListener Type  ---  <color=yellow>{messageType.ToString()}</color>  ---  Has Registed Function  ---  <color=yellow>{handle.Method.Name}</color>  ---");
                    return;
                }
            }
        }

        messageMap[messageType] = (messageDelHandle)Delegate.Combine(myHandle, handle);
    }
    /// <summary>
    /// 移除监听
    /// </summary>
    public void RemoveListener(MessageCenterEventType messageType, messageDelHandle handle)
    {
        if (handle == null) return;
        messageMap[messageType] = (messageDelHandle)Delegate.Remove(messageMap[messageType], handle);
    }

    /// <summary>
    /// 清空消息
    /// </summary>
    public void Clear()
    {
        messageMap.Clear();
    }

    /// <summary>
    /// 发送消息
    /// </summary>
    /// <param name="messageName">消息类型 </param>
    /// <param name="body"> 发送消息主体 </param>
    public void SendMessage(MessageCenterEventType messageType, object body = null)
    {

        messageDelHandle handle;
        if (messageMap.TryGetValue(messageType, out handle))
        {
            MessagDeta evt = new MessagDeta(messageType, body);
            try
            {
                if (handle != null)
                {
                    handle(evt);
                }
            }
            catch (System.Exception e)
            {
                Debug.Log($"SendMessage:::{evt.Type.ToString()}:::{e.Message}:::{e.StackTrace}:::{e}");
            }
        }

    }

    #region 枚举类型接口

    #region MessageType
    //public void RegisterListener(MessageCenterEventType messageType, messageDelHandle handle)
    //{
    //    RegisterListener(messageType, handle);
    //}
    //public void RemoveListener(MessageCenterEventType messageType, messageDelHandle handle)
    //{
    //    RemoveListener(messageType, handle);
    //}
    //public void SendMessage(MessageCenterEventType messageType, object body = null)
    //{
    //    SendMessage(messageType, body);
    //}
    #endregion


    #region BattleEvent
    //public void RegisterListener(BattleEvent messageType, messageDelHandle handle)
    //{
    //    RegisterListener((int)messageType, handle);
    //}
    //public void RemoveListener(BattleEvent messageType, messageDelHandle handle)
    //{
    //    RemoveListener((int)messageType, handle);
    //}
    //public void SendMessage(BattleEvent messageType, object body = null)
    //{
    //    SendMessage((int)messageType, body);
    //}
    #endregion


    #region ProtocolEvent
    //public void RegisterListener(ProtocolEvent messageType, messageDelHandle handle)
    //{
    //    RegisterListener((int)messageType, handle);
    //}
    //public void RemoveListener(ProtocolEvent messageType, messageDelHandle handle)
    //{
    //    RemoveListener((int)messageType, handle);
    //}
    //public void SendMessage(ProtocolEvent messageType, object body = null)
    //{
    //    SendMessage((int)messageType, body);
    //}

    #endregion

    #endregion

}
cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
/// <summary>
/// 消息类
/// </summary>
public class MessagDeta
{
    public MessageCenterEventType Type  //发送的消息类型
    {
        get;
        private set;
    }
    public object Data  //消息主体
    {
        get;
        private set;
    }
    /// <summary>
    /// 方法
    /// </summary>
    public Action action;
    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="type">消息类型</param>
    /// <param name="body">消息体</param>
    public MessagDeta(MessageCenterEventType type, object data,Action action_=null)
    {
        Type = type;
        Data = data;
        if (action_ != null)
        {
            action = action_;
        }
    }
}
/// <summary>
/// 用户事件类型,必须按照类型,顺序添加,合理命名规范
/// </summary>
public enum MessageCenterEventType
{
    /// <summary>
    /// UI消息号开始
    /// </summary>
    UI_Start = 100000,

    UI_Fence_ChangeCameraView = 100101, //围栏UI界面切换主相机视角
    /// <summary>
    /// 实体消息号开始
    /// </summary>
    Entity_Start = 200000,
    //可以自己添加类型,每个类型预留100000接口

    Ground_Wire=300000,//主接线图接地线
    Ground_Wire_ding_wei = 300001,//主接线图接地线定位

 
    Ti_shi_tong_yong = 400001,//通用提示
    /// <summary>
    /// 其他杂类接口
    /// </summary>
    Other_Start = 900000
}
cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class MC_Int_Data
{
    public int _num;
}

public class MC_Uint_Data
{
    public uint _num;
}

public class MC_Long_Data
{
    public long _num;
}

public class MC_Ulong_Data
{
    public ulong _num;
}

public class MC_Shotr_Data
{
    public short _num;
}

public class MC_Ushotr_Data
{
    public ushort _num;
}

public class MC_Float_Data
{
    public float _num;
}

public class MC_Double_Data
{
    public double _num;
}

public class MC_String_Data
{
    public string _Str;
}

public class MC_Char_Data
{
    public char _char;
}

public class MC_Bool_Data
{
    public bool _flag;
}

public class MC_Object_Data
{
    public object _obj;
}
/// <summary>
/// 接地线控制
/// </summary>
public class Fan_Ground_Wire_Data
{
    public string id;//
    public bool active;//是否激活
    public Fan_Ground_Wire_Data()
    {

    }
    public Fan_Ground_Wire_Data(string ID,bool Active)
    {
        id = ID;
        active = Active;
    }
}
/// <summary>
/// 接地线定位
/// </summary>
public class Fan_Ground_Wire_go
{
    public string id;// 
    public Fan_Ground_Wire_go(string ID)
    {
        id = ID;
    }
}
/// <summary>
/// 提示通用
/// </summary>
public class Ti_shi_tong_yong_c
{
    public string content;// 
    public string text_icon;// 
    public Action action;
    public Ti_shi_tong_yong_c(string content_,string text_icon_, Action action_)
    {
        content = content_;
        text_icon = text_icon_;
        action = action_;
    }
}
相关推荐
叶帆7 天前
【YFIOs】用C#开发硬件之设备上云
开发语言·unity·c#
久数君7 天前
AI三维建模工具“造形家”:地理场景三维化的高效解决方案
unity·glb·ai算法·ai三维建模工具·地图框选·造形家·城市建筑模型
会思考的猴子8 天前
Unity VFX 属性 Postion 和 TargetPostion
unity
hai3152475438 天前
九章编程法 · 猜数字游戏 (GW-BASIC 重构版) *
人工智能·microsoft·游戏引擎·游戏程序
心前阳光8 天前
Unity资源导入之自动化资源导入
unity·自动化·游戏引擎
心前阳光8 天前
Unity之2021.3.45f2c1发布安卓程序遇到的问题
android·unity·游戏引擎
纪纯8 天前
PicoVR Unity Integration SDK 3.4 常用交互API
unity·游戏引擎·vr·pico
龙智DevSecOps解决方案8 天前
3A 游戏优化技术栈:如何打通引擎级分析工具与 DevOps 持续集成管线?
unity·性能优化·游戏开发·技术美术·perforce·unrealengine
葛兰岱尔8 天前
从 SolidWorks 到 Three.js,从 Inventor 到 Unity——制造业CAD模型“几何-语义一体化“转换,不再是天方夜谭!
开发语言·javascript·unity
鼎艺创新科技8 天前
三维电子沙盘中OSGB倾斜摄影数据的加载与渲染
游戏引擎·cocos2d