一、基本概念
1、需要继承IJobEntity的前面加上partial关键字
partial struct RotateAndScaleJob : IJobEntity
2、实现IJobEntity接口的Struct类型,都需要实现Execute方法,否则会有异常提示
3、Execute可以携带不定参数;也可以不携带参数;当然这里如果不携带参数的话,那么也就没有意义了;
4、在IJobEntity的内部结构中,会根据Execute中的参数创建一个EntityQuery; 如果查找到对应条件的所有ntities;那么就会执行;这里的的Execute就是单个entity的执行;也就是for循环中的一环。
5、如果没有满足EntityQuery的Entity,那么是不会执行Execute的
二、代码解析
[BurstCompile]
partial struct RotateAndScaleJob : IJobEntity
{
public float deltaTime;
public float elapsedTime;
void Execute(ref LocalTransform transform, ref PostTransformMatrix postTransform,
in RotationSpeed speed)
{
}
}
解析:Execute
这里会存在一个EntityQuery,通过EntityQuery查找出同时具有LocalTransform、PostTransformMatrix、RotationSpeed组件的所有Entities;如果有匹配的数据,那么就会执行Execute
注意问题:
1、如果再Execute中查找的组件为Null,那么就不能用ref来修饰Execute的参数;
例如:
[BurstCompile]
partial struct RotateAndScaleJob : IJobEntity
{
public float deltaTime;
public float elapsedTime;
void Execute(ref LocalTransform transform, ref PostTransformMatrix postTransform,
ref TempComponentData tempComponentData, in RotationSpeed speed)
{
}
}
struct TempComponentData : IComponentData
{
}
在上面的代码中定义了TempComponentData组件,在Exectue中使用了ref TempComponentData tempComponentData来修饰参数,没有找到该组件,会有如下报错:
所以这个地方使用的时候要注意;
2、如果在查询条件中,存在ShareComponent、ManagedComponent组件,那么该Job将会在主线程上运行;原因是因为这种情况会生成EntityManager,导致访问主线程的数据;