目录
System groups and system update order
Transform components and systems
Systems
system runs on the main thread (usually once per frame). Normally, a system will only access entities of its own world, but this is not an enforced restriction.
has three key methods:
ISystemState method | Description |
---|---|
OnUpdate() | Normally called once per frame, though this depends upon the SystemGroup to which the system belongs. |
OnCreate() | Called before the first call to OnUpdate and whenever a system resumes running. |
OnDestroy() | Called when a system is destroyed. |
A system may additionally implement ISystemStartStop
, which has these methods:
ISystemStartStop method | Description |
---|---|
OnStartRunning() | Called before the first call to OnUpdate and after any time the system's Enabled property is changed from false to true . |
OnStopRunning() | Called before OnDestroy and after any time the system's Enabled property is changed from true to false . |
System groups and system update order
system groups 决定了update order. A system group is defined as a class inheriting from ComponentSystemGroup.
group的children 当有新的child 添加或移除的时候,会重新排序
The [UpdateBefore] and [UpdateAfter] 属性可用于确定同组中子元素之间的相对排序顺序.
Creating worlds and systems
By default, an automatic bootstrapping process creates a default world with three system groups:
InitializationSystemGroup
, which updates at the end of theInitialization
phase of the Unity player loop.start之后SimulationSystemGroup
, which updates at the end of theUpdate
phase of the Unity player loop.update之后PresentationSystemGroup
, which updates at the end of thePreLateUpdate
phase of the Unity player loop.PreLateUpdate之后
默认添加到 SimulationSystemGroup
,除非声明了 [UpdateInGroup] attribute.
The automatic bootstrapping process can be disabled with scripting defines:
Scripting define | Description |
---|---|
#UNITY_DISABLE_AUTOMATIC_SYSTEM_BOOTSTRAP_RUNTIME_WORLD | Disables automatic bootstrapping of the default world. |
#UNITY_DISABLE_AUTOMATIC_SYSTEM_BOOTSTRAP_EDITOR_WORLD | Disables automatic bootstrapping of the Editor world. |
#UNITY_DISABLE_AUTOMATIC_SYSTEM_BOOTSTRAP | Disables automatic bootstrapping of both the default world and the Editor world. |
当 automatic bootstrapping 关闭时,你需要:
- Creating any worlds you need.
- Calling World.GetOrCreateSystem<T>() to add the system and system group instances to the worlds.
- Registering top-level system groups (like
SimulationSystemGroup
) to update in the Unity PlayerLoop.
Alternatively, automatic bootstrapping can be customized by creating a class that implements ICustomBootstrap.
Time in worlds and systems
world 有一个 Time
属性, 返回 TimeData struct, containing the frame delta time and elapsed time. 通过 world's UpdateWorldTimeSystem 更新,The time value can be manipulated with these World
methods:
World method | Description |
---|---|
SetTime | Set the time value. |
PushTime | Temporarily change the time value. |
PopTime | Restore the time value from before the last push. |
Some system groups, like FixedStepSimulationSystemGroup, push a time value before updating their children and then pop the value once done updating.
SystemState
SystemState
包括:
Method or property | Description |
---|---|
World | The system's world. |
EntityManager | The EntityManager of the system's world. |
Dependency | A JobHandle used to pass job dependencies between systems. |
GetEntityQuery() | Returns an EntityQuery . |
GetComponentTypeHandle<T>() | Returns a ComponentTypeHandle<T> . |
GetComponentLookup<T>() | Returns a ComponentLookup<T> . |
⚠ IMPORTANT |
---|
SystemState来查询实体,而不是EntityManager . |
SystemAPI
The SystemAPI class has many static convenience methods, 和 World
, EntityManager
, and SystemState 里面的很像
The SystemAPI
methods rely upon source generators, so they only work in systems and IJobEntity
(but not IJobChunk
). 好处就是复制的时候代码是一样的。
📝 NOTE |
---|
SystemAPI 、SystemState 、 EntityManager、 World 调用api的顺序,如果api system里面没有,依次往下找 |
SystemAPI
also provides a special Query() method that, through source generation, helps conveniently create a foreach loop over the entities and components matching a query.
Transform components and systems
LocalTransform 组件代表 entity的transform, entity transform层级还有下面三个:
- The Parent component stores the id of the entity's parent.
- The Child dynamic buffer component stores the ids of the entity's children.
- The PreviousParent component stores a copy of the id of the entity's parent.
To modify the transform hierarchy:
- Add the
Parent
component to parent an entity. - Remove an entity's
Parent
component to de-parent it. - Set an entity's
Parent
component to change its parent.
ParentSystem 负责更新 Child
and PreviousParent
组件
⚠ IMPORTANT |
---|
不能直接错做Child buffer 组件, 只能通过设置实体的父组件来修改转换层次结构。 |
每一帧 LocalToWorldSystem 会计算每一个 entity's world-space transform (from the LocalTransform
components of the entity and its ancestors) and assigns it to the entity's LocalToWorld component.
📝 NOTE |
---|
The Entity.Graphics systems read the LocalToWorld component but not any of the other transform components, so LocalToWorld is the only transform component an entity needs to be rendered. |