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.mzph.cn/news/53209.shtml

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

相关文章

macbook m1 docker中使用go

已经有一个centos8的镜像&#xff0c;本来打算在centos8中安装go 安装方法&#xff1a; # 1.下载go的安装包 mkdir install && cd install # 任意创建个文件夹 wget https://go.dev/dl/go1.20.2.linux-amd64.tar.gz# 2. 解压 tar -C xzf go1.20.2.linux-amd64.tar.g…

ThreadPoolExecutor的参数keepAliveTime的作用

直接断点进去&#xff1a; Test public void testKeepAliveTime3() {//生存时间 - 针对救急线程ThreadPoolExecutor executor new ThreadPoolExecutor(1, 2, 10, TimeUnit.SECONDS, new SynchronousQueue<>()); }public ThreadPoolExecutor(int corePoolSize,int maxim…

layui表格事件分析实例

在 layui 的表格组件中&#xff0c;区分表头事件和行内事件是通过事件类型&#xff08;toolbar 和 tool&#xff09;以及 lay-filter 值来实现的。 我们有一个表格&#xff0c;其中有一个工具栏按钮和操作按钮。我们将使用 layui 的 table 组件来处理这些事件。 HTML 结构&…

vue2+element-ui 实现下拉框滚动加载

一、自定义滚动指令。 VUE.directive( el-select-loadmore: { bind(el, binding) { const SELECTWRAP_DOM el.querySelector(.el-select-dropdown .el-select-dropdown__wrap) SELECTWRAP_DOM.addEventListener(scroll, function () { /*…

算法训练营第三十六天(8.26)| 动态规划Part07:完全背包

Leecode 139.单词拆分 题目地址&#xff1a;力扣&#xff08;LeetCode&#xff09;官网 - 全球极客挚爱的技术成长平台 题目类型&#xff1a;完全背包 class Solution { public:bool wordBreak(string s, vector<string>& wordDict) {// 完全背包问题// dp[i]代表从0…

汇总区间java解决方案

问题&#xff1a; 给定一个 无重复元素 的 有序 整数数组 nums 。 返回 恰好覆盖数组中所有数字 的 最小有序 区间范围列表 。也就是说&#xff0c;nums 的每个元素都恰好被某个区间范围所覆盖&#xff0c;并且不存在属于某个范围但不属于 nums 的数字 x 。 列表中的每个区…

unity 模型显示在UI上 并交互(点击、旋转、缩放)

项目工程&#xff1a;unity模型显示在UI上并交互&#xff08;点击、旋转、缩放&#xff09;资源-CSDN文库 1.在Assets创建 Render Texture&#xff08;下面会用到&#xff09;&#xff0c;根据需要设置Size 2.创建UIRawImage&#xff0c;并把Render Texture赋上 3.创建相机&am…

深度解读Promise.prototype.finally

由一个问题引发的血案&#xff1a; 手写源码实现Promise.prototype.finally。 我们知道&#xff0c;对于promise来讲&#xff0c;当状态敲定&#xff0c;无论状态兑现或拒绝时都需要调用的函数&#xff0c;可以使用Promise.prototype.finally的回调来实现。那么如何手写实现Pro…

在vscode(idea)使用GitHub账号、Copilot异常

在idea使用GitHub账号、Copilot异常 登录GitHub显示 Invalid authentication data.Connection refused: connect或者副驾驶显示 Failed to initiate the GitHub login process. Please try again.一般网上的方法推荐使用token登录&#xff0c;或者降级副驾驶 经过研究&#x…

Docker安装并配置Pushgateway

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

前端需要理解的CSS知识

CSS&#xff08;层叠样式表&#xff0c;Cascading Style Sheets&#xff09;不是编程语言&#xff0c;而是用来描述 HTML 或 XML&#xff08;包括如 SVG、MathML 或 XHTML 之类的 XML 分支语言&#xff09;文档的表现与展示效果的样式表语言。CSS3是CSS的最新标准&#xff0c;是…

mysql基础——认识索引

一、介绍 “索引”是为了能够更快地查询数据。比如一本书的目录&#xff0c;就是这本书的内容的索引&#xff0c;读者可以通过在目录中快速查找自己想要的内容&#xff0c;然后根据页码去找到具体的章节。 二、优缺点 优势&#xff1a;以快速检索&#xff0c;减少I/O次数&am…

【Go语言】基于Socket编程的P2P通信程序示例

Go语言的Socket编程实现为开发者提供了一种高效且强大的方式来实现网络通信。通过Go语言的并发模型和内置的网络库&#xff0c;如net包&#xff0c;开发者可以轻松地创建基于套接字的通信应用。Go语言的goroutine和channel机制使并发处理变得简单&#xff0c;能够轻松处理多个连…

U盘怎么加密?U盘加密方法有哪些?

U盘是我们生活和工作中最常用的移动储存设备&#xff0c;经常被用来存放各种重要数据&#xff0c;为了保证数据的安全&#xff0c;我们需要加密U盘。那么&#xff0c;U盘加密方法有哪些呢&#xff1f; U盘加密普通方法 如果你的U盘储存数据不多&#xff0c;并且对于加密的要求…

C. Another Problem on Strings

Problem - C - Codeforces 问题描述&#xff1a;给定一个k和一个01串&#xff0c;求该01串中有多少个’1’个数为k的字串&#xff0c;出现位置不同算不同的子串。 思路&#xff1a;暴力会挂&#xff0c;考虑优化。对于一个字串他满足’1’为k个条件时&#xff0c;它的前面第一…

Linux 系统下 GDB 调试器的使用

文章目录 简介GDB 的介绍GDB 的使用 GDB 常用命令及示例查看相关操作断点相关操作运行相关操作变量相关操作分隔窗口操作 简介 GDB 的介绍 GDB 是 GNU 调试程序&#xff0c;是用来调试 C 和 C 程序的调试器。它可以让程序开发者在程序运行时观察程序的内部结构和内存的使用情况…

初阶c语言:趣味扫雷游戏

目录 前言 制作菜单 构建游戏选择框架 实现游戏功能 模块化编程&#xff1a;查看前节三子棋的内容 初始化雷区 ​编辑 优化棋盘 随机埋入地雷 点击后的决策 实现此功能代码 game&#xff08;&#xff09;&#xff1b;的安排 前言 《扫雷》是一款大众类的益智小游戏&…

java八股文面试[JVM]——元空间

JAVA8为什么要增加元空间 为什么要移除永久代&#xff1f; 知识来源&#xff1a; 【2023年面试】JVM8为什么要增加元空间_哔哩哔哩_bilibili

Django 简易PACS读片系统

1、Django中写一个后端接口&#xff0c;给HTML提供dicom文件接口的方式 1、首先创建django项目 1、下载安装跨域的包 pip3 install django-cors-headers2、使用pycharm创建一个Django项目 3、点击创建在另一个窗口&#xff0c;这个都无所谓&#xff0c;怎么都行&#xff0c;…

Hadoop Yarn 配置多队列的容量调度器

文章目录 配置多队列的容量调度器多队列查看 配置多队列的容量调度器 首先&#xff0c;我们进入 Hadoop 的配置文件目录中&#xff08;$HADOOP_HOME/etc/hadoop&#xff09;&#xff1b; 然后通过编辑容量调度器配置文件 capacity-scheduler.xml 来配置多队列的形式。 默认只…