返回

ECS各种类型变种

Entity

Entity在ECS中实际上就是指ID,暂无变种,但创建方式较多,下面列出创建方式

创建单个Entity

    //传入若干ComponentType,本质上通过传入的组件类型,然后创建原型块,再将Entity归纳到该块中
    public Entity CreateEntity(params ComponentType[] types);
    //指定原型块
    public Entity CreateEntity(EntityArchetype archetype);
    // 克隆一个已有的entity
    public Entity Instantiate(Entity srcEntity); 
    // 创建一个空的entity,然后再添加components
    public Entity CreateEntity(); 

创建多个Entity

    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);

Component

类型 说明
IComponentData 常用组件,或者chunk components
IBufferElementData 用于将dynamic buffer和entity关联DynamicBuffer
ISharedComponentData 共享组件,用于按archetype中的值来对entity分类或分组
ISystemStateComponentData 用于将system-specific state与entity关联用于检测单个entity的创建或销毁
ISharedSystemStateComponentData shared和system state的组合
Blob assets 不算是component,但可以用来存储data。Blob assets可以被一个或多个component通过BlobAssetReference使用,并且是immutable的。Blob assets允许你在assets之间共享data,并在c# job中访问这个数据。

System

生命周期回调函数,在main thread调用:

回调 说明
OnCreate system创建
OnStartRunning 在第一个OnUpdate调用之前在每次resumes running之时
OnUpdate 每帧调用,只要system是enabled的,且有事可做
OnStopRunning 当query找不到entities时,会停止调用OnUpdate,这个时候会触发该回调
OnDestroy system销毁

System类型:

System类型 说明
Component Systems 工作在main thread
Job Component Systems 在OnUpdate中调用IJobForEach或IJobChunk这些Job工作在worker threads
Entity Command Buffer Systems thread-safe的command buffer用来缓存一些entities和components的操作
Component System Groups 用于提供systems的嵌套管理和执行顺序管理

NativeContainer 本地容器

NativeContainer实际上是native memory的一个wrapper,包含一个指向非托管内存的指针。

**不需要拷贝:**使用NativeContainer可以让一个job和main thread共享数据,而不用拷贝。(copy虽然能保证Safety System,但每个job的计算结果也是分开的)。

数据结构 说明 来源
NativeArray 数组 Unity
NativeSlice 可以访问一个NativeArray的某一部分 Unity
NativeList 一个可变长的NativeArray ECS
NativeHashMap key value pairs ECS
NativeMultiHashMap 一个key对应多个values ECS
NativeQueue FIFO的queue ECS

NativeContainer Allocator分配器:

(1)Allocator.Temp

最快,维持1 frame,job不能用,需要手动Dispose(),比如可以在native层的callback调用时使用。

(2)Allocator.TempJbo

稍微慢一点,最多维持4 frames,thread-safe,如果4 frames内没有Dispose(),会有warning。大多数small jobs都会使用这个类型的分配器.

(3)Allocator.Persistent

最慢,但是可持久存在,就是malloc的wrapper。Longer jobs使用这个类型,但在性能敏感的地方不应该使用。

NativeArray<float> result = new NativeArray<float>(1, Allocator.TempJob);
Licensed under CC BY-NC-SA 4.0
0