ECS 简略版说明四:system、transform component,system

文章详细介绍了Unity中系统(Systems)的工作方式,包括它们在主循环中的运行、生命周期方法以及如何通过SystemGroups控制更新顺序。创建世界和系统的过程,以及时间管理在不同世界和系统中的应用。此外,还提到了Transform组件和相关系统如何维护实体的转换层次结构。
摘要由CSDN通过智能技术生成

目录

Systems

System groups and system update order

Creating worlds and systems

Time in worlds and systems

SystemState

SystemAPI

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 methodDescription
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:

ISystemStartStopmethodDescription
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 the Initialization phase of the Unity player loop.start之后
  • SimulationSystemGroup, which updates at the end of the Update phase of the Unity player loop.update之后
  • PresentationSystemGroup, which updates at the end of the PreLateUpdate phase of the Unity player loop. PreLateUpdate之后

默认添加到 SimulationSystemGroup ,除非声明了 [UpdateInGroup] attribute.

The automatic bootstrapping process can be disabled with scripting defines:

Scripting defineDescription
#UNITY_DISABLE_AUTOMATIC_SYSTEM_BOOTSTRAP_RUNTIME_WORLDDisables automatic bootstrapping of the default world.
#UNITY_DISABLE_AUTOMATIC_SYSTEM_BOOTSTRAP_EDITOR_WORLDDisables automatic bootstrapping of the Editor world.
#UNITY_DISABLE_AUTOMATIC_SYSTEM_BOOTSTRAPDisables 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 methodDescription
SetTimeSet the time value.
PushTimeTemporarily change the time value.
PopTimeRestore 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 propertyDescription
WorldThe system's world.
EntityManagerThe EntityManager of the system's world.
DependencyJobHandle 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, 和 WorldEntityManager, 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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TO_ZRG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值