Version: Unity 7.0 Alpha (7000.0)
LanguageEnglish
  • C#

SystemBase

class in Unity.Entities

/

Inherits from:Unity.Entities.ComponentSystemBase

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

Implement SystemBase to create a system in ECS.

### 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 OnUpdate() 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.

#### Entity iteration options

" [IJobEntity] — define a struct with an Execute method taking ref / in component parameters. Run it using Schedule(), ScheduleParallel(), or Run(). 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, new MyJob { DeltaTime = SystemAPI.Time.DeltaTime }). " __SystemAPI.Query — iterate immediately on the main thread (optionally Burst compiled) using @@foreach@@ over tuples of @@RefRO<T>@@ / @@RefRW<T>@@ 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.

#### 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 OnUpdate if every EntityQuery used by the system is empty. " DisableAutoCreationAttribute -- do not create the system automatically. " AlwaysSynchronizeSystemAttribute -- force a sync point before invoking OnUpdate.

Migration note: Entities.ForEach and Job.WithCode are deprecated as of Entities 1.4. Use [IJobEntity], [IJobChunk] or SystemAPI.Query. For more information, refer to the upgrade guide.

[JobHandle]: xref:Unity.Jobs.JobHandle [JobHandle.CompleteDependencies]: xref:Unity.Jobs.JobHandle.CombineDependencies [C# Job]: xref:JobSystem [ECB]: xref:Unity.Entities.EntityCommandBuffer [ComponentSystemBase.GetEntityQuery]: xref:Unity.Entities.ComponentSystemBase.GetEntityQuery" [ComponentSystemBase.RequireForUpdate]: xref:Unity.Entities.ComponentSystemBase.RequireForUpdate*

Properties

Property Description
CheckedStateRef The SystemState for this SystemBase.
Dependency The ECS-related data dependencies of the system.

Public Methods

Method Description
GetBufferLookup Manually gets a BufferLookup<T> object that can access a DynamicBuffer<T0>.
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.
Update Update the system manually.

Protected Methods

Method Description
CompleteDependency Completes job handles registered with this system. See SystemBase.Dependency for more information.
HasBuffer Checks whether an entity has a dynamic buffer of a specific IBufferElementData type.
OnUpdateImplement OnUpdate() to perform the major work of this system.

Inherited Members

Properties

PropertyDescription
Enabled Controls whether this system executes when its OnUpdate function is called.
EntityManager The EntityManager object of the ComponentSystemBase.World in which this system exists.
EntityQueries The query objects cached by this system.
GlobalSystemVersion The current change version number in this ComponentSystemBase.World.
LastSystemVersion The current version of this system.
SystemHandle The SystemHandle of this system.
World The World in which this system exists.
WorldUpdateAllocator Retrieve world update allocator from system state.

Public Methods

MethodDescription
GetBufferTypeHandle Manually gets the run-time type information required to access an array of buffer components in a chunk.
GetComponentTypeHandle Manually gets the run-time type information required to access an array of component data in a chunk.
GetDynamicComponentTypeHandle Manually gets the run-time type information required to access an array of component data in a chunk.
GetDynamicSharedComponentTypeHandle Manually gets the run-time type information required to access a shared component data in a chunk.
GetEntityQuery Gets the cached query for the specified component types, if one exists; otherwise, creates a new query instance and caches it.
GetEntityTypeHandle Manually gets the run-time type information required to access the array of Entity objects in a chunk.
GetSharedComponentTypeHandle Manually gets the run-time type information required to access a shared component data in a chunk.
RequireAnyForUpdate Provide a set of queries, one of which must match entities for the system to run.
RequireForUpdate Adds a query that must match entities for the system to run. You can add multiple required queries to a system; all of them must match at least one entity for the system to run.
ShouldRunSystem Reports whether this system satisfies the criteria to update. This function is used internally to determine whether the system's OnUpdate function can be skipped.

Protected Methods

MethodDescription
OnCreate Called when this system is created.
OnDestroy Called when this system is destroyed.
OnStartRunning Called before the first call to OnUpdate and when a system resumes updating after being stopped or disabled.
OnStopRunning Called when this system stops running because no entities match the system's EntityQuery objects or because you change the system ComponentSystemBase.Enabled property to false.