跳至正文
OpenAgentSkill
浏览已开放 · 目录记录日期 2026-11-21 · 自动更新和账户服务暂时关闭。
设计与 UI

unity-ecs-dots

Unity 6 ECS/DOTS development guide. Use when working with Entity Component System, data-oriented design, Jobs system, or Burst Compiler. Covers Entities, IComponentData, ISystem, EntityManager, baking, Jobs, and Burst. Based on Unity 6.3 LTS documentation.

Nice-Wolf-Studio · 已有静态检查记录

安装与使用

运行命令前先阅读来源与依赖说明。复制命令不会自动安装或执行。

npx skills add Nice-Wolf-Studio/unity-claude-skills --skill unity-ecs-dots

技能说明


name: unity-ecs-dots description: > Unity 6 ECS/DOTS development guide. Use when working with Entity Component System, data-oriented design, Jobs system, or Burst Compiler. Covers Entities, IComponentData, ISystem, EntityManager, baking, Jobs, and Burst. Based on Unity 6.3 LTS documentation.

Unity ECS / DOTS (Data-Oriented Technology Stack)

Based on Unity 6.3 LTS (6000.3) -- Entities 1.3, Burst 1.8, Jobs package

Core Concepts

ECS (Entity Component System) is Unity's data-oriented framework. It replaces the GameObject/MonoBehaviour model with a cache-friendly, high-performance architecture.

ECS vs MonoBehaviour

AspectMonoBehaviourECS
IdentityGameObject (heavy, managed)Entity (lightweight int ID)
DataClass fields on componentsUnmanaged structs (IComponentData)
LogicMethods on MonoBehaviourSystems (ISystem / SystemBase)
Memory layoutScattered across heapContiguous chunks by archetype
ThreadingMain thread onlyJobs + Burst for parallel work

Entities

Entities are lightweight identifiers (an index + version integer). They have no behavior and no data -- they are handles used to associate components.

Components (IComponentData)

Components are unmanaged structs with no methods. They hold only data.

using Unity.Entities;

public struct Speed : IComponentData
{
    public float Value;
}

public struct Health : IComponentData
{
    public float Current;
    public float Max;
}

Archetypes and Chunks

An archetype is a unique combination of component types. All entities with the same set of components share an archetype. Entities are stored in chunks (16 KB blocks) grouped by archetype, enabling cache-efficient iteration.

World and EntityManager

A World contains an EntityManager and a set of systems. The default world is created automatically. EntityManager is the primary API for creating/destroying entities and adding/removing components.

Baking and SubScene Workflow

Baking converts authoring GameObjects into runtime entities. This is a one-way conversion that happens at build time or when a SubScene is loaded in the Editor.

Baker

using Unity.Entities;

// Authoring component (MonoBehaviour on GameObject)
public class SpeedAuthoring : MonoBehaviour
{
    public float speed;
}

// Baker converts authoring data to ECS components
public class SpeedBaker : Baker<SpeedAuthoring>
{
    public override void Bake(SpeedAuthoring authoring)
    {
        var entity = GetEntity(TransformUsageFlags.Dynamic);
        AddComponent(entity, new Speed { Value = authoring.speed });
    }
}

SubScene

  • SubScenes contain GameObjects that are baked into entities at build time
  • At runtime, entity data streams in efficiently (no GameObject overhead)
  • In the Editor, SubScenes can be opened for editing (shows GameObjects) or closed (shows baked entities)
  • Always place ECS-managed objects inside a SubScene

TransformUsageFlags

FlagUse
DynamicEntity moves at runtime (gets LocalTransform, LocalToWorld)
RenderableEntity is rendered but not moved by code
WorldSpaceEntity uses world-space transform only
NoneNo transform components added

Systems

Systems contain all logic. They iterate over entities that match a component query.

ISystem (Recommended)

ISystem is the modern, unmanaged system type. It is Burst-compatible and should be preferred over SystemBase.

using Unity.Burst;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;

[BurstCompile]
public partial struct MovementSystem : ISystem
{
    [BurstCompile]
    public void OnCreate(ref SystemState state)
    {
        state.RequireForUpdate<Speed>();
    }

    [BurstCompile]
    public void OnUpdate(ref SystemState state)
    {
        float deltaTime = SystemAPI.Time.DeltaTime;

        foreach (var (transform, speed) in
            SystemAPI.Query<RefRW<LocalTransform>, RefRO<Speed>>())
        {
            transform.ValueRW.Position +=
                new float3(0, 0, speed.ValueRO.Value * deltaTime);
        }
    }

    [BurstCompile]
    public void OnDestroy(ref SystemState state) { }
}

Key points:

  • Must be a partial struct
  • Use [BurstCompile] on the struct and each method
  • OnCreate, OnUpdate, OnDestroy receive ref SystemState
  • state.RequireForUpdate<T>() -- system only runs when T exists
  • SystemAPI.Query<T>() -- type-safe foreach iteration

SystemBase (Managed, Legacy)

SystemBase is a managed class-based system. It supports managed code but cannot be Burst-compiled at the system level. Use ISystem for all new code. SystemBase uses Entities.ForEach() lambda syntax instead of SystemAPI.Query.

SystemGroup and Update Ordering

Systems are organized into groups that update in a defined order:

InitializationSystemGroup
  -> BeginInitializationEntityCommandBufferSystem
SimulationSystemGroup (default group)
  -> BeginSimulationEntityCommandBufferSystem
  -> [Your systems go here by default]
  -> EndSimulationEntityCommandBufferSystem
PresentationSystemGroup
  -> BeginPresentationEntityCommandBufferSystem

Control ordering with attributes:

[UpdateInGroup(typeof(SimulationSystemGroup))]
[UpdateBefore(typeof(OtherSystem))]
[UpdateAfter(typeof(AnotherSystem))]
public partial struct MySystem : ISystem { }

SystemAPI

SystemAPI provides static methods for safe, Burst-compatible access:

MethodPurpose
SystemAPI.Query<T1, T2>()Iterate matching entities
SystemAPI.TimeAccess TimeData (DeltaTime, ElapsedTime)
SystemAPI.GetSingleton<T>()Get singleton component value
SystemAPI.SetSingleton<T>(value)Set singleton component value
SystemAPI.GetComponent<T>(entity)Read component from entity
SystemAPI.SetComponent<T>(entity, value)Wr
阅读完整来源 ↗