原本这里的代码用于unity,但是写法与任何unityAPI没关联,所以只要是c#语言的都可以用
用法:
- 首先定义一个字符串常量,用于对某事件的意义进行标明,例如
public static string showDebugDrawGo = "showDebugDrawGo";
- 然后,在某个地方对这个事件进行监听
MsgUtil.AddEventListener(MsgEvents.showDebugDrawGo, (o) =>
{
//o是个object数组,如果长度不是0,需要里面数据的时候需要显示转换为自己所需要的类型
//to do what u want to do
});
- 最后,当这个事件被调起的时候,上述监听的内容就会被执行
//可以传若干个参数,当然需要装箱拆箱,会引起GC操作
//nowSurGo 会被传递到上面的o数组里面
MsgUtil.CallFunc(MsgEvents.showDebugDrawGo, nowSurGo );
将脚本复制到项目,按照上述过程进行使用即可达到目标效果
MsgUtil脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MsgUtil
{
class MsgStruct {
public funcDelegate func;
public int key;
}
private static Hashtable msg_function = new Hashtable();
public delegate void funcDelegate(params object[] bc);
public delegate bool boolDelegate();
private static int count = 0;
public static int total = 0;
public static int AddEventListenerCall(string msg_type, funcDelegate func)
{
func();
return AddEventListener(msg_type, func);
}
public static int AddEventListener(string msg_type, funcDelegate func) {
int index = -1;
if (msg_function.ContainsKey(msg_type) == true) {
List funcArr2 = (List)msg_function[msg_type];
for (int i = 0; i
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?