SystemBase
Implement SystemBase to create a system in ECS.
Read time 5 minutesLast updated 3 days ago
Definition
- Type: Class
- Namespace: Unity.Entities
- Assembly: UnityEngine.EntitiesModule
- Inherits from: ComponentSystemBase
public abstract class SystemBase : ComponentSystemBase
Remarks
Systems in ECS
A typical system operates on a set of entities that have specific components. The system identifies the components of interest, reading and writing data, and performing other entity operations as appropriate.
The following example shows a basic system that schedules an [IJobEntity] job to iterate over entities. In this example, the system iterates over all entities with both a Position and a Velocity component and updates Position based on the delta time elapsed since the last frame.
System lifecycle callbacks
You can define a set of system lifecycle event functions when you implement a system. The runtime invokes these functions in the following order:
" ComponentSystemBase.OnCreate() -- called when the system is created. " ComponentSystemBase.OnStartRunning() -- before the first OnUpdate and whenever the system resumes running. " SystemBase.OnUpdate -- every frame as long as the system has work to do (see ComponentSystemBase.ShouldRunSystem()) and the system is ComponentSystemBase.Enabled. " ComponentSystemBase.OnStopRunning() -- whenever the system stops updating because it finds no entities matching its queries. Also called before OnDestroy. " ComponentSystemBase.OnDestroy() -- when the system is destroyed.
All of these functions are executed on the main thread. To perform work on background threads, you can schedule jobs from the SystemBase.OnUpdate function.
System update order
The runtime executes systems in the order determined by their ComponentSystemGroup. Place a system in a group using UpdateInGroupAttribute. Use UpdateBeforeAttribute and UpdateAfterAttribute to specify the execution order within a group.
If you do not explicitly place a system in a specific group, the runtime places it in the default WorldSimulationSystemGroup. By default, all systems are discovered, instantiated, and added to the default World. You can use the DisableAutoCreationAttribute to prevent a system from being created automatically.
Entity queries
A system caches all queries created through iteration constructs (such as an [IJobEntity] schedule or a SystemAPI.Query loop), through ComponentSystemBase.GetEntityQuery, or through ComponentSystemBase.RequireForUpdate. By default, a system calls every frame. You can use the RequireMatchingQueriesForUpdateAttribute to make the system only update when one of these cached queries finds entities. See ComponentSystemBase.ShouldRunSystem() for more details on whether a system will update.
OnUpdate()Entity iteration options
" [IJobEntity] — define a struct with an Execute method taking / component parameters. Run it using , , or . Jobs execute asynchronously, and cannot safely access data outside of their own instance. Put any required data into fields and set them when you create the job (for example, ). " SystemAPI.Query — iterate immediately on the main thread (optionally Burst compiled) using over tuples of / wrappers. " IJobChunk — implement job structs that run per chunk. " C# Jobs (IJob, IJobParallelFor, etc.) — general-purpose, non-ECS-specific jobs. Use such jobs for computation or processing that does not require an entity query.
refinSchedule()ScheduleParallel()Run()new MyJob { DeltaTime = SystemAPI.Time.DeltaTime }foreachRefRO<T>RefRW<T>System attributes
You can use a number of attributes on your SystemBase implementation to control when it updates:
" UpdateInGroupAttribute -- place the system in a ComponentSystemGroup. " UpdateBeforeAttribute -- always update the system before another system in the same group. " UpdateAfterAttribute -- always update the system after another system in the same group. " RequireMatchingQueriesForUpdateAttribute -- skip if every EntityQuery used by the system is empty. " DisableAutoCreationAttribute -- do not create the system automatically. " AlwaysSynchronizeSystemAttribute -- force a sync point before invoking .
OnUpdateOnUpdateMigration note: and are deprecated as of Entities 1.4. Use [IJobEntity], [IJobChunk] or SystemAPI.Query. For more information, refer to the upgrade guide.
Entities.ForEachJob.WithCodeProperties
Property | Description |
|---|---|
| CheckedStateRef | The SystemState for this SystemBase. |
| Dependency | The ECS-related data dependencies of the system. |
Methods
Method | Description |
|---|---|
| CompleteDependency | Completes job handles registered with this system. See SystemBase.Dependency for more information. |
| GetBufferLookup | Manually gets a BufferLookup\<T\> object that can access a DynamicBuffer<T>. |
| GetComponentLookup | Manually gets a dictionary-like container containing all components of type T, keyed by Entity. |
| GetEntityStorageInfoLookup | Manually gets an EntityStorageInfoLookup object that can access a EntityStorageInfo. |
| HasBuffer | Checks whether an entity has a dynamic buffer of a specific IBufferElementData type. |
| OnUpdate | Implement |
| Update | Update the system manually. |