admin管理员组

文章数量:1567522

首先我个人英语是极差的,但实在找不到相关的文章,只有这个文档,所以借助机翻和个人的理解写这一篇笔记,有不对的地方请指出。

Supported Features

You can execute the lambda function on the main thread using Run(), as a single job using Schedule(), or as a parallel job using ScheduleParallel(). These different execution methods have different constraints on how you access data. In addition, Burst uses a restricted subset of the C# language, so you need to specify WithoutBurst() when using C# features outside this subset (including accessing managed types).

你可以用这3个方法去执行Entities.ForEach里面的lambda函数,不同的方法对访问数据有不同的约束,另外,Burst使用的是C#中一个受限制的子集,所以当你要用这个子集外的功能时需要加上WithoutBurst(),包括使用管理类型。

子集之外的功能具体看下表,管理类型是啥,外国人的x是✔的意思吧。

Run是主线程中立即执行,Schedule和ScheduleParallel会安排新线程执行,这三个方法在旨在程序并行运行时保护数据安全,避免数据不同步,从这个角度就很好理解下面这个表,主要是读写上有做区分。

Supported FeatureRunScheduleScheduleParallel

Capture local value type

获取局部值类型变量

xxx

Capture local reference type

获取局部引用类型变量

x (only WithoutBurst)  

Writing to captured variables

可写捕获的变量

x  

Use field on the system class

读写当前system类的属性

x (only WithoutBurst)  

Methods on reference types

引用类型的方法(没明白)

x (only WithoutBurst)  

Shared Components

使用共享组件(组件的一种类型)

x (only WithoutBurst)  

Managed Components

使用Manager组件(组件的一种类型)

x (only WithoutBurst)  

Structural changes

结构改变(应该是指改变Entity的组件结构,增删组件会改变Entity所在的内存块位置,如从原型A块移动到原型B块,即使在主线程中也需要加上WithStructuralChanges)

x (only WithoutBurst and WithStructuralChanges)  
SystemBase.GetComponentxxx
SystemBase.SetComponentxx 
GetComponentDataFromEntityxxx (only as ReadOnly)
HasComponentxxx

WithDeallocateOnJobCompletion

(没能理解,应该跟上面一样是个方法)

xxx

Run是在主线程上执行所以没什么特别的什么情况都可以使用,但性能会差很多。

Schedule和ScheduleParallel都是指派多线程并发执行,对于局部变量的方法都是可读不可写,但对于组件数据,Schedule是可读可写而ScheduleParallel是只读,这是这两个方法最大的区别。

Schedule会安排组件数据的读写进行同步,对组件使用有交集的system会按顺序执行,没有交集的system并发执行。

ScheduleParallel因为是只读属性,通过这个方法调度的所有线程都会并发执行,但我个人理解这个方法获取的组件和Schedule调度的组件有交集时,还是会按照Schedule安排的计划去执,以保证数据安全。

当system只读组件数据时,应尽量使用ScheduleParallel,没必要在主线程强制同步的system,应尽量使用Schedule或ScheduleParallel,按照这种规范性能让游戏获得更好的性能。

}——个人理解并非官方文档。

 

An Entities.ForEach construction uses specialized intermediate language (IL) compilation post-processing to translate the code you write for the construction into correct ECS code. This translation allows you to express the intent of your algorithm without having to include complex, boilerplate code. However, it can mean that some common ways of writing code are not allowed.

大意是:这个方法会用专门的IL(intermediate language)把你写的代码编译成符合规范的ECS代码,但是意味着你的代码有些写法是不支持的,下面表里就是不支持的功能。

The following features are not currently supported:

Unsupported Feature
Dynamic code in .With invocations (不允许执行动态代码,反射相关不能用吧)
SharedComponent parameters by ref (共享组件不能用ref修饰)
Nested Entities.ForEach lambda expressions (不允许镶嵌使用Entities.ForEach)

Entities.ForEach in systems marked with [ExecuteAlways] (currently being fixed)

(当前系统不能标志属性[ExecuteAlways],正在修复,当前文档版本0.16.0)

Calling with delegate stored in variable, field or by method (不能使用任何形式的委托)
SetComponent with lambda parameter type (with是啥意思,不能在lambda里set)
GetComponent with writable lambda parameter(不能在可写的lambda里get,不是很懂)
Generic parameters in lambdas (不能在lambda使用泛型)
In systems with generic parameters (不能在system中使用泛型)

还是有很多不理解的地方,后续补充

本文标签: 场景文档ECSUnity3dScheduleParallel