ET框架7.2的ECS编程练习

 

目录

ECS

什么是ECS

 ECS编程原则

用ECS思想在客户端编写一台电脑

1.创建ECS的实体E及组件C

2.编写组件和实体的行为也就是System 

 3.为实体组装组件

 4.运行

 同理在服务端编写一台电脑

1.创建ECS的实体E及组件C

2.编写组件和实体的行为也就是System 

 3.为实体组装组件

 4.运行


TIPS:类比6学习ET7 

ECS

什么是ECS

实体即组件,组件即实体

遵循组合优于继承的原则
Entity——实体:实体只是一个概念上的定义,指的是存在你游戏世界中的一个独特物体,是一系列组件的集合
Component——组件:一个组件是一堆数据的集合组件没有方法,即不存在任何的行为,只用来存储状态。
System——系统:  实体和组件的一系列行为、功能、逻辑; 一个系统就是对拥有一个或多个相同组件的实体集合进行操作的工具,它只有行为,没有状态,即不应该存放任何数据。


 ECS编程原则

  1. 实体既组件,组件既实体。
  2. 如要编写一个新的实体或者组件,绝不继承除Entity之外的任何父类!
  3. 绝不使用任何的虚函数,使用逻辑分发替代。
  4. Model和ModelView只存放实体和组件的数据字段声明,如非必要绝不放任何逻辑函数。
  5. Hotfix和HotfixView中只保留纯逻辑函数,也就是使用静态类和扩展方法编写的System,且绝不允许存在任何数据字段。
  6. Model和Hotfix中绝不允许出现跟Unity3d引擎相关的游戏对象类和调用相关API函数。
  7. 如实体或组件有数据字段声明必须编写相关生命周期函数,以防实体对象池回收再利用导致逻辑错误

用ECS思想在客户端编写一台电脑

1.创建ECS的实体E及组件C

在下图中的路径下创建文件夹Computer,并创建电脑及组件相关的实体类

注意:实体只能继承Entity

电脑实体:

namespace ET.Client
{
    [ChildOf(typeof(Scene))]       //声明Computer是Scene的子实体
    public class Computer : Entity, IAwake,IUpdate, IDestroy //IAwake,IUpdate, IDestroy 为生命周期函数
    {

    }
}

 机箱组件: 机箱本质上也是一个实体所以继承 Entity,下边同理

namespace ET.Client
{
    [ComponentOf(typeof(Computer))]  //把机箱组件挂在到Computer上
    public class PCCaseComponent : Entity,IAwake
    {

    }
}

显示器组件:

namespace ET.Client
{
    [ComponentOf(typeof(Computer))]
    public class MonitorsComponent : Entity, IAwake
    {
    }
}

 鼠标组件:

namespace ET.Client
{
    [ComponentOf(typeof(Computer))]
    public class MouseComponent : Entity, IAwake
    {
    }
}

键盘组件:

namespace ET.Client
{
    [ComponentOf(typeof(Computer))]
    public class KeyboardComponent : Entity, IAwake
    {
    }
}

2.编写组件和实体的行为也就是System 

在下图中的路径下创建文件夹Computer,并创建电脑及组件相关的执行逻辑类

 电脑的行为:

namespace ET.Client
{
    public static class ComputerSystem
    {
        public static void Start(this Computer self)  
        {
            Log.Debug("Computer Start!!!!!!");
            self.GetComponent<PCCaseComponent>().StartPower();
            self.GetComponent<MonitorsComponent>().Display();
        }
    }
}

机箱的行为:

namespace ET.Client
{
    public static  class PCCaseComponentSystem
    {
        public static void StartPower(this PCCaseComponent self) 
        {
            Log.Debug("start power!!!!!!!");  //启动电源
        }
    }
}

显示器的行为:

namespace ET.Client
{
    public static class MonitorsComponentSystem
    { 
        public static void Display(this MonitorsComponent self)
        { 
            Log.Debug("monitors start display!!!!!");  //显示器开始显示
        }
    } 
}

 3.为实体组装组件

namespace ET.Client
{
	[Event(SceneType.Client)]
	public class AppStartInitFinish_CreateLoginUI: AEvent<EventType.AppStartInitFinish>
	{
		protected override async ETTask Run(Scene scene, EventType.AppStartInitFinish args)
		{
			await UIHelper.Create(scene, UIType.UILogin, UILayer.Mid);

			Computer computer = scene.AddChild<Computer>();
			computer.AddComponent<PCCaseComponent>();
			computer.AddComponent<MonitorsComponent>();
			computer.AddComponent<KeyboardComponent>();
			computer.AddComponent<MouseComponent>();
			computer.Start();
		}
	}
}

 4.运行

 同理在服务端编写一台电脑

1.创建ECS的实体E及组件C

在下图中的路径下创建文件夹Computer,并创建电脑及组件相关的实体类

 电脑实体:

namespace ET.Server
{
    [ChildOf(typeof(Scene))]       //声明Computer是Scene的子实体
    public class Computer : Entity, IAwake,IUpdate, IDestroy //IAwake,IUpdate, IDestroy 为生命周期函数
    {

    }
}

 机箱组件: 机箱本质上也是一个实体所以继承 Entity,下边同理

namespace ET.Server
{
    [ComponentOf(typeof(Computer))]  //把机箱组件挂在到Computer上
    public class PCCaseComponent : Entity,IAwake
    {

    }
}

显示器组件:

namespace ET.Server
{
    [ComponentOf(typeof(Computer))]
    public class MonitorsComponent : Entity, IAwake
    {
    }
}

 鼠标组件:

namespace ET.Server
{
    [ComponentOf(typeof(Computer))]
    public class MouseComponent : Entity, IAwake
    {
    }
}

键盘组件:

namespace ET.Server
{
    [ComponentOf(typeof(Computer))]
    public class KeyboardComponent : Entity, IAwake
    {
    }
}

2.编写组件和实体的行为也就是System 

在下图中的路径下创建文件夹Computer,并创建电脑及组件相关的执行逻辑类

  电脑的行为:

namespace ET.Server
{
    public static class ComputerSystem
    {
        public static void Start(this Computer self)  
        {
            Log.Debug("Computer Start!!!!!!");
            self.GetComponent<PCCaseComponent>().StartPower();
            self.GetComponent<MonitorsComponent>().Display();
        }
    }
}

机箱的行为:

namespace ET.Server
{
    public static  class PCCaseComponentSystem
    {
        public static void StartPower(this PCCaseComponent self) 
        {
            Log.Debug("start power!!!!!!!");  //启动电源
        }
    }
}

显示器的行为:

namespace ET.Server
{
    public static class MonitorsComponentSystem
    { 
        public static void Display(this MonitorsComponent self)
        { 
            Log.Debug("monitors start display!!!!!");  //显示器开始显示
        }
    } 
}

 3.为实体组装组件

Computer computer = scene.AddChild<Computer>();
computer.AddComponent<PCCaseComponent>();
computer.AddComponent<MonitorsComponent>();
computer.AddComponent<KeyboardComponent>();
computer.AddComponent<MouseComponent>();
computer.Start();

 4.运行

重新生成解决方案运行


通过Logs文件夹下的Debug日志查看打印结果 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity ET框架是一套双端框架,可以同时支持前端和后端开发。它提供了一些特色功能,比如ET版本的ECS和异步模块,以及双端网络模块。使用ET框架可以提高双端开发的效率,因为前后端的代码可以共享。然而,需要注意的是,ET框架限制了后端只能使用C#开发语言。此外,ET框架的功能并不是非常完整和精细,特别是前端的框架部分,可能只能作为一个建议的demo使用,无法满足商业项目的需求。\[1\] 相比之下,GF框架是一套比较成熟、完整的游戏框架,适用于任何游戏引擎。GF框架提供了几乎所有在Unity开发游戏中可能用到的模块,结构清晰、耦合度低。然而,由于GF框架的完整性,学习成本可能会比较高,需要一定的适应时间。\[2\] 总的来说,ET框架更适合有经验的小团队或个人游戏开发者,尤其是那些熟悉C#语言的开发者。而GF框架则更适合需要一个完整、成熟的游戏框架的开发团队。选择哪个框架取决于团队的需求和开发经验。 #### 引用[.reference_title] - *1* *2* *3* [Unity 游戏框架之GameFramework和ET对比](https://blog.csdn.net/qq563129582/article/details/106993157)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值