Unity DOTS Programming
This skill governs the Data-Oriented Design (DOD) architecture using Unity's Entity Component System (ECS). It explicitly forbids Object-Oriented paradigms (like MonoBehaviour) in performance-critical code to maximize cache locality.
When to Use
- Use when simulating massive numbers of entities (e.g., thousands of units).
- Use when defining
IComponentDatastructs orISystemlogic. - NOT for standard UI logic or simple 2D prototypes.
Core Process
Phase 1: Pure Data Components
- Use
IComponentDataexclusively for state. - Components must be strictly unmanaged (blittable structs). Do not store classes, arrays, or strings in ECS components.
Phase 2: System Isolation
- Use
ISystem(unmanaged systems) instead ofSystemBase(managed systems) whenever possible to allow Burst compilation. - Queries (
SystemAPI.Query) must be extremely specific. Filter out entities usingWithNoneorWithAllto minimize iteration times.
Phase 3: The MonoBehaviour Bridge
- If interaction with GameObjects is required (e.g., legacy UI), utilize
IComponentDataas a bridge, reading it from a managed system. Do not link GameObjects directly into pure ECS structural data.
Common Rationalizations
| Rationalization | Reality |
|---|---|
"I'll just add a reference to the Transform in my component data." |
Managed references destroy cache locality and prevent Burst compilation. Use LocalTransform and sync them if absolutely necessary. |
"I'll use SystemBase because it's easier to write." |
SystemBase incurs garbage collection overhead. ISystem guarantees strict unmanaged performance. |
Red Flags
- Components defined as
classinstead ofstruct. - Using
GameObject.Findinside any ECS system update loop.
Verification
Before completing the ECS architecture, verify:
- All components are unmanaged blittable
structs. - Systems inherit from
ISystemand are decorated with[BurstCompile]. -
SystemAPI.Queryexplicitly limits iteration scopes.