【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:多线程并行

cs 复制代码
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");
    }
}
相关推荐
charlie1145141912 小时前
从0开始使用面对对象C语言搭建一个基于OLED的图形显示框架(协议层封装)
c语言·驱动开发·单片机·学习·教程·oled
马船长3 小时前
[BSidesCF 2020]Had a bad day1
学习
黄交大彭于晏3 小时前
三端回链增加截图功能
学习
linwq84 小时前
设计模式学习(二)
java·学习·设计模式
Fhd-学习笔记4 小时前
《大语言模型》综述学习笔记
笔记·学习·语言模型
Dr.勿忘5 小时前
C#面试常考随笔8:using关键字有哪些用法?
开发语言·unity·面试·c#·游戏引擎
简知圈6 小时前
【04-自己画P封装,并添加已有3D封装】
笔记·stm32·单片机·学习·pcb工艺
YxVoyager6 小时前
GAMES101学习笔记(五):Texture 纹理(纹理映射、重心坐标、纹理贴图)
笔记·学习·图形渲染
存储服务专家StorageExpert6 小时前
答疑解惑:如何监控EMC unity存储系统磁盘重构rebuild进度
运维·unity·存储维护·emc存储
徐某人..6 小时前
ARM嵌入式学习--第十天(UART)
arm开发·单片机·学习·arm