ECS & Monobehaviours

Monobehaviours: C#类,引用类型,数据分散在内存中,当数据取回时,系统需要遍历内存去寻找,操作时不需要的数据也被处理

ECS: 使用值类型,数据会在内存中紧密地装在一块(chunk),仅处理需要操作的数据

Entities只是帮助我们找到(component)数据的handle

Components举例:(Component只是模块化的数据)

1
2
3
4
5
6
7
8
9
10
11
12
struct Health : IComponentData
{
int current;
int max;
}

struct Sprite2D : IComponentData
{
Entity image;
Rect imageRegion;
Vector2 pivot;
}

Entities举例:

比如某个Entity 1有四个组件(Component),LocalPosition组件,Sprite2DRenderer组件,Health组件,Player组件(这里没有行为,行为由系统(System)给出)

Systems不在乎它处理的生命值是玩家的生命值还是一个兽人或者哥布林的生命值

Systems包括两部分,一是query,帮我找到这个数据,那个数据…,找到了之后对数据的action

用System的方式筛选要操作的rntities:

1
2
3
4
5
6
7
8
9
10
11
12
public override void OnUpdate()
{
var dt = Scheduler.DeltaTime();
ForEach(
LocalPosition
MoveToTarget
Not : Frozen => //注意查询时的这种筛选,用Not
{
//actions
}
);
}