当前位置: 首页 > news >正文

Unity简易事件分发器

一、EventFunction

using System;
namespace EventCore
{public struct EventFunction{public object _caller;public Action _action;}public struct EventFunction<T>{public object _caller;public Action<T> _action;}
}

二、Event<T>

using System;
using System.Collections.Generic;
using UnityEngine;namespace EventCore
{public class Event<T>{private readonly List<EventFunction<T>> _event = new List<EventFunction<T>>();public void AddListener(object caller, Action<T> action){if (action != null){for (int index = 0, max = _event.Count; index < max; index++){var element = _event[index];if (element._caller == caller && element._action == action){Debug.LogError("EventGeneric,重复添加事件:" + element._caller + "," + element._action.Method);return;}}}_event.Add(new EventFunction<T> { _caller = caller, _action = action });}public void RemoveListener(object caller, Action<T> action){for (int index = _event.Count - 1; index >= 0; index--){var element = _event[index];if (element._caller == caller && element._action == action){_event.RemoveAt(index);return;}}}public void Trigger(T data){for (int index = _event.Count - 1; index >= 0; index--){var element = _event[index];if (element._action != null){element._action(data);}else{Debug.LogError("EventGeneric: event removed");}}}}
}

三、Event

using System;
using System.Collections.Generic;
using UnityEngine;namespace EventCore
{public class Event{private readonly List<EventFunction> _event = new List<EventFunction>();public void AddListener(object caller, Action action){if (action != null){for (int index = 0, max = _event.Count; index < max; index++){var element = _event[index];if (element._caller == caller && element._action == action){Debug.LogError("EventGeneric,重复添加事件:" + element._caller + "," + element._action.Method);return;}}}_event.Add(new EventFunction { _caller = caller, _action = action });}public void RemoveListener(object caller, Action action){for (int index = _event.Count - 1; index >= 0; index--){var element = _event[index];if (element._caller == caller && element._action == action){_event.RemoveAt(index);return;}}}public void Trigger(){for (int index = _event.Count - 1; index >= 0; index--){var element = _event[index];if (element._action != null){element._action();}else{Debug.LogError("EventGeneric: event removed");}}}}
}

四、使用分发器

事件定义:

using EventCore;public class TestEvent
{public static Event<int> TestEvent1 = new Event<int>(); // 事件测试public static Event TestEvent2 = new Event(); // 事件测试
}

事件注册、事件注销:

TestEvent.TestEvent2.AddListener(this, OnTestEvent);
TestEvent.TestEvent2.RemoveListener(this, OnTestEvent);private void OnTestEvent()
{}

事件触发:

TestEvent.TestEvent2.Trigger();
http://www.rkmt.cn/news/15268.html

相关文章:

  • 题解:P12410 「知りたくなかった、失うのなら」
  • 《咳咳,未来编程大师,顶尖程序员的第一条博客》
  • 【任务】自然语言处理——情感分析 <上>
  • 完整教程:缓存总线是什么?
  • 紫外UV固化太阳光模拟器的原理 - 教程
  • 实用指南:无人机图传及组网功能如何实现?适用频段与传输模块选择全攻略
  • P5709 【深基2.习6】Apples Prologue / 苹果和虫子
  • Day10.1
  • 《电路基础》第八章学习笔记
  • XGBoost
  • 【K8S】Kubernetes 调度器深度解析:原理与源码分析
  • 深入解析:逻辑回归(Logistic Regression)
  • 题解:qoj6504 Flowers Land 2
  • 详细介绍:范式革命:RDMA 如何让网络成为 “分布式内存总线”
  • bMIND包本地安装
  • 网络实践——基于epoll_ET工作、Reactor设计模式的HTTP服务 - 实践
  • “AI元人文”构想说明:构建智能时代的人文学科新范式
  • ZR 2025 十一集训 #1
  • Channel-Driven 降低模块耦合设计复杂度
  • PML(Perfect Match Layer)介绍 - 实践
  • 2025山东设备回收公司 TOP 交易服务推荐排行榜,济宁,梁山设备回收,二手,饮料,食品,制药,实验室,生产线,化工厂,废旧,大型,专业设备回收公司推荐
  • 2025饮料设备回收公司 TOP 交易服务推荐排行榜,济宁,梁山饮料设备回收果汁饮料整厂,饮料生产线,碳酸饮料,乳制品,杀菌机,果汁饮料,二手灌装机,果汁设备回收公司推荐
  • 股票数据api接口编写从入门到精通:选型策略、服务商评测与代码示例
  • 基于SpringBoot3+Vue3的校园跑腿架构、校园跑腿服务平台、校园接单系统、智能校园跑腿平台、协同过滤推荐算法,毕业设计,课程设计
  • 初识文件管理
  • 2025 年激光粒度仪厂家 TOP 企业品牌推荐排行榜,电位仪 / 纳米粒度及 Zeta 电位仪 / Zeta 电位仪公司推荐
  • 完整教程:从 “T+1” 到 “秒级”:MySQL+Flink+Doris 构建实时数据分析全链路
  • 平均数
  • 从刷抖音说起:你每天都在 “用” 服务器,却可能不认识它 - 详解
  • 详细介绍:Java安全“幽灵”:深入剖析内存马的原理、注入与查杀