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");}
}