Unity 应用消息中心-MessageCenter

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

1.MessageCenter主脚本

2.DelegateEvent消息类型脚本

3.MC_Default_Data具体接收类脚本

 

 

 

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}

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
}

 

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_;}
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/83514.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux 下 Mysql 的使用(Ubuntu20.04)

文章目录 一、安装二、使用2.1 登录2.2 数据库操作2.2.1 创建数据库2.2.2 删除数据库2.2.3 创建数据表 参考文档 一、安装 Linux 下 Mysql 的安装非常简单&#xff0c;一个命令即可&#xff1a; sudo apt install mysql-server检查安装是否成功&#xff0c;输入&#xff1a; …

CSS中如何改变鼠标指针样式(cursor)?

聚沙成塔每天进步一点点 ⭐ 专栏简介⭐ CSS中改变鼠标指针样式&#xff08;cursor&#xff09;⭐ 示例&#xff1a;⭐ 写在最后 ⭐ 专栏简介 前端入门之旅&#xff1a;探索Web开发的奇妙世界 记得点击上方或者右侧链接订阅本专栏哦 几何带你启航前端之旅 欢迎来到前端入门之旅…

微信小程序创建项目以及注意事项

1.申请账号并完善信息 2.下载安装开发工具 3.开发小程序 4.上传代码 5.提交审核 6.发布 创建项目 根据需求选择模板&#xff0c;也可以不选择模板 创建完毕之后 进入页面点击终端 然后新建终端 输入npm init 一直按回车即可 安装成功 出现package.json 如何使用组件&#x…

jvs-rules(规则引擎)更新:新增功能介绍

jvs-rules更新内容 1.复合变量新增数据补充节点&#xff0c;实现请求回来的数据再以入参方式请求其他数据进行数据补充&#xff08;例如通过参数A&#xff0c;请求回数据B&#xff0c;再以数据B为入参&#xff0c;请求回数据C&#xff09; 2.规则流结束节点支持新增、新建、引…

在腾讯云服务器OpenCLoudOS系统中安装svn(有图详解)

1. 安装svn yum -y install subversion 安装成功&#xff1a; 2. 创建数据根目录及仓库 mkdir -p /usr/local/svn/svnrepository 创建test仓库&#xff1a; svnadmin create /usr/local/svn/test test仓库创建成功&#xff1a; 3. 修改配置test仓库 cd /usr/local/svn/te…

文旅景区vr体验馆游乐场vr项目是什么

我们知道现在很多的景区或者游玩的地方&#xff0c;以及学校、科技馆、科普馆、商场或公园或街镇&#xff0c;都会建一些关于游玩以及科普学习的项目。从而增加学习氛围或者带动人流量等等。这样的形式&#xff0c;还是有很好的效果呈现。 普乐蛙VR体验馆案例 下面是普乐蛙做的…

AI夏令营第三期用户新增挑战赛学习笔记

1、数据可视化 1.数据探索和理解&#xff1a;数据可视化可以帮助我们更好地理解数据集的特征、分布和关系。通过可视化数据&#xff0c;我们可以发现数据中的模式、异常值、缺失值等信息&#xff0c;从而更好地了解数据的特点和结构。2.特征工程&#xff1a;数据可视化可以帮助…

Docker安装并配置Pushgateway

Linux下安装Docker请参考&#xff1a;Linux安装Docker 简介 Pushgateway是Prometheus的一个组件&#xff0c;prometheus server默认是通过Exporter主动获取数据&#xff08;默认采取pull拉取数据&#xff09;&#xff0c;Pushgateway则是通过exporter主动方式推送数据到Pushg…

Java【HTTP】什么是 Cookie 和 Session? 如何理解这两种机制的区别和作用?

文章目录 前言一、Cookie1, 什么是 Cookie2, Cookie 从哪里来3, Cookie 到哪里去4, Cookie 有什么用 二、Session1, 什么是 Session2, 理解 Session 三、Cookie 和 Session 的区别总结 前言 各位读者好, 我是小陈, 这是我的个人主页, 希望我的专栏能够帮助到你: &#x1f4d5; …

【pytorch】Unfold和Fold的互逆操作

1. 参数定义 Unfold https://pytorch.org/docs/stable/generated/torch.nn.Unfold.html#torch.nn.Unfold Fold https://pytorch.org/docs/stable/generated/torch.nn.Fold.html#torch.nn.Fold 注意&#xff1a;参数当中的padding是在四周边补零&#xff0c;而当fold后的尺寸…

[当前就业]2023年8月25日-计算机视觉就业现状分析

计算机视觉就业现状分析 前言&#xff1a;超越YOLO&#xff1a;计算机视觉市场蓬勃发展 如今&#xff0c;YOLO&#xff08;You Only Look Once&#xff09;新版本的发布周期很快&#xff0c;每次迭代的性能都优于其前身。每 3 到 4 个月就会推出一个升级版 YOLO 变体&#xf…

MR混合现实实训教学系统演示

MR混合现实实训教学系统的应用场景&#xff1a; 1、汽车检测与维修 使用MR混合现实技术&#xff0c;学生可以通过虚拟头戴式设备&#xff0c;将课堂延伸到实地。例如&#xff0c;在汽车维修课程中&#xff0c;学生可以通过MR技术&#xff0c;熟悉汽车模型内部关键结构&#x…