Unity DOTS中的share component

share component是DOTS中一类比较特殊的component,顾名思义,它是全局共享的component,所有具有相同component值的entity,共享一个component,这就避免了数据的冗余。但是相应地,如果修改了某个entity上的share component值,则会触发structural change,Unity不得不将entity挪到使用新值的chunk中去。这就是说,对entity上share component值的修改,并非是真的修改了它的值,而是触发了entity的重新分组。在简要了解过它的利弊之后,我们来看一下Unity内部是如何管理share component的。

内存管理

Archetype不直接保存share component的值,而是保存了一个index数组,这些index用来真正索引share component值。虽然不同share component值的entity位于不同的chunk,但是这些chunk都属于同一个Archetype。一个Archetype可能包含多个share component的值,但每个chunk的share component值只有一个。

在这里插入图片描述

share component index在数组中的排序规则如上图所示,首先按不同的type进行排序,同一type再按chunk的index进行排序。share component index只保存设置过值的share component,意味着如果我们只是简单地调用AddComponent,而没有显式SetSharedComponent,压根就不会新增share component index。这是因为此时share component的值为默认值,并不需要显式保存。

share component真正的值保存在一个全局的m_UnmanagedSharedComponentsByType变量中。它可以认为是一个list数组,不同的component type对应不同的数组下标,share component index则映射到某个list的下标。当然,每个list的长度不一定相同。

在这里插入图片描述

在Unity Archetypes窗口中,可以看到share component是单独显示的:

在这里插入图片描述

创建流程

向Entity添加一个share component的流程和普通component类似,第一步也是去寻找满足相同type的Archetype,然后再去找是否有可复用的chunk存在。只不过这里判断chunk可复用的条件和普通component不同,普通component只需要满足chunk有空闲的slots即可,而share component还需要chunk所对应的share component value也要一致才行,如果找不到则会新建一个。

在有了chunk之后,就和普通component一样,需要把entity从之前的chunk中move到新的chunk中去。

public bool AddComponent(Entity entity, ComponentType type)
{
    var dstChunk = GetChunkWithEmptySlotsWithAddedComponent(entity, type);
    if (dstChunk == ChunkIndex.Null)
        return false;

    Move(entity, dstChunk);
    return true;
}

修改流程

当真正调用SetSharedComponent为share component设置值时,share component才会真正地存储下来。Unity内部为了在m_UnmanagedSharedComponentsByType中快速找到下一个空闲的位置,还使用了一个辅助数据结构m_UnmanagedSharedComponentInfo,它也是一个list数组,每个list的头部记录了list中下一个空闲的位置,所有空闲的位置以链表的形式串联在一起,如果不存在,则需要对list进行扩容。list中空闲的位置就是对应share component的index。

在这里插入图片描述

index更新之后,此时还需要把entity从原来index的chunk挪到新的index的chunk去,但Archetype不会改变,Archetype只和类型的数量有关,而每个chunk都有一个对应的share component index。

public void SetSharedComponentData_Unmanaged(
    Entity entity,
    TypeIndex typeIndex,
    void* componentData,
    void* componentDataDefaultValue,
    in SystemHandle originSystem = default)
{
    var componentType = ComponentType.FromTypeIndex(typeIndex);
    var newSharedComponentDataIndex = InsertSharedComponent_Unmanaged(typeIndex, 0, componentData, componentDataDefaultValue);
    EntityComponentStore->SetSharedComponentDataIndex(entity, componentType, newSharedComponentDataIndex);
}

销毁流程

remove share component的流程和普通component完全相同,此时的entity因为少了一个component,不再属于原来的Archetype,需要迁移到新的Archetype中去。如果此时的entity身上还有share component,那依旧按照之前寻找chunk的规则处理。

public bool RemoveComponent(Entity entity, ComponentType type)
{
    var dstChunk = GetChunkWithEmptySlotsWithRemovedComponent(entity, type);
    if (dstChunk == ChunkIndex.Null)
        return false;

    Move(entity, dstChunk);
    return true;
}

Reference

[1] Introducing shared components

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值