Class SystemBase
Implement SystemBase to create a system in ECS.
Inherited Members
Namespace: Unity.Entities
Assembly: solution.dll
Syntax
[RequireDerived]
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 iterates over entities using a Entities.
public struct Position : IComponentData
{
public float3 Value;
}
public struct Velocity : IComponentData
{
public float3 Value;
}
[RequireMatchingQueriesForUpdate]
public partial class ECSSystem : SystemBase
{
protected override void OnUpdate()
{
// Local variable captured in ForEach
float dT = SystemAPI.Time.DeltaTime;
Entities
.WithName("Update_Displacement")
.ForEach(
(ref Position position, in Velocity velocity) =>
{
position = new Position()
{
Value = position.Value + velocity.Value * dT
};
}
)
.ScheduleParallel();
}
}
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:
- On
Create() -- called when the system is created. - On
Start -- before the first OnUpdate and whenever the system resumes running.Running() - On
Update() -- every frame as long as the system has work to do (see ShouldRun ) and the system is Enabled.System() - On
Stop -- whenever the system stops updating because it finds no entities matching its queries. Also called before OnDestroy.Running() - On
Destroy() -- 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 On
System update order
The runtime executes systems in the order determined by their Component
If you do not explicitly place a system in a specific group, the runtime places it in the default World
Simulation
Entity queries
A system caches all queries created through an Entities.OnUpdate()
every frame. You can use the Require
Entities.ForEach and Job.WithCode constructions
The Entities property provides a convenient mechanism for iterating over entity
data. Using an Entities.
The Entities.
The Job property provides a similar mechanism for defining a C# Job. You can only use
Schedule()
to run a Job.
System attributes
You can use a number of attributes on your SystemBase implementation to control when it updates:
- Update
In -- place the system in a ComponentGroup Attribute System .Group - Update
Before -- always update the system before another system in the same group.Attribute - Update
After -- always update the system after another system in the same group.Attribute - Require
Matching -- skipQueries For Update Attribute OnUpdate
if every EntityQuery used by the system is empty. - Disable
Auto -- do not create the system automatically.Creation Attribute - Always
Synchronize -- force a sync point before invokingSystem Attribute OnUpdate
.
Properties
Name | Description |
---|---|
Checked |
The System |
Dependency | The ECS-related data dependencies of the system. |
Entities | Provides a mechanism for defining an entity query and invoking a lambda expression on each entity selected by that query. |
Job | Provides a mechanism for defining and executing an [IJob]. |
Methods
Name | Description |
---|---|
Complete |
Completes job handles registered with this system. See Dependency for more information. |
Exists(Entity) | Checks if the entity exists inside this system's EntityManager. |
Get |
Obsolete. Use Get |
Get |
Manually gets a BufferLookup<T> object that can access a Dynamic |
Get |
Gets the dynamic buffer of an entity. |
Get |
Obsolete. Use Get |
Get |
Manually gets a dictionary-like container containing all components of type T, keyed by Entity. |
Get |
Look up the value of a component for an entity. |
Get |
Manually gets an EntityStorageInfoLookup object that can access a Entity |
Get |
Obsolete. Use Get |
Has |
Checks whether an entity has a dynamic buffer of a specific IBufferElementData type. |
Has |
Checks whether an entity has a specific type of component. |
On |
Implement |
Set |
Sets the value of a component of an entity. |
Update() | Update the system manually. |