02-ET框架的ECS编程思想

本文介绍了ET框架中的ECS编程模型,包括实体(Entity)、组件(Component)和系统(System)的概念。通过实例展示了如何创建实体、组件和系统,并详细解释了如何在实体上添加组件以及如何在系统中实现逻辑。最后,提供了代码示例来演示在ET框架中应用ECS思想的过程。
摘要由CSDN通过智能技术生成

TIPS:
本系列贴仅用于博主学习ET框架的记录



前言

今天来学习OOP以外的另一种编程思想—ECS。


一、ET框架中的ECS是什么?

ECS:实体(Entity)、组件(Component)、系统(System),同时在框架中(实体即组件、组件即实体)类似电脑是一个实体,键盘是电脑的一个组件,但同时键盘也是一个实体,因为其下面还有按键这种组件。

二、ET框架的ECS编程思想用法

1.创建ECS中的实体Entity及组件Component

(1)打开ET目录下的Client-Server.sln,在Client/Unity.Model/Codes/Model/Demo文件夹下新建一个Computer文件夹。
在这里插入图片描述
(2)在Computer文件夹下新建一个Computer类命名空间为ET且继承Entity及IAwake接口,注意:实体只能继承Entity,如果继承Entity以外的其他类,就不是ET正规军(狗头保命)同时实体里面不能有任何的逻辑代码(好像是)。

代码如下(示例):Computer.cs

namespace ET
{
    public class Computer:Entity, IAwake
    {
        
    }
}

(3)同时也新建电脑所需的一些组件,例如电源、显示屏,同时命名空间也为ET且继承Entity及IAwake接口,使用[ComponentOf(typeof(Computer))]来说明这个组件是在Computer实体下的组件。

代码如下(示例):KeyboardComponent.cs

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

代码如下(示例):PCCaseComponent.cs

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

2.创建ECS中的系统System

(1)打开ET目录下的Client-Server.sln,在Client/Unity.Hotfix/Codes/Hotfix/Demo文件夹下新建一个Computer文件夹。
在这里插入图片描述
(2)在Computer文件夹下新建一个ComputerSystem类,且是静态的,实体逻辑代码写在System里)。

代码如下(示例):ComputerSystem.cs

namespace ET
{
    public static class ComputerSystem
    {
        public static void Start(this Computer self)
        {
            Log.Debug("computer start....");
            //下面这两行获取组件要在实体添加了组件后才能获取到
            self.GetComponent<PCCaseComponent>().Power();
            self.GetComponent<MonitorsComponent>().Display();
        }
    }
}

(3)同时也给Computer的PCCaseComponent、MonitorsCompoent建立System。

代码如下(示例):MonitorsComponentSystem.cs

namespace ET
{
    public static class MonitorsComponentSystem
    {
        public static void Display(this MonitorsComponent self)
        {
            Log.Debug("display show....");
        }
    }
}

代码如下(示例):PCCaseComponentSystem.cs

namespace ET
{
    public static class PCCaseComponentSystem
    {
        public static void Power(this PCCaseComponent self)
        {
            Log.Debug("start power!!!");
        }
    }
}

3.给实体添加组件

(1)打开Client/Unity.HotfixView/Codes/HotfixView/Demo文件夹下的AfterCreateZoneScene_AddComponent.cs文件。
在这里插入图片描述
(2)在Run方法的末尾创建Computer实体及添加组件。

代码如下(示例):AfterCreateZoneScene_AddComponent.cs

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

(3)保存代码回到Unity编辑器点击键盘上的F5建等待Unity编译通过即可运行。
在这里插入图片描述
在这里插入图片描述


总结

理解ET框架中的ECS编程思想来说还是相对简单的。

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值