Unity DOTS job system burst笔记

1、创建一个job, [BurstCompile]加上此属性代表使用Burst编译,Execute方法在构建job时执行。
2、

EntityQuery ObjGroup;
 
    protected override void OnCreate()
    {
        ObjGroup = GetEntityQuery(typeof(Translation), typeof(NavAgentComponent), typeof(Rotation));
    }

上面所示代码是获取包含所示组件的实体,构成一个组
3、JobHandle jobHandle = jobA.Schedule(PersonGrounp);
jobHandle.Complete();
异步执行操作

4、[DeallocateOnJobCompletion]加上此属性,数组在运行完成时自动释放
5、 var translationType = GetArchetypeChunkComponentType(false);此处的false表示是否为可读
6、CommandBuffer.DestroyEntity(chunkIndex, entities[i]);此代码为销毁实体
7、var input = new RaycastInput { Start = start, End = end, Filter = CollisionFilter.Default };
if (world.CastRay(input, out var hit))
{
}
此代码为射线检测

using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Transforms;

public class PersonMoveSystem : JobComponentSystem
{
    EntityQuery ObjGroup;
	 private EndSimulationEntityCommandBufferSystem endSimCommandBufferSystem;
    protected override void OnCreate()
    {
        ObjGroup = GetEntityQuery(typeof(Translation), typeof(NavAgentComponent), typeof(Rotation));
        endSimCommandBufferSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
    }

    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        var translationType = GetArchetypeChunkComponentType<Translation>(false);
		ref PhysicsWorld world = ref World.DefaultGameObjectInjectionWorld.GetExistingSystem<BuildPhysicsWorld>().PhysicsWorld;
        var jobA = new RunJob
        {
            translationType = translationType,
            Navigas = ObjGroup.ToComponentDataArray<NavAgentComponent>(Allocator.TempJob),
            entityType = GetArchetypeChunkEntityType(),
            CommandBuffer = endSimCommandBufferSystem.CreateCommandBuffer().ToConcurrent(),
            world = world,
        };

        JobHandle jobHandle = jobA.Schedule(PersonGrounp);
        jobHandle.Complete();
        
        return default;
    }


    [BurstCompile]
    struct RunJob : IJobChunk
    {
        public ArchetypeChunkComponentType<Translation> translationType;
        [DeallocateOnJobCompletion]
        [ReadOnly] public NativeArray<NavAgentComponent> Navigas;
        [ReadOnly] public ArchetypeChunkEntityType entityType;
		public EntityCommandBuffer.Concurrent CommandBuffer;
		public PhysicsWorld world;
		
        public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex)
        {
            var trans = chunk.GetNativeArray(translationType);
             var entities = chunk.GetNativeArray(entityType);

			var input = new RaycastInput { Start = start, End = end, Filter = CollisionFilter.Default };
			if (world.CastRay(input, out var hit))
            {
             }

            for (int i = 0; i < Persondatas.Length; i++)
            {
                Translation tra = trans[i];
                for (int j = 0; j < Navigas.Length; j++)
                {
                    if (Persondatas[i].ID == Navigas[j].entityID)
                    {
                        tra.Value = Navigas[j].m_Position;
                        trans[i] = tra;
                    }else
                    {
                    CommandBuffer.DestroyEntity(chunkIndex, entities[i]);
                    }
                }
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值