如何在 ECS 中引用本机 Arrrray 中的数组项<Entity>



我正在尝试执行以下操作:

private void AssignPlayer()
{
EntityQuery playerQuery = GetEntityQuery(ComponentType.ReadOnly<PlayerTag>());
Entity playerEntity = playerQuery.ToEntityArray(Allocator.Temp);
Entities.
WithAll<ChaserTag>().
ForEach((ref TargetData targetData) =>
{
if (playerEntity != Entity.Null)
{
targetData.targetEntity = playerEntity;
}
}).Schedule();
}

但是分配targetData.targetEntity = playerEntity;的行会抛出错误:

无法将类型"Unity.Collections.NativeArray"隐式转换为"Unity.Entities.Entity">

if( playerQuery.CalculateEntityCount()!=0 )
{
NativeArray<Entity> players = playerQuery.ToEntityArray( Allocator.TempJob );
Entities.WithName("targetData_update_job")
.WithReadOnly( players ).WithDeallocateOnJobCompletion( players )
.WithAll<ChaserTag>()
.ForEach( ( ref TargetData targetData ) =>
{
targetData.targetEntity = players[0];
}
).ScheduleParallel();
}
else Debug.LogWarning($"{nameof(AssignPlayer)}(): no players tho");

最新更新