news 2026/5/28 21:14:06

ET8.1-ECS组件式编程示例

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
ET8.1-ECS组件式编程示例

ET事件系统
Entry.Start()->创建MainFiber->FiberInit_Main被触发
按顺序发布事件
EntryEvent1(日志、ID生成)、EntryEvent2(配置加载、网络初始化)、EntryEvent3(8 添加全局组件,发布9.AppStartInitFinish)
AppStartInitFinish:创建UI、创建Computer

目录结构

  1. Computer.cs - 电脑实体(数据模型)
namespaceET.Client{[ChildOf(typeof(ComputersComponent))]// 标记:是ComputersComponent的子实体publicclassComputer:Entity,IAwake,IUpdate,IDestroy{// 只定义数据,不写方法}}

说明:

  • [ChildOf]:标记 Computer 必须是 ComputersComponent 的子实体
  • IAwake:初始化接口
  • IUpdate:每帧更新接口
  • IDestroy:销毁接口

  1. ComputersComponent.cs - 电脑集合(数据模型)
namespaceET.Client{[ComponentOf(typeof(Scene))]// 标记:挂载到Scene上publicclassComputersComponent:Entity,IAwake{// 管理所有Computer的容器}}

  1. PCCaseCompenent.cs - 机箱组件(数据模型)
namespaceET.Client{[ComponentOf(typeof(Computer))]// 挂载到Computer上publicclassPCCaseCompenent:Entity,IAwake{// 机箱数据(比如温度、风扇转速等)}}

  1. MonitorComponent.cs - 显示器组件(数据模型)
namespaceET.Client{[ComponentOf(typeof(Computer))]// 挂载到Computer上publicclassMonitorComponent:Entity,IAwake<int>,IDestroy{publicintBrightness;// 亮度数据}}

说明:

  • IAwake:初始化时接收一个 int 参数(初始亮度)
  • 有 Brightness 字段存储亮度值

  1. ComputerSystem.cs - 电脑逻辑(系统)
namespaceET.Client{[EntitySystemOf(typeof(Computer))]// 标记这是Computer的系统类publicstaticpartialclassComputerSystem{// 初始化时自动调用[EntitySystem]privatestaticvoidAwake(thisComputerself){Log.Debug("Computer Awake");}// 每帧更新时自动调用[EntitySystem]privatestaticvoidUpdate(thisComputerself){Log.Debug("Computer Update");}// 销毁时自动调用[EntitySystem]privatestaticvoidDestroy(thisComputerself){Log.Debug("Computer Destroy");}// 自定义方法:开机publicstaticvoidOpen(thisComputerself){Log.Debug("Computer Open!\nWelcome");}}}

  1. PCCaseComponentSystem.cs - 机箱逻辑(系统)
namespaceET.Client{[EntitySystemOf(typeof(PCCaseCompenent))]publicstaticpartialclassPCCaseComponentSystem{[EntitySystem]privatestaticvoidAwake(thisPCCaseCompenentself){Log.Debug("PCCaseComponent Awake");}}}

  1. MonitorComponentSystem.cs - 显示器逻辑(系统)
namespaceET.Client{[EntitySystemOf(typeof(MonitorComponent))][FriendOfAttribute(typeof(MonitorComponent))]// 友元类:可访问私有成员publicstaticpartialclassMonitorComponentSystem{// 初始化时接收亮度参数[EntitySystem]privatestaticvoidAwake(thisMonitorComponentself,inta){Log.Debug("MonitorComponent Awake");self.Brightness=a;// 设置初始亮度}[EntitySystem]privatestaticvoidDestroy(thisMonitorComponentself){Log.Debug("MonitorComponent Destroy");}// 自定义方法:调整亮度publicstaticvoidChangeBrightness(thisMonitorComponentself,intvalue){self.Brightness=value;}}}

  1. EntryEvent3_InitClient.cs - 事件处理器
[Event(SceneType.Main)]// 监听Main场景的EntryEvent3publicclassEntryEvent3_InitClient:AEvent<Scene,EntryEvent3>{protectedoverrideasyncETTaskRun(Sceneroot,EntryEvent3args){// 添加各种全局组件root.AddComponent<GlobalComponent>();root.AddComponent<UIGlobalComponent>();root.AddComponent<UIComponent>();root.AddComponent<ResourcesLoaderComponent>();root.AddComponent<PlayerComponent>();root.AddComponent<CurrentScenesComponent>();root.AddComponent<ComputersComponent>();// ← 你添加的!// 修改Scene类型SceneTypesceneType=EnumHelper.FromString<SceneType>(globalComponent.GlobalConfig.AppType.ToString());root.SceneType=sceneType;// 发布下一个事件:AppStartInitFinishawaitEventSystem.Instance.PublishAsync(root,newAppStartInitFinish());}}

  1. AppStartInitFinish_CreateLoginUI.cs - 使用代码
namespaceET.Client{[Event(SceneType.Demo)]publicclassAppStartInitFinish_CreateLoginUI:AEvent<Scene,AppStartInitFinish>{protectedoverrideasyncETTaskRun(Sceneroot,AppStartInitFinishargs){awaitUIHelper.Create(root,UIType.UILogin,UILayer.Mid);// 获取或创建 ComputersComponentComputersComponentcomputersComponent=root.GetComponent<ComputersComponent>();// 创建一台电脑Computercomputer=computersComponent.AddChild<Computer>();// 添加机箱组件computer.AddComponent<PCCaseCompenent>();// 添加显示器组件,初始亮度为10computer.AddComponent<MonitorComponent,int>(10);// 开机computer.Open();// 调整显示器亮度为5computer.GetComponent<MonitorComponent>().ChangeBrightness(5);// 等待3秒后销毁awaitroot.GetComponent<TimerComponent>().WaitAsync(3000);computer?.Dispose();}}}
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/28 21:13:55

深入理解 Claude Code 的 Token 预算与压缩策略

开源版Claude Code可运行源码 觉得有用可以给个star 谢谢啦 本文详细讲解 Claude Code 如何在有限的上下文窗口中管理 Token 预算&#xff0c;以及当空间不足时采用的各种压缩策略。每种策略都配有具体的代码示例和实际场景说明。 目录 Token 预算基础知识上下文窗口与阈值计算…

作者头像 李华
网站建设 2026/5/28 21:13:56

告别手动接线!用这个十几块的USB烧录器搞定ESP01S(Arduino IDE环境)

零基础玩转ESP01S&#xff1a;USB烧录器Arduino IDE极简指南 每次看到桌上散落的杜邦线和反复插拔的USB转TTL模块&#xff0c;总让我想起初学物联网开发时的狼狈——ESP01S的烧录过程简直是一场对耐心的终极考验。GPIO0需要手动拉低进入下载模式&#xff0c;EN引脚要确保正确电…

作者头像 李华
网站建设 2026/5/23 2:03:24

AI赋能紫微斗数,能做到多专业的命理解析?

「天府Agent」是目前国内少见的将传统命理与AI技术深度融合的专业分析工具&#xff0c;其他同类产品大多还停留在简单排盘或者话术生成的阶段。我们发现很多用户拿到工具之后&#xff0c;只会用基础的排盘功能&#xff0c;完全没发挥出它的全部能力。今天就一步步教大家&#x…

作者头像 李华
网站建设 2026/5/23 2:02:35

【Proteus 仿真实战】基于51单片机的智能测距与自适应报警系统设计

1. 项目背景与核心功能 最近在做一个基于51单片机的智能测距系统仿真项目&#xff0c;发现很多初学者对如何实现自适应报警功能特别感兴趣。这个项目最吸引人的地方在于它不仅仅是个简单的距离测量装置&#xff0c;而是能根据危险程度自动调整报警策略的智能系统。想象一下&…

作者头像 李华
网站建设 2026/5/23 2:02:24

ROG笔记本色彩管理与配置优化指南:从问题诊断到专业校准

ROG笔记本色彩管理与配置优化指南&#xff1a;从问题诊断到专业校准 【免费下载链接】g-helper Lightweight, open-source control tool for ASUS laptops and ROG Ally. Manage performance modes, fans, GPU, battery, and RGB lighting across Zephyrus, Flow, TUF, Strix, …

作者头像 李华