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

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

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.lryc.cn/news/539398.html

相关文章:

  • 确保设备始终处于最佳运行状态,延长设备的使用寿命,保障系统的稳定运行的智慧地产开源了
  • RedisTemplate存储含有特殊字符解决
  • 28、深度学习-自学之路-NLP自然语言处理-做一个完形填空,让机器学习更多的内容程序展示
  • 【NLP 22、语言模型 language model】
  • 刚性平衡机建模
  • 【算法】双指针(上)
  • 【Linux Redis】关于用docker拉取Redis后,让虚拟机运行起来redis,并使得其可以连接到虚拟机外的navicat。
  • 用deepseek学大模型04-模型可视化与数据可视化
  • 一周学会Flask3 Python Web开发-post请求与参数获取
  • 第3章 .NETCore核心基础组件:3.1 .NET Core依赖注入
  • cs*n 网页内容转为html 加入 onenote
  • 平板作为电脑拓展屏
  • Pytorch实现论文之一种基于扰动卷积层和梯度归一化的生成对抗网络
  • 关系数据库标准语言SQL
  • AI工具篇:利用DeepSeek+Kimi 辅助生成综述汇报PPT
  • 学习总结2.18
  • electron下载文件,弹窗选择下载路径,并通知下载进度
  • 【Docker】容器被停止/删除的方式及命令:全面解析与实践指南
  • 线上就医全流程医药机构接入文档接口代码-医保就医接口php-demo版本
  • 缓存三大问题及其解决方案
  • 大语言模型常用微调与基于SFT微调DeepSeek R1指南
  • LabVIEW的吞雨测控系统
  • redis基础命令
  • 基于Java+SpringBoot+Vue的前后端分离的校园闲置物品交易网站
  • K8s 证书认知(K8s Certificate Awareness)
  • 前x-ai首席科学家karpathy的从零构建ChatGPT视频学习笔记--8000字长图文笔记预警(手打纯干货,通俗易懂)
  • 【智慧校园】分体空调节能监管:打造高效节能的学习环境
  • 深度学习-1.简介
  • 【Rust中级教程】1.10. 引用及内部可变性(简单回顾):引用、内部可变性、`Cell`类型及相关操作
  • Docker 安装和配置 Nginx 详细图文教程