Unity简易事件分发器

news/2025/10/3 17:35:34/文章来源:https://www.cnblogs.com/SummerCode/p/19124792

一、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.mzph.cn/news/926138.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

react怎么做pc网站外贸soho建站

本文给大家整理了腾讯视频网页下载_腾讯视频怎么下载视频方面的内容。腾讯视频独播剧质量还是可以的&#xff0c;比较给力的是腾讯视频大量买入了老剧的版权&#xff0c;不乏一些比较经典的港剧&#xff0c;还把这些老剧修复了。腾讯视频播放器是一款支持多种音视频格式的主流播…

实用指南:1、docker入门简介

实用指南:1、docker入门简介pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco"…

调试parlant的大模型配置,最终自己动手写了g4f的模块挂载 - 教程

调试parlant的大模型配置,最终自己动手写了g4f的模块挂载 - 教程pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "…

网站模板如何使用 如何修改吗网站视频插件

合并分支用rebase还是merge&#xff1f; 实际开发工作的时候&#xff0c;我们都是在自己的分支开发&#xff0c;然后将自己的分合并到主分支&#xff0c;那合并分支用2种操作&#xff0c;这2种操作有什么区别呢&#xff1f; git上新建一个项目&#xff0c;默认是有master分支…

迁安做网站教育培训机构设计图

Java核心类库篇6——IO 1、File 1.1、构造方法 方法声明功能介绍public File(File parent, String child)从父抽象路径名和子路径名字符串创建新的 File实例public File(String pathname)通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例public File(String pa…

12380网站建设情况报告网站总体规划设计说明

hive分区重命名后&#xff0c;新的分区的分区大小为0 , 例如 alter table entersv.ods_t_test partition(dt2022-11-08) rename to partition(dt2022-11-21) ods_t_test 的2022-11-21分区大小为0。怎样修复 使用 msck repair table 命令来修复表的元数据&#xff0c;让hive重新…

太极 - MKT

太极 环境 下雨 下午 卧室 附上音乐 (沙石头 鱼儿 本身不也是物质的一部分么,都在不同的层次适应存在。 石头在河里打磨成圆滑,在沙漠变成啥子,这么看好像都是被动的过程。 但本质沙子石头都是原子层面的硅原子在…

佛山营销网站旅游网站建设方案后台

0-1背包理论基础 基础 DP数组与其下标的含义 dp[i][j]&#xff0c;i为物品编号&#xff0c;j为背包容量 dp[i][j]表示从下标为[0-i]的物品里任意取&#xff0c;放进容量为j的背包&#xff0c;价值总和最大是多少。 递推公式 分类&#xff1a;是否要放入下标为i的物品&…

网站建设人员职责分布昌吉网站建设咨询电话

一、智能家居与会议系统 智能家居与会议系统分论坛将于3月28日同期举办&#xff01; 智能会议系统它通过先进的技术手段&#xff0c;提高了会议效率&#xff0c;降低了沟通成本&#xff0c;提升了参会者的会议体验。对于现代企业、政府机构和学术界是不可或缺的。在这里&#x…

题解:P12410 「知りたくなかった、失うのなら」

草 -我ら不会と算に时む复なりlink 说在前面 如果你看了这个东西你最好就看个乐子别真的去写,卡常卡死你。 做法什么的请直接看正文。 注意到其他题解给出了很优美的做法,那么我就来点不优美的。 先设几个数字吧,设…

unity面向组合开发二:EC的代码实践

一、ECCore 需要在Unity项目中使用插件:UniRx,通过UniRx代替Mono的Update,Mono下做轮询性能消耗会有点大。 EntityMono代码:using System; using System.Collections.Generic; using EC; using UniRx; using Unity…

《咳咳,未来编程大师,顶尖程序员的第一条博客》

Helloooooo World!本人目前是一个在校大二的学生,正在备战蓝桥杯,希望有相同目标的朋友联系我,我们可以一起备赛,一起刷题。我的目标是在2026蓝桥杯比赛上拿下国一,哈哈哈哈虽然听起来很扯,但是我是会用拿国一的…

CSP-JF36

CSP-JF36T2 B. 最小的公倍数小题 ((10^L / 210) + 1) * 210 就是最小值#include <bits/stdc++.h> using namespace std;int n; int main(){// for(int i = 2; i <= 18; i++){ // long long x = pow…

airsim多无人机+无人车联合仿真辅导 - 教程

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

超越炒作:使用Agentic AI构建系统架构

本文深入探讨了Agentic AI系统的架构设计,分享了实际应用中的模式、反模式和用例,讨论了如何管理这些分布式系统的复杂性和非确定性,并提供了构建可信赖、可扩展生产系统的实用建议。超越炒作:使用Agentic AI构建系…

河北省建设信息网站seo网站优化平台

12.全排列II 题目描述 给定一个可包含重复数字的序列 nums &#xff0c;按任意顺序 返回所有不重复的全排列。 示例 1&#xff1a; 输入&#xff1a;nums [1,1,2] 输出&#xff1a; [[1,1,2],[1,2,1],[2,1,1]]示例 2&#xff1a; 输入&#xff1a;nums [1,2,3] 输出&…

一个网站的建站流程建设安全协会网站

【题目来源】https://leetcode.cn/problems/valid-parenthesis-string/description/【题目描述】 给你一个只包含三种字符的字符串&#xff0c;支持的字符类型分别是 (、) 和 *。请你检验这个字符串是否为有效字符串&#xff0c;如果是有效字符串返回 true 。 有效字符串符合如…

岷县城乡建设局网站wordpress有多大的数据量

欢迎同步关注公众号【逆向通信猿】 远程声控系统技术报告 一、题目要求 实现一个远程声音控制系统。首先采集不同的语音指示信号,进行适当压缩;然后通过噪声信道实现远程传输,远端接收后再通过适当计算识别出是何指示,最后送入一个处于未知状态、但能控/能观的控制系统,…

【进入便捷的系统不解决问题】ubuntu开机出现‘系统出错且无法恢复。请联系系统管理员’

【进入便捷的系统不解决问题】ubuntu开机出现‘系统出错且无法恢复。请联系系统管理员’2025-10-03 17:09 tlnshuju 阅读(0) 评论(0) 收藏 举报pre { white-space: pre !important; word-wrap: normal !important;…

K个节点的组内逆序调整

K个节点的组内逆序调整题目 给定一个单链表的头节点head,和一个正数k实现k个节点的小组内部逆序,如果最后一组不够k个就不调整 例子: 调整前:1->2->3->4->5->6->7->8,k=3 调整后:3->2-&…