Unity Entity Component System --- Entity Command Buffer

本文探讨了Unity DOTS系统中EntityCommandBuffer的重要作用,它解决了在Job中无法访问EntityManager的问题,并确保了组件修改的安全性和确定性执行。通过实例展示了如何在ComponentSystem的子类中使用EntityCommandBuffer来创建实体并设置组件,以及如何在并行Jobs中安全地执行EntityManager命令。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

EntityCommandBuffer解决了2个问题:

当你再Job中时,不可以访问EntityManager
当访问EntityManager时(比如,创建一个Entity)导致导致访问的Components数组失效,及EntityQuery对象失效。

Entity Command Buffer抽象允许我们将改变保存到队列(从Job或者主线程),然后在稍后在主线程中应用。有2中应用EntityCommandBuffer的方法:

ComponentSystem的子类PostUpdateCommands可以在主线程中执行。可以简单地调用方法来将改变队列化,当world执行完该系统的Update后,会立即应用它们。

例子:

PostUpdateCommands.CreateEntity(TwoStickBootstrap.BasicEnemyArchetype);
PostUpdateCommands.SetComponent(new Position2D{Value= spawnPosition});
PostUpdateCommands.SetComponent(new Heading2D{Value = new float2(0.0f,-10f)});
PostUpdateCommands.SetComponent(default(Enemy));
PostUpdateCommands.SetComponent(new Helath{Value=TwoStickBootstrap.Settings.enemyInitialHealth});
PostUpdateCommands.SetComponent(new EnemyShootState{Cooldown=0.5f});
PostUpdateCommands.SetComponent(new MoveSpeed(speed=TwoStickBootstrap.Setting.enemySpeed));
PostUpdateCommands.AddSharedComponent(TwoStickBootstrap.EnemyLook);

对Jobs来说,必须在主线程中从entity command buffer system请求EntityCommandBuffer,然后传递给jobs。当EntityCommandBufferSystem更新时,command buffer会在主线程中按照创建的顺序执行它们。

Entity Command Buffer Systems

默认World初始化时创建了3个system groups,用来初始化,模拟,显示,它们在每一帧顺序执行。在每个group中,有一个entity command buffer system,在groups内所有其它system执行前执行一次,并且在所有其它system执行完后再执行一次。我们建议使用已有的command buffer systems,而不是自己创建,这样可以最小化同步点数量(Command buffer system越多,Sync Points越多)。

Using EntityCommandBuffers from ParallelFor jobs

当在ParallelFor jobs中使用EntityCommandBuffer来执行EntityManager命令时,EntityCommandBuffer.Concurrent接口可以保证线程安全和执行的确定性。该方法需要一个额外的jobIndex作为参数,用来保证命令回放执行的顺序是确定的。JobIndex必须是每个job唯一的。为了提升效率,jobIndex应该是传递给IJobParallelFor.Execute()的自增的index值。除非你帧地了解它们的顺序关系,否则用Execute()的index 参数是最佳选择。使用其它的jobIndex即使正确执行了,也可能带来效率方面的其它问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值