unity3d - How do you reference an array item within nativeArrray in ECS
问题描述
I'm trying to do the following:
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();
}
But the line that assigns targetData.targetEntity = playerEntity;
throws the error:
Cannot implicitly convert type 'Unity.Collections.NativeArray<Unity.Entities.Entity>' to 'Unity.Entities.Entity'
解决方案
if( players.Length!=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");
推荐阅读
- python - 从 NumPy 中的索引列表中索引多维数组
- mysql - 将数据从 MySQL 数据库部分加载到 Tableau
- excel - 如何合并行而不丢失excel中的数据
- vba - WinHttp.WinHttpRequest.5.1 URL 编码
- linux-kernel - 为什么 sys_futex 上的 kretprobe 调用频率低于相应的 kprobe?
- node.js - Puppeteer 无法识别链接
- python - 使用 super() 进行直接多重继承时保持可读性
- java - Android 我是否使用多个 ClassLoader/PathClassLoader?
- json - 请求参数 RefreshType 格式不正确
- java - 如果直接使用传递依赖,gradle build 会失败?