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,IAwake<int>,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.Brightness=a;// 设置初始亮度}[EntitySystem]privatestaticvoidDestroy(thisMonitorComponentself){Log.Debug("MonitorComponent Destroy");}// 自定义方法:调整亮度publicstaticvoidChangeBrightness(thisMonitorComponentself,intvalue){self.Brightness=value;}}}- 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());}}- 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();}}}