• Unity DOTS 1.0 (5) Baking System、Baking phases 和 Baking World


    Baking System

    • 一个 baking system在处理数据的方式上不同于baker。和baker一个一个处理components不同,它是进行批处理的,并且它可以经由job 和burst 大幅提升处理性能。
    • baking system是在entity创建完成后运行,所以它可以访问到所有初始创建的Entity,包括baker创建的。
    • baking system可以随意改变world,包括创建新的entity。但是在system中创建的entity,不会在场景Bake结束以后销毁;也就是在这个阶段创建的Entity是可以留在我们的世界里面的。
      你也可以来创建entity在多个Baking System之间进行数据传递,如果你希望这个entity在你这个bake场景结束以后有销毁你创建的entity,那么你创建entity就必须要在Baker里面创建,在Baker里面创建的entity(使用CreateAdditionalEntity方法来创建),就会在Baker结束以后销毁

    创建一个Baking System

    • 给System做一个特定注解:[WorldSystemFilter(WorldSystemFilterFlags.BakingSystem)] attribute.有了这个注解,Baking 系统就会识别这个System,把它当作Baking System加入到的Baking World里面。

    示例代码:给出了代码实现,给所有的有带有组件A的entity加一个tag component,如果一个entity没有组件A,但是又带了tag component,我们就把这个tag component从entity里面删除掉;

    public struct AnotherTag : IComponentData { }
    
            [WorldSystemFilter(WorldSystemFilterFlags.BakingSystem)]
            partial struct AddTagToRotationBakingSystem : ISystem
            {
                public void OnUpdate(ref SystemState state)
                {
                //查找出所有拥有RotationSpeed Component并且没有AnotherTagComponent的Entity
                    var queryMissingTag = SystemAPI.QueryBuilder()
                        .WithAll<RotationSpeed>()
                        .WithNone<AnotherTag>()
                        .Build();
    				//然后批量添加AnotherTag
                    state.EntityManager.AddComponent<AnotherTag>(queryMissingTag);
    
                    // Omitting the second part of this function would lead to inconsistent
                    // results during live baking. Added tags would remain on the entity even
                    // after removing the RotationSpeed component.
    
                    var queryCleanupTag = SystemAPI.QueryBuilder()
                        .WithAll<AnotherTag>()
                        .WithNone<RotationSpeed>()
                        .Build();
    				//然后批量删除AnotherTag
                    state.EntityManager.RemoveComponent<AnotherTag>(queryCleanupTag);
                }
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    这个代码就实现了批量处理Entity。

    Baking phases (阶段)

    0. 执行PreBakingSystemGroup 分组内容

    1. Entity 创建:

    • Unity会先创建出来场景里面的每个GameObject对应的entity, entity只是一个空的容器(internal metadata),没有ComponentData;

    2. Baker phase:

    • 对Authoring GameObject做单个单个的转换,转换成ecs entity + component;执行我们每个数据的Bakers,来完成转换;同一种类型的Component转换的时候,使用同一种baker;EntityA, EntityB,但是都有Authoring Component使用同一个Baker;
    • Unity提供的组件数据都是使用系统自带默认的Baker来进行转换;
    • Unity执行Bakers的顺序是不固定的,所以Baker之间的相互依赖是不被允许的。不能在baker A Entity的时候去读取或更改B Entity的Component
    • 只能进行添加组件操作

    3. Baking System phase:

    • 所有Baker执行完了后,进入Baking System阶段
    • 对所有的entity 与component来做批量的数据处理,提升性能。
    • 所有的Baking System只能在Baking过程中运行;可以通过注解[UpdateAfter, UpdateBefore, and UpdatelnGroup ]来标记执行顺序;
    • Unity 已经提供了一些默认的分组,按照这样的特定顺序;
      • PreBakingSystemGroup:在Baker之前来调用它分组下面的所有的system(这个组在Baker之前,是个例外);
      • TransformBakingSystemGroup:就在Baker之后,这个分组,可以用来做一些特定的数据转换。
      • BakingSystemGroup: 用户一般编写的所有的Baking System都会被放入到这个分组下。
      • PostBakingSystemGroup:你可以编写Baking System来做结束时候的一些处理。

    Baking World

    • 我们每个Authroing Scene在Bake的时候都是独立分来来处理的;
    • 后台每次一个一个场景的来进行Bake处理;
    • 每个subscene,都会有2个World: Conversion world, Shadow worldConversion world。所有的Baker 与Baking System都是运行在它里面。
    • Shadow world:保存了上一次Baking的处理结果,可以通过比较看本次修改了哪些地方;
    • baking完成后,比较两次世界的变化,然后同步到main word(用户可见world)
  • 相关阅读:
    机械臂B样条插补+带源代码
    java版工程管理系统Spring Cloud+Spring Boot+Mybatis实现工程管理系统源码
    融云「百幄」之数字人,升级交互体验的「新同事」
    【ASP.NET Core】MVC控制器的各种自定义:应用程序约定的接口与模型
    自定义注解+切面,环绕通知打印方法日志
    一键批量剪辑:视频随机分割新玩法,高效剪辑不再难
    mysql 主从配置
    luffy-(12)
    APS车间排产软件在汽配行业的应用
    AI智能量化之qlib的回测子框架代码解读
  • 原文地址:https://blog.csdn.net/weixin_38027841/article/details/137410990