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]);
}
}
}
}
}
}