当前位置: 首页 > 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.jsqmd.com/news/7683/

相关文章:

  • 实用指南:1、docker入门简介
  • 调试parlant的大模型配置,最终自己动手写了g4f的模块挂载 - 教程
  • 太极 - MKT
  • 题解:P12410 「知りたくなかった、失うのなら」
  • unity面向组合开发二:EC的代码实践
  • 《咳咳,未来编程大师,顶尖程序员的第一条博客》
  • CSP-JF36
  • 超越炒作:使用Agentic AI构建系统架构
  • 【进入便捷的系统不解决问题】ubuntu开机出现‘系统出错且无法恢复。请联系系统管理员’
  • K个节点的组内逆序调整
  • 【任务】自然语言处理——情感分析 <上>
  • 2025华为 OD 机试2025C卷 机考真题库清单(全真题库)含考点说明(OD上机考试2025年C卷) - 教程
  • QOJ #8147. Math Exam 题解
  • 10.03模拟赛t3
  • 国庆梦熊集训做题记录
  • 完整教程:缓存总线是什么?
  • 兰博平台: 星云抽卡豪华版. 作者acc177
  • 打工人日报#20250927 - 教程
  • GPT-5 撼动量子计算:AI 在科研领域的颠覆性应用 - 实践
  • python 肘部法则,判点聚类分为几类,K-means聚类分析
  • AT_abc315_f [ABC315F] Shortcuts
  • 紫外UV固化太阳光模拟器的原理 - 教程
  • 《系统与软件工程功能规模测量IFPUG手段》(GB/T42449-2023)标准解读
  • 实用指南:无人机图传及组网功能如何实现?适用频段与传输模块选择全攻略
  • 每日一题
  • P5709 【深基2.习6】Apples Prologue / 苹果和虫子
  • 问题表 - microsoft
  • KD论文阅读 - 实践
  • Leetcode 736. Lisp 语法解析
  • Day10.1