EntitiesSample_9. CrossQuery

该示例主要的内容:

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

2.EntityQuery转数组

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

3.IJobChunk的使用

复制代码
  new CollisionJob
            {
                //获取LocalTransform组件的句柄
                LocalTransformTypeHandle = SystemAPI.GetComponentTypeHandle<LocalTransform>(true),
                //获取DefaultColor组件的句柄
                DefaultColorTypeHandle = SystemAPI.GetComponentTypeHandle<DefaultColor>(true),
                //获取URPMaterialPropertyBaseColor组件的句柄
                BaseColorTypeHandle = SystemAPI.GetComponentTypeHandle<URPMaterialPropertyBaseColor>(),
                //获取实体类型的句柄
                EntityTypeHandle = SystemAPI.GetEntityTypeHandle(),
                //实体查询对象转化为数组句柄
                OtherChunks = boxQuery.ToArchetypeChunkArray(state.WorldUpdateAllocator)
                //作业调度,   Complete():需要全部完成,然后执行下一次 
            }.ScheduleParallel(boxQuery, state.Dependency).Complete();

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

ReadOnly public ComponentTypeHandle<LocalTransform> LocalTransformTypeHandle;

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

复制代码
[BurstCompile]
    public struct CollisionJob : IJobChunk
    {
        [ReadOnly] public ComponentTypeHandle<LocalTransform> LocalTransformTypeHandle;
        [ReadOnly] public ComponentTypeHandle<DefaultColor> DefaultColorTypeHandle;
        public ComponentTypeHandle<URPMaterialPropertyBaseColor> BaseColorTypeHandle;
        [ReadOnly] public EntityTypeHandle EntityTypeHandle;

        [ReadOnly] public NativeArray<ArchetypeChunk> OtherChunks;

        public void Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask,
            in v128 chunkEnabledMask)
        {
            var transforms = chunk.GetNativeArray(ref LocalTransformTypeHandle);
            var defaultColors = chunk.GetNativeArray(ref DefaultColorTypeHandle);
            var baseColors = chunk.GetNativeArray(ref BaseColorTypeHandle);
            var entities = chunk.GetNativeArray(EntityTypeHandle);

            for (int i = 0; i < transforms.Length; i++)
            {
                var transform = transforms[i];
                var baseColor = baseColors[i];
                var entity = entities[i];
                // reset to default color
                baseColor.Value = defaultColors[i].Value;
                for (int j = 0; j < OtherChunks.Length; j++)
                {
                    var otherChunk = OtherChunks[j];
                    var otherTranslations = otherChunk.GetNativeArray(ref LocalTransformTypeHandle);
                    var otherEntities = otherChunk.GetNativeArray(EntityTypeHandle);

                    for (int k = 0; k < otherChunk.Count; k++)
                    {
                        var otherTranslation = otherTranslations[k];
                        var otherEntity = otherEntities[k];

                        if (entity != otherEntity && math.distancesq(transform.Position, otherTranslation.Position) < 1)
                        {
                            baseColor.Value.y = 0.5f; // set green channel
                            break;
                        }
                    }
                }
                baseColors[i] = baseColor;
            }
        }
    }
相关推荐
元气少女小圆丶13 小时前
SenseGlove Nova 2+Unity开发笔记2
笔记·unity·游戏引擎
想不明白的过度思考者18 小时前
Unity学习笔记——虚拟摇杆实现笔记(事件触发器的使用、UGUI 坐标转换)
笔记·学习·unity
魔士于安18 小时前
unity volumefog带各种demo第一人称 wsad 穿墙控制
游戏·unity·游戏引擎·贴图·模型
魔士于安21 小时前
红色文化馆技术文档
前端·unity·游戏引擎·贴图·模型
LONGZETECH21 小时前
Unity 3D工业级教育软件实战:200+无人机装调任务的碰撞检测与交互落地
3d·unity·架构·游戏引擎·无人机·交互·cocos2d
淡海水21 小时前
08-认知篇-对比-injectfix深度解析
unity·c#·热更新·clr·hybrid·injectfix
迪普阳光开朗很健康21 小时前
BepInEx 插件开发文档中文翻译 - 项目分享
unity·bepinex
元气少女小圆丶1 天前
SenseGlove Nova 2+Unity开发笔记1
笔记·学习·unity
mxwin1 天前
Unity URP下新技术MSSPT 取代SSR和光线追踪
unity·游戏引擎·shader
LF男男2 天前
IBuilder.cs 接口
unity