ECS is a software architectural pattern that follows the Composition over Inheritance principle. It is the heart of high-performance engines like Unity DOTS and Bevy, allowing games to handle hundreds of thousands of active objects.
Position, Velocity, Health).In standard OOP (List<GameObject>), objects are scattered in memory. When the CPU tries to update them, it constantly “misses” the cache because it has to jump around.
In ECS, components of the same type are stored in contiguous arrays (Archetypes). The CPU can prefetch this data efficiently, leading to 10x-100x performance gains.
// 1. Component Data
public struct Velocity : IComponentData {
public float3 Value;
}
// 2. The System
public partial struct MovementSystem : ISystem {
[BurstCompile] // Compiles to highly optimized machine code
public void OnUpdate(ref SystemState state) {
float dt = SystemAPI.Time.DeltaTime;
// Query all entities that have both LocalTransform AND Velocity
foreach (var (transform, velocity) in
SystemAPI.Query<RefRW<LocalTransform>, RefRO<Velocity>>()) {
transform.ValueRW.Position += velocity.ValueRO.Value * dt;
}
}
}