DeepSeek教unity------事件管理

server/2025/2/23 4:25:17/

1. 定义事件类型

定义一个枚举来表示不同类型的事件。组织和识别不同的事件。

2. 创建事件参数类

为了让事件携带数据,创建一个通用的事件参数类或者为每个事件类型创建特定的参数类。

3. 实现事件管理器

创建一个EventManager类,用于管理事件的注册、注销和触发。

/****************************************************文件:EventManager.cs作者:Edision日期:#CreateTime#功能:事件管理
*****************************************************/using System;
using System.Collections.Generic;public enum EventType
{PlayerJump,PlayerAttack,ItemCollected,// 添加更多事件类型...
}public interface IEventParam { }public static class EventManager
{private static Dictionary<EventType, Action<IEventParam>> eventDictionary = new Dictionary<EventType, Action<IEventParam>>();public static void RegisterListener<T>(EventType eventType, Action<T> listener) where T : IEventParam{if (!eventDictionary.ContainsKey(eventType)){eventDictionary[eventType] = param => listener((T)param);}}public static void UnregisterListener<T>(EventType eventType) where T : IEventParam{if (eventDictionary.ContainsKey(eventType)){eventDictionary.Remove(eventType);}}public static void TriggerEvent(EventType eventType, IEventParam eventParam){if (eventDictionary.TryGetValue(eventType, out var action) && action != null){action(eventParam);}}
}
/****************************************************文件:PlayerJumpEventArgs.cs作者:Edision日期:#CreateTime#功能:玩家跳跃事件参数
*****************************************************/public class PlayerJumpEventArgs : IEventParam
{public float JumpForce;public PlayerJumpEventArgs(float jumpForce){JumpForce = jumpForce;}
}

使用:

/****************************************************文件:TestEvent.cs作者:Edision日期:#CreateTime#功能:使用代码测试
*****************************************************/using UnityEngine;public class TestEvent : MonoBehaviour
{private void Awake(){// 注册监听器EventManager.RegisterListener<PlayerJumpEventArgs>(EventType.PlayerJump, OnPlayerJump);}private void OnPlayerJump(PlayerJumpEventArgs args){Debug.Log($"Player jumped with force: {args.JumpForce}");}private void Update(){if (Input.GetKeyDown(KeyCode.I)){// 触发事件EventManager.TriggerEvent(EventType.PlayerJump, new PlayerJumpEventArgs(5f));}if (Input.GetKeyDown(KeyCode.O)){// 移除事件EventManager.UnregisterListener<PlayerJumpEventArgs>(EventType.PlayerJump);}}}


http://www.ppmy.cn/server/170036.html

相关文章

在wsl环境中配置和开发verilog(一种比较新颖的verilog开发指南)

WSL是windows中自带的linux子系统&#xff0c;笔者在若干月前首次接触其便爱不释手&#xff0c;verilog作为一种硬件解释语言&#xff0c;可否像c语言那样被游刃有余的编译和运行呢&#xff0c;笔者这次大胆的尝试在WSL环境VSCODEIverilog开发verilog。 首先默认按照了WSL和VS…

Apache Logic4j 库反序列化漏洞复现与深度剖析

前言 在渗透测试领域&#xff0c;反序列化漏洞一直是安全研究人员和攻击者关注的焦点。今天&#xff0c;我们将深入探讨 Apache Logic4j 库中的反序列化漏洞&#xff0c;详细了解其原理&#xff0c;并进行完整的复现演示。 一、漏洞原理 Apache Logic4j 库在处理对象的反序列…

Typora的Github主题美化

[!note] Typora的Github主题进行一些自己喜欢的修改&#xff0c;主要包括&#xff1a;字体、代码块、表格样式 美化前&#xff1a; 美化后&#xff1a; 一、字体更换 之前便看上了「中文网字计划」的「朱雀仿宋」字体&#xff0c;于是一直想更换字体&#xff0c;奈何自己拖延症…

ubuntu 守护进程

#!/bin/bash # 定义所守护的进程名称或关键字 TARGET_PROCESS"AppRun" while true; do # 检测目标进程是否运行 if pgrep -x "$TARGET_PROCESS" >/dev/null; then echo "The process is running." else # 启…

SpringBoot项目集成MinIO

最近在学习MinIO&#xff0c;所以想让自己的SpringBoot项目集成MinIO,在网上查阅资料&#xff0c;并进行操作的过程中遇到一些问题&#xff0c;所以想把自己遇到的坑和完成步骤记录下来供自己和各位查阅。 一. MinIO的下载安装以及基本使用 1. 下载地址&#xff1a;https://d…

通过例子学 rust 个人精简版 5-all

5 类型转换 fn main() {let decimal 65.4321_f32;let integer decimal as u8;let character integer as char;println!("Casting: {} -> {} -> {}", decimal, integer, character);//Casting: 65.4321 -> 65 -> A }要点1 &#xff1a; 精度丢失个人…

基于微信小程序的民宿短租系统设计与实现(ssm论文源码调试讲解)

第4章 系统设计 4.1系统设计的目标 系统设计的目标是满足用户的需求和满足系统实现所需要的所有要求。本系统收集了信息浏览、信息删除、信息添加、信息修改、信息查询为一体[17]。改变了用户民宿短租的方式&#xff0c;提高管理员管理效率以及用户预订的效率。为用户、房主提…

如何通过 Python 实现一个消息队列,为在线客服系统与海外运营的APP对接

对方有两个核心需求: 访客上线的时候,要通知对方的业务系统,业务系统根据访客的身份信息,推送个性化的欢迎词。访客完成下单的时候,要能推送一个下单成功的通知,并且包含订单信息和链接。根据这两个需求,那就需要实现由客服系统到业务系统的消息队列推送,以及通过 Open…