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

这个代码就实现了批量处理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)
  • 12
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值