Unity ECS学习(9)

现在让我们回来看一下IJobEntity

IJobEntity使用

首先要先声明一个JobEntity
声明需要继承IJobEntity接口,需要是stuct结构体
最后需要实现 Execute方法

public partial struct  MoveChangeJobEntity : IJobEntity
{
    public void Execute(ref MoveComponent moveComponent)
    {
        if (moveComponent.Speed>10)
        {
            moveComponent.Speed = -moveComponent.Speed;
        }
        else
        {
            moveComponent.Speed += 0.1f;
        }
    }
}

然后是使用 直接new后调用 ScheduleParallel接口就可以了

    protected override void OnUpdate()
    {
        new MoveChangeJobEntity().ScheduleParallel();
    }

这里有一个问题 我们并没有调用 Execute,也就谈不上传参,这个参数是怎么来的???

IJobEntity查询

首先我们定义一个EntityQuery
然后给他赋值
赋值后再new JobEntity的时候把EntityQuery当作参数传入

    EntityQuery query_boidtarget;
     protected override void OnCreate()
    {
        query_boidtarget = GetEntityQuery(ComponentType.ReadWrite<MoveComponent>());
        // Query that contains all of Execute params found in `QueryJob` - as well as 
    }
        protected override void OnUpdate()
    {
        // Uses the BoidTarget query
        new MoveChangeJobEntity().ScheduleParallel(query_boidtarget);
    }

这样就相当于给JobEmtity传入了一个集合,让它再这个集合里面去走它的逻辑

他还有一个特殊的筛选方法
通过在结构上设置进行筛选

[WithAll(typeof(MoveComponent))]
[WithNone(typeof(Banana))]
public partial struct  MoveChangeJobEntity2 : IJobEntity
  • Unity.Entities.WithAll(params Type[]) 在作业结构上设置。缩小查询范围,以便实体必须与提供的所有类型匹配。
  • Unity.Entities.WithAny(params Type[]) 在作业结构上设置。缩小查询范围,以便实体必须匹配提供的任何类型。
  • Unity.Entities.WithNone(params Type[]) 在作业结构上设置。缩小查询范围,以便实体必须与提供的任何类型都不匹配。
  • Unity.Entities.WithChangeFilter(params Type[]) 在作业结构上设置或附加到 中的参数。缩小查询范围,以便实体必须在给定组件的原型块中发生更改。Execute
  • Unity.Entities.WithOptions(params EntityQueryOptions[]) 在作业结构上设置。更改查询的范围以使用所述的 EntityQueryOptions。
  • Unity.Entities.EntityIndexInQuery 设置参数 in 以获取当前实体迭代的当前索引。这与Entities.ForEach中相同。

共有这些筛选方法
由于类似于Job,因此还可以使用在Job的所有属性

  • Unity.Burst.BurstCompile
  • Unity.Collections.DeallocateOnJobCompletion
  • Unity.Collections.NativeDisableParallelForRestriction
  • Unity.Burst.BurstDiscard
  • Unity.Collections.LowLevel.Unsafe.NativeSetThreadIndex
  • Unity.Collections.NativeDisableParallelForRestriction
  • Unity.Burst.NoAlias

Execute参数

  • IComponentData 可以标记为ref(读写)或者in(只读)
  • ICleanupComponentData 可以标记为ref(读写)或者in(只读)
  • ISharedComponent 只读,并且如果有该参数 无法使用 Burst,只能在主线程执行(Run())(这个我们后面在研究下这里不展开)
  • Managed components 使用值复制进行读写访问,或使用in进行托管组件的只读访问,并且无法进行突发编译,只能主线程执行
  • Entity 一个Id的复制,因此,不要用 ref 和 in 进行修饰
  • DynamicBuffer 获取DynamicBuffer 可以标记为ref(读写)或者in(只读)
  • IAspect 无法直接使用,但是可以通过值复制进行修改 可以标记为ref(读写)或者in(只读)
  • int 支持3种整数
    [Unity.Entities.ChunkIndexInQuery]标记以获取查询中的当前原型块索引
    [Unity.Entities.EntityIndexInChunk] 获取当前原型块中的当前实体索引。您可以添加 和 获取每个实体的唯一标识符
    [Unity.Entities.EntityIndexInQuery] 获取查询的打包索引。此参数在内部使用,这会对性能产生负面影响。

JobEntity大概就这些内容了

  • 28
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值