当前位置:   article > 正文

EntitiesSample_9. CrossQuery

EntitiesSample_9. CrossQuery

该示例主要的内容:

1.URPMaterialPropertyBaseColor:Unity.Rendering空间下的材质组件,该结构体拥有一个float4颜色的变量,赋值和其他的组件一样

2.EntityQuery转数组

boxQuery.ToComponentDataArray<LocalTransform>(Allocator.Temp);

3.IJobChunk的使用

  1. new CollisionJob
  2. {
  3. //获取LocalTransform组件的句柄
  4. LocalTransformTypeHandle = SystemAPI.GetComponentTypeHandle<LocalTransform>(true),
  5. //获取DefaultColor组件的句柄
  6. DefaultColorTypeHandle = SystemAPI.GetComponentTypeHandle<DefaultColor>(true),
  7. //获取URPMaterialPropertyBaseColor组件的句柄
  8. BaseColorTypeHandle = SystemAPI.GetComponentTypeHandle<URPMaterialPropertyBaseColor>(),
  9. //获取实体类型的句柄
  10. EntityTypeHandle = SystemAPI.GetEntityTypeHandle(),
  11. //实体查询对象转化为数组句柄
  12. OtherChunks = boxQuery.ToArchetypeChunkArray(state.WorldUpdateAllocator)
  13. //作业调度, Complete():需要全部完成,然后执行下一次
  14. }.ScheduleParallel(boxQuery, state.Dependency).Complete();

特别注意的是IJobChunk接口中的句柄变量,都需要 [ReadOnly]标签修饰,例如:

 [ReadOnly] public ComponentTypeHandle<LocalTransform> LocalTransformTypeHandle;

在Chunk作业中,需要使用ArchetypeChunk chunk获取当前要操作的变量值,示例代码是这样的没什么好解释的,就是双循环判断距离

  1. [BurstCompile]
  2. public struct CollisionJob : IJobChunk
  3. {
  4. [ReadOnly] public ComponentTypeHandle<LocalTransform> LocalTransformTypeHandle;
  5. [ReadOnly] public ComponentTypeHandle<DefaultColor> DefaultColorTypeHandle;
  6. public ComponentTypeHandle<URPMaterialPropertyBaseColor> BaseColorTypeHandle;
  7. [ReadOnly] public EntityTypeHandle EntityTypeHandle;
  8. [ReadOnly] public NativeArray<ArchetypeChunk> OtherChunks;
  9. public void Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask,
  10. in v128 chunkEnabledMask)
  11. {
  12. var transforms = chunk.GetNativeArray(ref LocalTransformTypeHandle);
  13. var defaultColors = chunk.GetNativeArray(ref DefaultColorTypeHandle);
  14. var baseColors = chunk.GetNativeArray(ref BaseColorTypeHandle);
  15. var entities = chunk.GetNativeArray(EntityTypeHandle);
  16. for (int i = 0; i < transforms.Length; i++)
  17. {
  18. var transform = transforms[i];
  19. var baseColor = baseColors[i];
  20. var entity = entities[i];
  21. // reset to default color
  22. baseColor.Value = defaultColors[i].Value;
  23. for (int j = 0; j < OtherChunks.Length; j++)
  24. {
  25. var otherChunk = OtherChunks[j];
  26. var otherTranslations = otherChunk.GetNativeArray(ref LocalTransformTypeHandle);
  27. var otherEntities = otherChunk.GetNativeArray(EntityTypeHandle);
  28. for (int k = 0; k < otherChunk.Count; k++)
  29. {
  30. var otherTranslation = otherTranslations[k];
  31. var otherEntity = otherEntities[k];
  32. if (entity != otherEntity && math.distancesq(transform.Position, otherTranslation.Position) < 1)
  33. {
  34. baseColor.Value.y = 0.5f; // set green channel
  35. break;
  36. }
  37. }
  38. }
  39. baseColors[i] = baseColor;
  40. }
  41. }
  42. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/702158
推荐阅读
相关标签
  

闽ICP备14008679号