網(wǎng)站縮放代碼專(zhuān)業(yè)網(wǎng)站優(yōu)化推廣
?ComponentSystem:僅支持主線(xiàn)程執(zhí)行,不支持多線(xiàn)程,并且無(wú)法使用SystemBase介紹的擴(kuò)展方法。
using Unity.Entities;
using Unity.Transforms;
using Unity.Mathematics;/// <summary>
/// 僅主線(xiàn)程執(zhí)行,不支持多線(xiàn)程
/// </summary>
public class MyComponentSystem : ComponentSystem
{protected override void OnUpdate(){Entities.ForEach((ref Translation trans) =>{trans.Value = new float3(0, 1, 1);});//無(wú)法使用SystemBase的其他擴(kuò)展功能}
}
JobComponentSystem:僅支持多線(xiàn)程,不支持主線(xiàn)程,可以使用擴(kuò)展方法,并且重寫(xiě)的方法與其他都不同,它需要一個(gè)JobHandle返回值?
using Unity.Entities;
using Unity.Jobs;
using Unity.Transforms;
/// <summary>
/// 支持多線(xiàn)程 但不支持主線(xiàn)程執(zhí)行
/// </summary>
public class MyJobComponentSystem : JobComponentSystem
{protected override JobHandle OnUpdate(JobHandle inputDeps){return Entities.ForEach((ref Translation trans) =>{trans.Value = new Unity.Mathematics.float3(0, 0, 1);}).WithName("MyName").Schedule(inputDeps);//可以使用SystemBase擴(kuò)展方法}
}
具體文檔:Using Entities.ForEach | Entities | 0.50.1-preview.2
如下圖是三個(gè)執(zhí)行篩選的方法限制,簡(jiǎn)單理解就是:
Run:非Burst編譯下 可訪(fǎng)問(wèn)所有成員,可修改實(shí)體?!局骶€(xiàn)程執(zhí)行】
Schedule:僅能獲取局部成員,可修改實(shí)體。【新開(kāi)一個(gè)后臺(tái)線(xiàn)程執(zhí)行】
ScheduleParallel:僅能獲取局部成員,可以讀取實(shí)體,但不能修改實(shí)體,并且可讀數(shù)據(jù)只允許存儲(chǔ)到WithReadOnly(xxx)方法標(biāo)記的只讀變量?jī)?nèi)?!径嗑€(xiàn)程并行執(zhí)行】
Supported Feature | Run | Schedule | ScheduleParallel |
---|---|---|---|
Capture local value type | x | x | x |
Capture local reference type | x (only WithoutBurst and not in ISystem) | ||
Writing to captured variables | x | ||
Use field on the system class | x (only WithoutBurst) | ||
Methods on reference types | x (only WithoutBurst and not in ISystem) | ||
Shared Components | x (only WithoutBurst and not in ISystem) | ||
Managed Components | x (only WithoutBurst and not in ISystem) | ||
Structural changes | x (only WithStructuralChanges and not in ISystem) | ||
SystemBase.GetComponent | x | x | x |
SystemBase.SetComponent | x | x | |
GetComponentDataFromEntity | x | x | x (only as ReadOnly) |
HasComponent | x | x | x |
WithDisposeOnCompletion | x | x | x |
WithScheduleGranularity | x |