Game Development
SystemBase
Learn system logic with SystemBase.
What Is SystemBase?
SystemBase is one of the base classes used to create systems that process component data in Unity ECS. A system can find Entity objects with specific components and perform operations on their data. These operations are generally performed inside the OnUpdate method.
using Unity.Entities;
public partial class HealthSystem : SystemBase
{
protected override void OnUpdate()
{
// System operations
}
}
When using SystemBase, you must create the required structure for Unity ECS to recognize the system.
Processing Data with SystemBase
A SystemBase class can query Entity objects that contain specific components. For example, you can use Entities.ForEach to change the health values of objects with a Health component. The OnUpdate method contains operations that run during each system update cycle.
protected override void OnUpdate()
{
Entities.ForEach((ref Health health) =>
{
health.Value += 1;
}).Schedule();
}
In this example, the values of objects with a Health component are increased. The Entities.ForEach approach depends on the Entities package version being used.
Exercise Increase the Score Value with a System
Create a SystemBase class named ScoreSystem. Inside the OnUpdate method, use Entities.ForEach to increase the Value of objects containing the Score component by one and schedule the operation with Schedule. During the operation, first set the score value to 1 and print an update message to the console. Assume that the Score component has already been defined.
Show example solution
using Unity.Entities;
using UnityEngine;
public partial class ScoreSystem : SystemBase
{
protected override void OnUpdate()
{
Entities.ForEach((ref Score score) =>
{
score.Value = 1;
score.Value += 1;
}).Schedule();
Debug.Log("Score updated");
}
}
Test yourself
-
Which method performs the system update operations in the SystemBase class?
- OnDisable
- Awake
- OnUpdate
- Start
Show answer
OnUpdate — OnUpdate is the method called during a SystemBase system update.
-
Choose the System Class Base
public partial class MovementSystem : ____
Show answer
public partial class MovementSystem : SystemBase
-
SystemBase Elements
- SystemBase
- Base class for an ECS system
- OnUpdate
- Executes the system update
- Entities.ForEach
- Processes objects containing components
- Schedule
- Schedules the operation for execution
Next lesson: Burst compiler