当前位置: 首页 > news >正文

【Unity3D】ECS入门学习(九)SystemBase

SystemBase:支持主线程或多线程执行筛选实体任务。
主要介绍是内部成员:Entities的各种筛选方法,其内部成员还有EntityManager

ForEach方法筛选,传递一个有参委托函数进去,参数ref xxx组件类(可填多个)代表筛选条件。

WithAll:&& 形式筛选组件
WithAny: || 形式筛选组件
WithNone: 筛选出不带某组件的
WithChangeFilter:监听组件数据变化时触发筛选
WithSharedComponentFilter:筛选共享组件(指定特定数据)
WithStoreEntityQueryInField(ref EntityQuery对象):存储筛选结果
WithEntityQueryOptions(EntityQueryOptions):
        EntityQueryOptions.FilterWriteGroup:筛选带[WriteGroup(typeof(xxx))特性的组件
        EntityQueryOptions.IncludeDisabled:具有Disabled(已禁用)组件的实体
        EntityQueryOptions.IncludePrefab:具有Prefab组件的实体
WithoutBurst:不使用Burst编译
WithBurst:使用Burst编译

Run:主线程(单线程执行)
Schedule:多线程并发
ScheduleParallel:多线程并行

using System.Collections;
using System.Collections.Generic;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;
/// <summary>
/// 可操作主线程或多线程; ComponentSystem单线程 JobComponentSystem多线程
/// </summary>
public class MySystemBase : SystemBase
{EntityQuery query;//以下方法从上到下按顺序执行protected override void OnCreate(){Debug.Log("OnCreate");}protected override void OnStartRunning(){Debug.Log("OnStartRunning");}protected override void OnUpdate(){Debug.Log("OnUpdate");//使用内部成员Entities遍历所有实体筛选出Translation组件的实体进行修改Entities.ForEach((ref Translation trans) =>{trans.Value = new Unity.Mathematics.float3(0, 5, 0);}).Run();//使用WithAll增加筛选条件,必须带有PrintTestComponentData组件的//使用Run()代表是主线程执行Entities.ForEach((ref Translation trans) =>{trans.Value = new Unity.Mathematics.float3(0, 5, 0);}).WithAll<PrintTestComponentData>().Run();//WithAny 只要带任意一个即可满足筛选Entities.ForEach((ref Translation trans) =>{trans.Value = new Unity.Mathematics.float3(0, 5, 0);}).WithAny<PrintTestComponentData>().Run();//WithNone 不包含xxx的Entities.ForEach((ref Translation trans) =>{trans.Value = new Unity.Mathematics.float3(0, 5, 0);}).WithNone<PrintTestComponentData>().Run();//WithChangeFilter 监听组件值变化时才触发方法体,参数必须带上ref PrintTestComponentData  否则会报错Entities.ForEach((ref Translation trans, ref PrintTestComponentData data) =>{trans.Value = new Unity.Mathematics.float3(0, 5, 0);}).WithChangeFilter<PrintTestComponentData>().Run();//WithSharedComponentFilter 筛选共享组件 特定值为2的Entities.ForEach((ref Translation trans) =>{trans.Value = new Unity.Mathematics.float3(0, 5, 0);}).WithSharedComponentFilter(new MyShareComponentData() { data = 2 }).Run();//存储筛选结果        Entities.ForEach((ref Translation trans) =>{trans.Value = new Unity.Mathematics.float3(0, 5, 0);}).WithStoreEntityQueryInField(ref query).Run();NativeArray<Entity> array = query.ToEntityArray(Unity.Collections.Allocator.TempJob);foreach (var v in array){//do something}//过滤出带[WriteGroup]特性的组件实体  [WriteGroup(typeof(PrintTestComponentData))] 查询会根据查询中指定的组件的WriteGroupAttribute属性筛选所选实体Entities.ForEach((ref Translation trans) =>{trans.Value = new Unity.Mathematics.float3(0, 5, 0);}).WithEntityQueryOptions(EntityQueryOptions.FilterWriteGroup).Run();//过滤出带[DisableAutoCreation]特性的组件实体  该查询不会隐式的排除具有Disabled(已禁用)组件的实体Entities.ForEach((ref Translation trans) =>{trans.Value = new Unity.Mathematics.float3(0, 5, 0);}).WithEntityQueryOptions(EntityQueryOptions.IncludeDisabled).Run();//过滤出预制体实体  [DisableAutoCreation]  该查询不会隐式的排除具有Prefab组件的实体Entities.ForEach((ref Translation trans) =>{trans.Value = new Unity.Mathematics.float3(0, 5, 0);}).WithEntityQueryOptions(EntityQueryOptions.IncludePrefab).Run();//Schedule 多线程并发执行 ; WithoutBurst 不使用Burst编译Entities.ForEach((ref Translation trans) =>{trans.Value = new Unity.Mathematics.float3(0, 5, 0);}).WithoutBurst().Schedule();//ScheduleParallel 配合Job 多线程并行执行 WithBurst 使用Burst编译Entities.ForEach((ref Translation trans) =>{trans.Value = new Unity.Mathematics.float3(0, 5, 0);}).WithBurst().ScheduleParallel();query.Dispose();array.Dispose();}protected override void OnStopRunning(){Debug.Log("OnStopRunning");}protected override void OnDestroy(){Debug.Log("OnDestroy");}
}

http://www.lryc.cn/news/514359.html

相关文章:

  • 【Triton-ONNX】如何使用 ONNX 模型服务与 Triton 通信执行推理任务上-Triton快速开始
  • CertiK《Hack3d:2024年度安全报告》(附报告全文链接)
  • TIOBE 指数 12 月排行榜公布,VB.Net排行第九
  • 【网络协议】开放式最短路径优先协议OSPF详解(一)
  • 嵌入式Linux驱动开发的基本知识(驱动程序的本质、常见的设备类型、设备号的本质理解、设备实例的注册过程)
  • 爱死机第四季(秘密关卡)4KHDR国语字幕
  • kubelet状态错误报错
  • <div>{{ $t(“collectionPlan“) }}</div> 中的$t是什么
  • [C++刷题] 求回文素数
  • SQLALchemy如何将SQL语句编译为特定数据库方言
  • [卫星遥感] 解密卫星目标跟踪:挑战与突破的深度剖析
  • I2C(一):存储器模式:stm32作为主机对AT24C02写读数据
  • scrapy 教程
  • 2025元旦源码免费送
  • 高级架构五 设计模式
  • RFID手持机与RFID工业平板在仓储物流管理系统中的选型
  • IoC设计模式详解:控制反转的核心思想
  • 《云原生安全攻防》-- K8s安全配置:CIS安全基准与kube-bench工具
  • LINUX下载编译gtk
  • 基于VSCode软件框架的RISC-V IDE MRS2正式上线发布
  • AWS re:Invent 2024 - Dr. Werner Vogels 主题演讲
  • 前端小案例——520表白信封
  • FPGA随记——过约束
  • 如何利用云计算进行灾难恢复?
  • 【华为OD-E卷 - 九宫格按键输入 100分(python、java、c++、js、c)】
  • 基于AI大模型的医院SOP优化:架构、实践与展望
  • Linux快速入门-一道简单shell编程题目
  • Hive如何创建自定义函数(UDF)?
  • 聊聊前端框架中的process.env,env的来源及优先级(next.js、vue-cli、vite)
  • linux shell脚本 【分支结构case...in 、循环结构、函数】内附练习