Unity JobSystem Job的写法

由于可能会使用多线程,需要定义字段存储数据,然后统一执行 Excute() 进行运算。
另外Job可继承自多种接口,各接口参数不同,需要自定义字段用以实现。

1. IJobForeach<T1,T2,T3,…>

简单地绑定ComponentData类型

 public struct Job : IJobForEach<SpriteSheetAnimation_Data, Translation> 
 {
 	//任何可能的数据
 	 public float DeltaTime;
 	 public void Execute(ref SpriteSheetAnimation_Data spriteSheetAnimationData, ref Translation translation)
 	 {
 	 }
 }

{
	job.Schedule(ComponentSystemBase systemBase, JobHandle handle)
}

2. IJobEntityBatch

合批处理,适用于大量/分批操作数据

struct RotationSpeedJob : IJobEntityBatch
    {
        public float DeltaTime;
        public ComponentTypeHandle<Rotation> RotationTypeHandle;
        [ReadOnly] public ComponentTypeHandle<RotationSpeed_IJobEntityBatch> RotationSpeedTypeHandle;
         public void Execute(ArchetypeChunk batchInChunk, int batchIndex)
        {
            var chunkRotations = batchInChunk.GetNativeArray(RotationTypeHandle);
            var chunkRotationSpeeds = batchInChunk.GetNativeArray(RotationSpeedTypeHandle);
            for (var i = 0; i < batchInChunk.Count; i++)
            {
                var rotation = chunkRotations[i];
                var rotationSpeed = chunkRotationSpeeds[i];

                // Rotate something about its up vector at the speed given by RotationSpeed_IJobChunk.
                chunkRotations[i] = new Rotation
                {
                    Value = math.mul(math.normalize(rotation.Value),
                        quaternion.AxisAngle(math.up(), rotationSpeed.RadiansPerSecond * DeltaTime))
                };
            }
        }
    }
{
	job.ScheduleParallel(EntiyQuery query, int batchCountPerChunk, JobHandle handle);
}

3. IJobChunk

主线程的OnUpdate也可以进行Burst加速

 struct RotationSpeedJob : IJobChunk
    {
        public float DeltaTime;
        public ComponentTypeHandle<Rotation> RotationTypeHandle;
        [ReadOnly] public ComponentTypeHandle<RotationSpeed_IJobChunkStructBased> RotationSpeedTypeHandle;

        public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex)
        {
            var chunkRotations = chunk.GetNativeArray(RotationTypeHandle);
            var chunkRotationSpeeds = chunk.GetNativeArray(RotationSpeedTypeHandle);
            for (var i = 0; i < chunk.Count; i++)
            {
                var rotation = chunkRotations[i];
                var rotationSpeed = chunkRotationSpeeds[i];

                // Rotate something about its up vector at the speed given by RotationSpeed_IJobChunkStructBased.
                chunkRotations[i] = new Rotation
                {
                    Value = math.mul(math.normalize(rotation.Value),
                        quaternion.AxisAngle(math.up(), rotationSpeed.RadiansPerSecond * DeltaTime))
                };
            }
        }
    }
{
	job.ScheduleSingle(EntityQuery query,JobHandle handle)
}

4. IJobParalleFor

大量数据,多线程并行,可细分接口类型

public struct ReallyToughParalleJob : IJobParallelFor
{
	//需要数组字段存储数据,用以运算
    public void Execute(int index){}
}

job.Schedule(int arrayLength,int batchCount);

细分接口如IJobParallelForTransform

public struct ReallyToughParalleJobTransforms : IJobParallelForTransform
{
    public void Execute(int index, TransformAccess transform) {}
}
job.Schedule(TransformAccesseArray array);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值