ECS:Entitias

Entities是一个ID

Entities就是游戏中的“对象”或者“单位”,在ECS里面它表示一组数据的组合。
Systems为它提供行为。
Components为之提供数据存储。
 
一个Entity就是一个ID,Entity的ID是稳定的,你可以用它来存储对一个component或entity的引用,比如在hierarchy中,一个child entity需要存储它的parent entity的引用。
 
EntityManager:管理一个World中所有的entities。
它维护一个entities列表,并以高效的方式组织与entity相关联的data。
 
Entity没有type,但是可以按照他拥有的data components组合来进行分类。当你创建entity或动态增删compoents时,EntityManager会实时跟踪现有entities的所有唯一组合。这样一个唯一的组合就叫做Archetype(原型)。

创建Entity

方式一:最容易的方式,在Editor中直接在运行时将Scene中的GameObjects或Prefabs转化为entities。
方式二:更动态的方式是,创建spawning systems用来在job中创建多个entities。(疑问:啥意思?)
方式三:EntityManager.CreateEntity()
    Entity与EntityManager在同一个World中创建。
    可以一个一个的创建:
    public Entity CreateEntity(params ComponentType[] types);
    public Entity CreateEntity(EntityArchetype archetype);
    // 克隆一个已有的entity
    public Entity Instantiate(Entity srcEntity); 
    // 创建一个空的entity,然后再添加components
    public Entity CreateEntity(); 
    也可以一次创建多个:
    public void CreateEntity(EntityArchetype archetype, NativeArray<Entity> entities);
    // 克隆很多个现有entity的拷贝
    public void Instantiate(Entity srcEntity, NativeArray<Entity> outputEntities);
    // 直接创建含有给定ArcheType的Chunk,并填充
    public void CreateChunk(EntityArchetype archetype, NativeArray<ArchetypeChunk> chunks, int entityCount);

增删Components

增删components会导致entity的archetype变化,然后EntityManager会将修改过后的entity移到对应的chunk。
导致entity结构性变化的修改行为,不能在job中执行,因为有可能导致job依赖的数据无效:
    (1)增删components;
    (2)修改SharedComponentData;
    (3)destroying the endity。
EntityCommandBuffer:可以将这些修改命令添加到EntityCommandBuffer,并在job完成后执行这个command buffer。

遍历entities

遍历所有拥有相同archetype的entities的功能是ECS的核心,具体细节在后面章节介绍。

World

一个World = 1个EntityManager + N个ComponentSystems。
游戏中可以创建任意多个World。
一般来说可以创建一个simulation world + rendering or presentation world。
默认情况下,unity会自动创建一个world,并自动填充所有可用的ComponentSystem对象。我们可以禁用默认world,并自己用代码实现。
使用宏定义禁用默认world:
    #UNITY_DISABLE_AUTOMATIC_SYSTEM_BOOTSTRAP_RUNTIME_WORLD 禁用runtime默认world
    #UNITY_DISABLE_AUTOMATIC_SYSTEM_BOOTSTRAP_EDITOR_WORLD  禁用editor态默认world
    #UNITY_DISABLE_AUTOMATIC_SYSTEM_BOOTSTRAP               同时禁用上述两个world
 
 
 

猜你喜欢

转载自www.cnblogs.com/sifenkesi/p/12286915.html
ECS
今日推荐