SystemBase
- Game Development course
- Expert
- Lesson 22/47
- 4 min
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.
Practice 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.
Starter code
using Unity.Entities;
// ScoreSystem sınıfını tanımla
{
// OnUpdate metodunu burada oluştur
{
// Score bileşenini işle
// Value değerini ayarla ve artır
// İşlemi zamanla
// Güncelleme mesajını yazdır
}
}Sample 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");
}
}Expected output
Score updated
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which method performs the system update operations in the SystemBase class?
- OnDisable
- Awake
- OnUpdate Correct answer
- Start
Explanation: OnUpdate is the method called during a SystemBase system update.
2 Choose the System Class Base
public partial class MovementSystem : ____
public partial class MovementSystem : SystemBase
Explanation: SystemBase is the base class for an ECS system. MovementSystem inherits from this class.
3 SystemBase Elements
| Term | Meaning |
|---|---|
SystemBase |
Base class for an ECS system |
OnUpdate |
Executes the system update |
Entities.ForEach |
Processes objects containing components |
Schedule |
Schedules the operation for execution |