当前位置:网站首页>Simple message mechanism of unity
Simple message mechanism of unity
2022-07-26 09:02: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);
}
}
}
}
}
}
}
边栏推荐
- 公告 | FISCO BCOS v3.0-rc4发布,新增Max版,可支撑海量交易上链
- Clean the label folder
- Recurrence of SQL injection vulnerability in the foreground of a 60 terminal security management system
- 巴比特 | 元宇宙每日必读:元宇宙的未来是属于大型科技公司,还是属于分散的Web3世界?...
- Cadence (x) wiring skills and precautions
- What are the differences in the performance of different usages such as count (*), count (primary key ID), count (field) and count (1)? That's more efficient
- 堆外内存的使用
- Replication of SQL injection vulnerability in the foreground of Pan micro e-cology8
- redis原理和使用-安装和分布式配置
- Database operation topic 1
猜你喜欢
CSDN TOP1“一个处女座的程序猿“如何通过写作成为百万粉丝博主?
Cadence (x) wiring skills and precautions
Review notes of Microcomputer Principles -- zoufengxing
垂直搜索
Okaleido上线聚变Mining模式,OKA通证当下产出的唯一方式
day06 作业---技能题7
对标注文件夹进行清洗
Okaleido launched the fusion mining mode, which is the only way for Oka to verify the current output
Elastic APM安装和使用
C Entry series (31) -- operator overloading
随机推荐
Pan micro e-cology8 foreground SQL injection POC
正则表达式:判断是否符合USD格式
2000年的教训。web3是否=第三次工业革命?
TypeScript版Snowflake主键生成器
海内外媒体宣发自媒体发稿要严格把握内容关
数据库操作 题目一
Datawhale panda book has been published!
Self review ideas of probability theory
力扣刷题,三数之和
【final关键字的使用】
Clean the label folder
SQL入门——组合表
Set of pl/sql -2
mysql函数
Uploading pictures on Alibaba cloud OSS
My meeting of OA project (query)
CIS 2020 - alternative skills against cloud WAF (pyn3rd)
Study notes of automatic control principle --- stability analysis of control system
P3743 Kotori's equipment
Sklearn machine learning foundation (linear regression, under fitting, over fitting, ridge regression, model loading and saving)