Unity 简易监听框架
全局维护一个字典,字典中的key为字符串或者自定义类型,value为委托,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System; public delegate void CallBack();
public delegate void CallBack<T>(T t);
public delegate void CallBack<T, D>(T t, D d);
public delegate void CallBack<T, D, U>(T t, D d, U u); public class Messenger
{
public static Dictionary<string, Delegate> eventTable = new Dictionary<string, Delegate>(); static void OnListenerAdding(string eventkey, Delegate listenerDelegate)
{
if (!eventTable.ContainsKey(eventkey))
{
eventTable.Add(eventkey, null);
}
}
public static void AddListener(string eventtype, CallBack handler)
{
OnListenerAdding(eventtype, handler);
eventTable[eventtype] = (eventTable[eventtype] as CallBack) + (handler);
} public static void AddListener<T>(string eventtype, CallBack<T> handler)
{
OnListenerAdding(eventtype, handler);
eventTable[eventtype] = (eventTable[eventtype] as CallBack<T>) + (handler);
}
public static void AddListener<T, D>(string eventtype, CallBack<T, D> handler)
{
OnListenerAdding(eventtype, handler);
eventTable[eventtype] = (eventTable[eventtype] as CallBack<T, D>) + (handler);
}
public static void AddListener<T, D, U>(string eventtype, CallBack<T, D, U> handler)
{
OnListenerAdding(eventtype, handler);
eventTable[eventtype] = (eventTable[eventtype] as CallBack<T, D, U>) + (handler);
}
static void OnBroadcast(string eventtype)
{
if (!eventTable.ContainsKey(eventtype))
{
Debug.LogError("不包含此监听");
return;
}
} public static void Broadcast(string eventtype)
{
OnBroadcast(eventtype);
CallBack callback;
if (eventTable.ContainsKey(eventtype))
{
callback = eventTable[eventtype] as CallBack;
callback?.Invoke();//如果不为空调用,unity2017以下不可简写
}
}
public static void Broadcast<T>(string eventtype,T t)
{
OnBroadcast(eventtype);
CallBack<T> callback;
if (eventTable.ContainsKey(eventtype))
{
callback = eventTable[eventtype] as CallBack<T>;
callback?.Invoke(t);//如果不为空调用,unity2017以下不可简写
}
}
public static void Broadcast<T,D>(string eventtype, T t,D d)
{
OnBroadcast(eventtype);
CallBack<T,D> callback;
if (eventTable.ContainsKey(eventtype))
{
callback = eventTable[eventtype] as CallBack<T,D>;
callback?.Invoke(t, d);//如果不为空调用,unity2017以下不可简写
}
}
public static void Broadcast<T, D,U>(string eventtype, T t, D d,U u)
{
CallBack<T, D,U> callback;
if (eventTable.ContainsKey(eventtype))
{
callback = eventTable[eventtype] as CallBack<T, D,U>;
callback?.Invoke(t, d, u); //如果不为空调用,unity2017以下不可简写
}
}
}
Unity 简易监听框架的更多相关文章
- 深入理解Spring的容器内事件发布监听机制
目录 1. 什么是事件监听机制 2. JDK中对事件监听机制的支持 2.1 基于JDK实现对任务执行结果的监听 3.Spring容器对事件监听机制的支持 3.1 基于Spring实现对任务执行结果的监 ...
- 【转】监听按钮除OnClick外其他事件的方法,附简易改编的UIButton类
http://lib.csdn.net/article/unity3d/38463 作者:IceFantasyLcj 大家好,我是雨中祈雨.一直以来,CSDN都是我最好的编程助手.这是我在CSDN的第 ...
- 【转】 NGUI 监听按钮除OnClick外其他事件的方法,附简易改编的UIButton类
http://blog.csdn.net/icefantasylcj/article/details/49450555 大家好,我是雨中祈雨.一直以来,CSDN都是我最好的编程助手.这是我在CSDN的 ...
- 使用HTML+CSS,jQuery编写的简易计算器后续(添加了键盘监听)
之前发布了一款简易的计算器,今天做了一下修改,添加了键盘监听事件,不用再用鼠标点点点啦 JS代码: var yunSuan = 0;// 运算符号,0-无运算;1-加法;2-减法;3-乘法;4-除法 ...
- 【iOS】7.4 定位服务->2.1.3.3 定位 - 官方框架CoreLocation 功能3:区域监听
本文并非最终版本,如果想要关注更新或更正的内容请关注文集,联系方式详见文末,如有疏忽和遗漏,欢迎指正. 本文相关目录: ================== 所属文集:[iOS]07 设备工具 === ...
- mui框架下监听返回按钮
用于监听mui框架下的Android手机的返回按键(物理键) mui.back = function() { if(b == true) {//一个标识符,在某个状态下不允许双击返回关闭程序 aler ...
- Android图片加载框架最全解析(四),玩转Glide的回调与监听
大家好,今天我们继续学习Glide. 在上一篇文章当中,我带着大家一起深入探究了Glide的缓存机制,我们不光掌握了Glide缓存的使用方法,还通过源码分析对缓存的工作原理进行了了解.虽说上篇文章和本 ...
- MVC框架入门准备(三)事件类 - 事件的监听和触发
在mvc框架中可以看到事件类,实现事件的监听和触发. 举例: <?php /** * 事件类 */ class Event { // 事件绑定记录 private static $events; ...
- Unity中利用委托与监听解耦合的思路
这篇随笔是一篇记录性的随笔,记录了从http://www.sikiedu.com/my/course/304,这门课程中学到的内容,附带了一些自己的思考. 一.单例模式的应用 首先假想一种情况,现在需 ...
随机推荐
- libsvm easy.py ValueError: need more than 0 values to unpack windows下终极解决
现象是: python easy.py train test 输出: Scaling training data...WARNING: original #nonzeros 100389 new #n ...
- 微信小程序 模块化
模块化也就是将一些通用的东西抽出来放到一个文件中,通过module.exports去暴露接口.我们在最初新建项目时就有个util.js文件就是被模块化处理时间的 /** * 处理具体业务逻辑 */ f ...
- 去OpenCVManager,大部分为转载,仅当自己学习使用
去OpenCVManager方法,可以参考这篇博客http://blog.csdn.net/yanzi1225627/article/details/27863615,可以用,挺好的.我这里只是做个总 ...
- python tensorflow方法手记
Variable tf.Variable(initializer, name)给变量取名initializer是初始化参数,可以有tf.random_normal,tf.constant等.name就 ...
- java API Runtime 启动进程
Runtime run = new Runtime.getRuntime(); Process p = run.exec("notepad.exe F:\\lesson\\a.java&qu ...
- Linux常用命令及Vim使用
1.ls 命令 --------------------------------------------------------------------- ls以默认方式显示当前目录文件列表 ls - ...
- poj3481(splay tree 入门题)
平衡树都能做. // // main.cpp // splay // // Created by 陈加寿 on 16/3/25. // Copyright © 2016年 chenhuan001. A ...
- 【BZOJ4176】Lucas的数论 莫比乌斯反演
[BZOJ4176]Lucas的数论 Description 去年的Lucas非常喜欢数论题,但是一年以后的Lucas却不那么喜欢了. 在整理以前的试题时,发现了这样一道题目“求Sigma(f(i)) ...
- RTSP安防摄像机(海康大华宇视等)如何推送到RTMP流媒体服务器进行直播
方案介绍 目前互联网直播的CDN和标准RTMP流媒体服务器通常只能接收RTMP格式的音视频推流.目前市场上有一些自带RTMP推流的摄像机和编码器,可以直接在其rtmp推流配置里面配置推送到RTMP流媒 ...
- Architectural Styles and the Design of Network-based Software Architectures
w Architectural Styles and the Design of Network-based Software Architectures http://www.ics.uci.ed ...