Unity3d ECS SystemBase Dependency

在当前系统OnUpdate调用前,Dependency是当前系统读取组件有写入的作业句柄和当前系统写入组件有读取的作业句柄,例如:当前系统读取A组件写入B组件, 在这个系统之前的任何系统中存在对A写入或者对B读取的作业,该作业的句柄会加入Dependency,当前系统作业执行前要等待Dependency里的作业全部完成

OnUpdate内部,Dependency会被隐式管理,顺序依赖,例如:

 protected override void OnUpdate()
   {
       Entities
           .WithName("ForEach_Job_One")
           .ForEach((ref AComponent c) =>
           {
               /*...*/
           })
           .ScheduleParallel();

       Entities
           .WithName("ForEach_Job_Two")
           .ForEach((ref AnotherComponent c) =>
           {
               /*...*/
           })
           .ScheduleParallel();

       Job
           .WithName("Job_Three")
           .WithCode(() =>
           {
               /*...*/
           })
           .Schedule();
   }

Job_Three依赖ForEach_Job_Two,ForEach_Job_Two依赖ForEach_Job_One

可以手动修改依赖关系,如:

protected override void OnUpdate()
   {
       JobHandle One = Entities
           .WithName("ForEach_Job_One")
           .ForEach((ref AComponent c) =>
           {
               /*...*/
           })
           .ScheduleParallel(this.Dependency);

       JobHandle Two = Entities
           .WithName("ForEach_Job_Two")
           .ForEach((ref AnotherComponent c) =>
           {
               /*...*/
           })
           .ScheduleParallel(this.Dependency);

       JobHandle intermediateDependencies =
           JobHandle.CombineDependencies(One, Two);

       JobHandle finalDependency = Job
           .WithName("Job_Three")
           .WithCode(() =>
           {
               /*...*/
           })
           .Schedule(intermediateDependencies);

       this.Dependency = finalDependency;
   }

ForEach_Job_One和ForEach_Job_Two 并行执行,Job_Three需要依赖前两个作业完成

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Unity ECS (Entity Component System) 是 Unity 的一种高性能游戏开发框架,它提供了一种基于数据驱动的编程模式,可以实现更高效、更灵活的游戏开发。 下面是使用 Unity ECS 框架生成可移动的 NPC 的代码示例: 1. 定义 NPC 实体的组件 ```csharp public struct NPCComponent : IComponentData { public float speed; // 移动速度 public float3 target; // 目标位置 } ``` 2. 创建 NPC 实体 ```csharp Entity npcEntity = entityManager.CreateEntity( typeof(NPCComponent), typeof(Translation), typeof(Rotation) ); // 初始化 NPC 组件 entityManager.SetComponentData(npcEntity, new NPCComponent { speed = 5f, // 设置移动速度 target = new float3(10f, 0f, 0f) // 设置目标位置 }); ``` 3. 更新 NPC 的位置 ```csharp public class MoveSystem : SystemBase { protected override void OnUpdate() { float deltaTime = Time.DeltaTime; Entities.ForEach((ref Translation translation, in NPCComponent npc) => { float3 direction = npc.target - translation.Value; float distance = math.distance(npc.target, translation.Value); if (distance > 0.1f) { // 移动 NPC float3 movement = math.normalize(direction) * npc.speed * deltaTime; translation.Value += movement; } }).Schedule(); } } ``` 4. 将 NPC 实体添加到系统中进行更新 ```csharp protected override void OnCreate() { // 获取实体管理器 EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager; // 创建 NPC 实体 Entity npcEntity = entityManager.CreateEntity( typeof(NPCComponent), typeof(Translation), typeof(Rotation) ); // 初始化 NPC 组件 entityManager.SetComponentData(npcEntity, new NPCComponent { speed = 5f, // 设置移动速度 target = new float3(10f, 0f, 0f) // 设置目标位置 }); // 将 MoveSystem 添加到更新系统列表中 World.DefaultGameObjectInjectionWorld.GetOrCreateSystem<MoveSystem>().AddJobHandleForProducer(Dependency); } ``` 以上就是使用 Unity ECS 框架生成可移动的 NPC 的代码示例。需要注意的是,在使用 ECS 框架时,需要遵循数据驱动的编程模式,将实体的状态和行为分离,并使用系统来更新实体的状态。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值