Unity DOTS中的share component

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中去。

c# 复制代码
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。

c# 复制代码
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的规则处理。

c# 复制代码
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](https://docs.unity3d.com/Packages/com.unity.entities@1.3/manual/components-shared-introducing.html)

相关推荐
南無忘码至尊2 小时前
Unity学习90天-第2天-认识Unity生命周期函数并用 Update 控制物体移动,FixedUpdate 控制物理
学习·unity·游戏引擎
小林up6 小时前
【Unity URP源码阅读1】ColorGradingLUT
unity·游戏引擎
风酥糖9 小时前
Godot游戏练习01-第27节-升级选项选择生效
游戏·游戏引擎·godot
郝学胜-神的一滴9 小时前
[简化版 GAMES 101] 计算机图形学 04:二维变换上
c++·算法·unity·godot·图形渲染·unreal engine·cesium
南無忘码至尊10 小时前
Unity学习90天-第2天-认识键盘 / 鼠标输入(PC)并实现WASD 移动,鼠标控制物体转向
学习·unity·c#·游戏开发
星夜泊客10 小时前
unity 海底海洋资源OceanEnviromentPackUrp材质丢失修正
unity·游戏引擎·材质
weixin_4242946711 小时前
Unity 的Button Animator
unity·游戏引擎
UQ_rookie12 小时前
【Unity3D】在URP渲染管线下使用liltoon插件出现粉色无法渲染情况的解决方案
unity·游戏引擎·shader·urp·着色器·vrchat·liltoon
aqiu~1 天前
VSCode编辑器用于Unity项目
vscode·unity
小贺儿开发1 天前
Unity3D 心理沙盘互动演示
unity·ai·pdf·人机交互·工具·互动·心理沙盘