当前位置:网站首页>unity简易消息机制
unity简易消息机制
2022-07-26 08:52:00 【m_girlAndGirl】
using System;
using System.Collections.Generic;
namespace WX
{
public class EasyMsg
{
static Dictionary<UnityEngine.Object, Dictionary<string, Action<object>>> mRegisteredObjects = new Dictionary<UnityEngine.Object, Dictionary<string, Action<object>>>();
public static void AddEvent(UnityEngine.Object obj, string msgName, Action<object> onMsgReceived, int limitCount = -1)
{
if (!mRegisteredObjects.ContainsKey(obj))
{
Dictionary<string, Action<object>> dict = new Dictionary<string, Action<object>>();
dict.Add(msgName, _ => {
});
dict[msgName] += onMsgReceived;
mRegisteredObjects.Add(obj, dict);
}
else
{
if (mRegisteredObjects[obj].ContainsKey(msgName))
{
mRegisteredObjects[obj][msgName] += onMsgReceived;
}
else
{
mRegisteredObjects[obj].Add(msgName, _ => {
});
mRegisteredObjects[obj][msgName] += onMsgReceived;
}
}
}
public static void RemoveEvent(UnityEngine.Object obj, string eventkey = "", Action<object> onMsgReceived = null)
{
if (onMsgReceived != null)
{
if (mRegisteredObjects.ContainsKey(obj))
{
if (mRegisteredObjects[obj].ContainsKey(eventkey.ToString()))
{
mRegisteredObjects[obj][eventkey.ToString()] -= onMsgReceived;
}
}
}
else if (eventkey != "")
{
if (mRegisteredObjects.ContainsKey(obj))
{
if (mRegisteredObjects[obj].ContainsKey(eventkey.ToString()))
{
mRegisteredObjects[obj].Remove(eventkey.ToString());
}
}
}
else
{
if (mRegisteredObjects.ContainsKey(obj))
{
mRegisteredObjects[obj].Remove(eventkey.ToString());
}
}
}
public static void Execute(string msgName, object data = null)
{
foreach (var item in mRegisteredObjects)
{
foreach (var obj in item.Value)
{
if (obj.Key == msgName)
{
if (obj.Value != null)
{
obj.Value(data);
}
}
}
}
}
}
}
边栏推荐
- [untitled]
- P3743 Kotori's equipment
- day06 作业--技能题1
- PHP page value transfer
- OA项目之我的会议(查询)
- 【LeetCode数据库1050】合作过至少三次的演员和导演(简单题)
- sklearn 机器学习基础(线性回归、欠拟合、过拟合、岭回归、模型加载保存)
- Implementation of Prometheus web authentication and alarm
- Study notes of automatic control principle -- dynamic model of feedback control system
- What are the contents of Oracle OCP and MySQL OCP certification exams?
猜你喜欢
[database] gbase 8A MPP cluster v95 installation and uninstall
day06 作业--技能题2
JDBC数据库连接池(Druid技术)
Pop up window in Win 11 opens with a new tab ---firefox
Super potential public chain dfinity -- the best time for DFI developers to enter
keepalived双机热备
Study notes of automatic control principle --- stability analysis of control system
机器学习中的概率模型
海内外媒体宣发自媒体发稿要严格把握内容关
【数据库 】GBase 8a MPP Cluster V95 安装和卸载
随机推荐
03 exception handling, state keeping, request hook -- 04 large project structure and blueprint
zsh: command not found: nvm
at、crontab
Sklearn machine learning foundation (linear regression, under fitting, over fitting, ridge regression, model loading and saving)
03异常处理,状态保持,请求钩子---04大型项目结构与蓝图
【LeetCode数据库1050】合作过至少三次的演员和导演(简单题)
Arbitrum Nova release! Create a low-cost and high-speed dedicated chain in the game social field
机器学习中的概率模型
【数据库 】GBase 8a MPP Cluster V95 安装和卸载
Registration of finite element learning knowledge points
数据库操作 题目一
Arbitrum launched the anytrust chain to meet the diverse needs of ecological projects
P3743 kotori的设备
Overview of motion recognition evaluation
JDBC数据库连接池(Druid技术)
Web3 Games: current situation and future
Replication of SQL injection vulnerability in the foreground of Pan micro e-cology8
js闭包:函数和其词法环境的绑定
The lessons of 2000. Web3 = the third industrial revolution?
[encryption weekly] has the encryption market recovered? The cold winter still hasn't thawed out. Take stock of the major events that occurred in the encryption market last week