当前位置:网站首页>学习(三):事件的订阅与发布
学习(三):事件的订阅与发布
2022-08-02 03:33:00 【落水无痕】

Singleton:单例模板,在学习(二)中
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 事件的订阅与发布
/// </summary>
public class EventManager : Singleton<EventManager>
{
public delegate void event_handler(string event_name, object udata);
private Dictionary<string, event_handler> dic = new Dictionary<string, event_handler>();
/// <summary>
/// 注册/订阅
/// </summary>
/// <param name="event_name">事件名</param>
/// <param name="h">函数</param>
public void AddListener(string event_name, event_handler h) {
if (this.dic.ContainsKey(event_name)) {
this.dic[event_name] += h;
}
else {
this.dic.Add(event_name, h);
}
}
/// <summary>
/// 删除/取消
/// </summary>
/// <param name="event_name">事件名</param>
/// <param name="h">函数</param>
public void RemoveListener(string event_name, event_handler h) {
if (!this.dic.ContainsKey(event_name)) {
return;
}
this.dic[event_name] -= h;
if (this.dic[event_name] == null) {
this.dic.Remove(event_name);
}
}
/// <summary>
/// 发送事件
/// </summary>
/// <param name="event_name">事件名</param>
/// <param name="udata">参数</param>
public void DispatchEvent(string event_name, object udata) {
if (!this.dic.ContainsKey(event_name)) {
return;
}
this.dic[event_name](event_name, udata);
}
}
边栏推荐
猜你喜欢
随机推荐
同时求最大值与最小值(看似简单却值得思考~)
剑指Offer 36.二叉搜索树与双向链表 中序遍历
install 命令
【LeetCode】Merge
剑指Offer 32.Ⅲ从上到下打印二叉树
MAC安装Mysql超详细完整教程
发布全新的配置格式 - AT
与TI的lvds芯片兼容-GM8284DD,GM8285C,GM8913,GM8914,GM8905C,GM8906C,国腾振芯LVDS类芯片,
【Connect the heart rate sensor to Arduino to read the heart rate data】
【LeetCode】链表相加 进位
Beckhoff ET2000 listener use
学习(四):显示FPS,和自定义显示调试
剑指Offer 32.Ⅱ从上到下打印二叉树
CCF刷题之旅--第一题
【plang1.4.3】编写水母动画脚本
STM32F4 CAN 配置注意的细节问题
vector的使用和模拟实现:
【plang 1.4.6】Plang高级编程语言(发布)
TC358860XBG BGA65 东芝桥接芯片 HDMI转MIPI
IDEA2021.2安装与配置(持续更新)









