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

张开发
2026/5/22 10:47:56 15 分钟阅读
ET8.1-ECS组件式编程示例
ET事件系统Entry.Start()-创建MainFiber-FiberInit_Main被触发按顺序发布事件EntryEvent1(日志、ID生成)、EntryEvent2(配置加载、网络初始化)、EntryEvent3(8 添加全局组件发布9.AppStartInitFinish)AppStartInitFinish创建UI、创建Computer目录结构Computer.cs - 电脑实体数据模型namespaceET.Client{[ChildOf(typeof(ComputersComponent))]// 标记是ComputersComponent的子实体publicclassComputer:Entity,IAwake,IUpdate,IDestroy{// 只定义数据不写方法}}说明[ChildOf]标记 Computer 必须是 ComputersComponent 的子实体IAwake初始化接口IUpdate每帧更新接口IDestroy销毁接口ComputersComponent.cs - 电脑集合数据模型namespaceET.Client{[ComponentOf(typeof(Scene))]// 标记挂载到Scene上publicclassComputersComponent:Entity,IAwake{// 管理所有Computer的容器}}PCCaseCompenent.cs - 机箱组件数据模型namespaceET.Client{[ComponentOf(typeof(Computer))]// 挂载到Computer上publicclassPCCaseCompenent:Entity,IAwake{// 机箱数据比如温度、风扇转速等}}MonitorComponent.cs - 显示器组件数据模型namespaceET.Client{[ComponentOf(typeof(Computer))]// 挂载到Computer上publicclassMonitorComponent:Entity,IAwakeint,IDestroy{publicintBrightness;// 亮度数据}}说明IAwake初始化时接收一个 int 参数初始亮度有 Brightness 字段存储亮度值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);}}}PCCaseComponentSystem.cs - 机箱逻辑系统namespaceET.Client{[EntitySystemOf(typeof(PCCaseCompenent))]publicstaticpartialclassPCCaseComponentSystem{[EntitySystem]privatestaticvoidAwake(thisPCCaseCompenentself){Log.Debug(PCCaseComponent Awake);}}}MonitorComponentSystem.cs - 显示器逻辑系统namespaceET.Client{[EntitySystemOf(typeof(MonitorComponent))][FriendOfAttribute(typeof(MonitorComponent))]// 友元类可访问私有成员publicstaticpartialclassMonitorComponentSystem{// 初始化时接收亮度参数[EntitySystem]privatestaticvoidAwake(thisMonitorComponentself,inta){Log.Debug(MonitorComponent Awake);self.Brightnessa;// 设置初始亮度}[EntitySystem]privatestaticvoidDestroy(thisMonitorComponentself){Log.Debug(MonitorComponent Destroy);}// 自定义方法调整亮度publicstaticvoidChangeBrightness(thisMonitorComponentself,intvalue){self.Brightnessvalue;}}}EntryEvent3_InitClient.cs - 事件处理器[Event(SceneType.Main)]// 监听Main场景的EntryEvent3publicclassEntryEvent3_InitClient:AEventScene,EntryEvent3{protectedoverrideasyncETTaskRun(Sceneroot,EntryEvent3args){// 添加各种全局组件root.AddComponentGlobalComponent();root.AddComponentUIGlobalComponent();root.AddComponentUIComponent();root.AddComponentResourcesLoaderComponent();root.AddComponentPlayerComponent();root.AddComponentCurrentScenesComponent();root.AddComponentComputersComponent();// ← 你添加的// 修改Scene类型SceneTypesceneTypeEnumHelper.FromStringSceneType(globalComponent.GlobalConfig.AppType.ToString());root.SceneTypesceneType;// 发布下一个事件AppStartInitFinishawaitEventSystem.Instance.PublishAsync(root,newAppStartInitFinish());}}AppStartInitFinish_CreateLoginUI.cs - 使用代码namespaceET.Client{[Event(SceneType.Demo)]publicclassAppStartInitFinish_CreateLoginUI:AEventScene,AppStartInitFinish{protectedoverrideasyncETTaskRun(Sceneroot,AppStartInitFinishargs){awaitUIHelper.Create(root,UIType.UILogin,UILayer.Mid);// 获取或创建 ComputersComponentComputersComponentcomputersComponentroot.GetComponentComputersComponent();// 创建一台电脑ComputercomputercomputersComponent.AddChildComputer();// 添加机箱组件computer.AddComponentPCCaseCompenent();// 添加显示器组件初始亮度为10computer.AddComponentMonitorComponent,int(10);// 开机computer.Open();// 调整显示器亮度为5computer.GetComponentMonitorComponent().ChangeBrightness(5);// 等待3秒后销毁awaitroot.GetComponentTimerComponent().WaitAsync(3000);computer?.Dispose();}}}

更多文章