ISystem overview
To create an unmanaged system, implement the interface type ISystem
.
Implement abstract methods
You must implement the following abstract methods, which you can Burst compile:
Method | Description |
---|---|
OnCreate |
System event callback to initialize the system and its data before usage. |
OnUpdate |
System event callback to add the work that your system must perform every frame. |
OnDestroy |
System event callback to clean up resources before destruction. |
ISystem
systems aren't inherited through a base class, like SystemBase
systems. Instead, each of the OnCreate
, OnUpdate
, and OnDestroy
methods have a ref SystemState
argument that you can use to access World
, WorldUnmanaged
, or contextual World
data and APIs such as EntityManager
.
Optionally implement ISystemStartStop
You can also optionally implement the interface ISystemStartStop
, which provides the following callbacks:
Method | Description |
---|---|
OnStartRunning |
System event callback before the first call to OnUpdate , and when a system resumes after it's stopped or disabled. |
OnStopRunning |
System event callback when a system is disabled or doesn't match any of the system's required components for update. |
Schedule jobs
All system events run on the main thread. It's best practice to use the OnUpdate
method to schedule jobs to perform most of the work. To schedule a job from a system, use one of the following:
IJobEntity
: Iterates over component data in multiple entities, which you can reuse across systems.IJobChunk
: Iterates over data by archetype chunk.
Callback method order
There are several callback methods within ISystem
that Unity invokes at various points during the system creation process, which you can use to schedule the work your system must do every frame:
OnCreate
: Called when ECS creates a system.OnStartRunning
: Called before the first call toOnUpdate
and whenever a system resumes running.OnUpdate
: Called every frame as long as the system has work to do. For more information on what determines when a system has work to do, seeShouldRunSystem
.OnStopRunning
: Called beforeOnDestroy
. Also called whenever the system stops running, which happens if no entities match the system'sRequireForUpdate
, or if you've set the system'sEnabled
property tofalse
. If you've not specified aRequireForUpdate
, the system runs continuously unless disabled or destroyed.OnDestroy
: Called when ECS destroys a system.
The following diagram illustrates a system's event order:
A parent system group's OnUpdate
method triggers the OnUpdate
methods of all the systems in its group. For more information about how systems update, see Update order of systems.