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\ LocalTransformTypeHandle; 在Chunk作业中,需要使用ArchetypeChunk chunk获取当前要操作的变量值,示例代码是这样的没什么好解释的,就是双循环判断距离 [BurstCompile] public struct CollisionJob : IJobChunk { [ReadOnly] public ComponentTypeHandle LocalTransformTypeHandle; [ReadOnly] public ComponentTypeHandle DefaultColorTypeHandle; public ComponentTypeHandle BaseColorTypeHandle; [ReadOnly] public EntityTypeHandle EntityTypeHandle; [ReadOnly] public NativeArray 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; } } }

相关推荐
ttod_qzstudio11 小时前
Unity中使用EzySlice实现模型切割与UV控制完全指南
unity
南無忘码至尊11 小时前
Unity 实现与 Ollama API 交互的实时流式响应处理
unity·游戏引擎·交互
平行云15 小时前
如何实现UE程序大并发多集群的像素流部署
unity·ue5·图形渲染
向宇it1 天前
【unity小技巧】在 Unity 中将 2D 精灵添加到 3D 游戏中,并实现阴影投射效果,实现类《八分旅人》《饥荒》等等的2.5D游戏效果
游戏·3d·unity·编辑器·游戏引擎·材质
向宇it2 天前
Unity Universal Render Pipeline/Lit光照材质介绍
游戏·unity·c#·游戏引擎·材质
__water2 天前
RHA《Unity兼容AndroidStudio打Apk包》
android·unity·jdk·游戏引擎·sdk·打包·androidstudio
两水先木示3 天前
【Unity3D】微信小游戏适配安全区域或胶囊控件(圆圈按钮)水平高度一致方案
unity·微信小游戏·安全区域·ui适配·胶囊控件·safearea
枯萎穿心攻击3 天前
ECS由浅入深第三节:进阶?System 的行为与复杂交互模式
开发语言·unity·c#·游戏引擎
不绝1913 天前
怪物机制分析(有限状态机、编辑器可视化、巡逻机制)
网络·游戏·unity·游戏引擎
unicrom_深圳市由你创科技3 天前
Unity开发如何解决iOS闪退问题
unity·ios·蓝桥杯