当前位置:网站首页>学习(三):事件的订阅与发布
学习(三):事件的订阅与发布
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);
}
}
边栏推荐
猜你喜欢
随机推荐
Chrome 里的小恐龙游戏是怎么做出来的?
Industry where edge gateway strong?
Application of electronic flow on business trip
谷粒商城10——搜索、商品详情、异步编排
Comparative analysis of OneNET Studio and IoT Studio
2019 - ICCV - 图像修复 Image Inpainting 论文导读《StructureFlow: Image Inpainting via Structure-aware ~~》
剑指Offer 32.Ⅲ从上到下打印二叉树
install 命令
Altium Designer Basics
进程(中):进程状态、进程地址空间
【详解】线程池及其自定义线程池的实现
实现动态库(DLL)之间内存统一管理
Lightly 支持 Markdown 文件在线编写(文中提供详细 Markdown 语法)
list:list的介绍和模拟实现
使用飞凌嵌入式IMX6UL-C1板子——qt+opencv环境搭建
振芯科技GM8285C:功能TTL转LVDS芯片简介
【LeetCode】Add the linked list with carry
剑指Offer 33.二叉搜索树的后序遍历序列
Process (present) : custom shell command line interpreter
GM8284DD,GM8285C,GM8913,GM8914,GM8905C,GM8906C,国腾振芯LVDS类芯片








