unity-dots-best-practices
Unity DOTS, ECS, Jobs, and Burst guidance for scalable runtime systems. Core best-practices covering components, systems, bakers, blob assets, scheduling, structural change costs, and performance-sensitive simulation patterns for Entities 1.3+.
- License 许可
- MIT
- 最近更新
- 2026-07-25
- Stars
- ★ 8
- Forks
- 0
01 前言 Front Matter
- name
- unity-dots-best-practices
- metadata
- { "tier": 1, "source": "https://docs.unity3d.com/Packages/com.unity.entities@1.3", "version": "1.3.8", "user-invocable": false }
- use-when
- Load for unity-dots-dev on any DOTS or Hybrid domain task. Always included as P1 knowledge for architect and unity-dots-dev. Load when writing ISystem, IJobEntity, Baker, blob assets, ECB, or any ECS architecture decisions.
- platforms
- [ "claude-code", "codex", "copilot", "cursor", "windsurf" ]
- description
- Unity DOTS, ECS, Jobs, and Burst guidance for scalable runtime systems. Core best-practices covering components, systems, bakers, blob assets, scheduling, structural change costs, and performance-sensitive simulation patterns for Entities 1.3+.
- do-not-use-when
- Do not load for the unity-dev classic lane (MonoBehaviour tasks). Do not load for tester or verifier roles. Not needed for pure editor-tooling with no runtime ECS.
- task-categories
- [ "ecs", "performance", "burst", "jobs", "dots" ]
03 SKILL.md 正文
Unity DOTS Best Practices
When working on Unity DOTS runtime code, design and verify against these constraints. See @.claude/docs/mcp-integration.md for ai-game-developer and agentmemory tool usage.
Design Principles
- Design around data layout and access patterns first.
- Prefer
IComponentData,IBufferElementData,BlobAssetReference<T>,IAspect,ISystem, Burst, and jobs where they clearly improve scale and maintainability. - Keep hot paths allocation-free — no managed objects in simulation code.
- Minimize structural changes inside frequently executed loops.
- Treat archetype churn, sync points, and main-thread fallbacks as explicit costs, not background noise.
- Keep authoring/baker code separate from runtime systems (asmdef boundaries).
- Make update order and ownership explicit. Never imply ordering.
- Prefer deterministic, debuggable data flow over implicit side effects.
Component & Buffer Design
- One concern per component. Don't pack unrelated fields just because they share an entity.
IBufferElementDatafor variable-size per-entity data; cap withInternalBufferCapacitythoughtfully.BlobAssetReference<T>for large, immutable, shared data (e.g., level configs, ability data).IEnableableComponentfor state toggles that would otherwise cause add/remove churn.- Avoid managed components except for editor-bridging cases.
System Design
- Prefer
ISystem(Burst-compilable) overSystemBasefor hot paths. - Use
SystemAPIfor queries, singletons, time, lookups — keeps intent explicit. - Explicit
[UpdateInGroup],[UpdateAfter],[UpdateBefore]— never rely on implicit ordering. - Keep system responsibilities narrow. Compose, don't merge.
Job Design
IJobEntityfor per-entity work where chunk access is not needed.IJobChunkwhen you need chunk-level batching, change filtering, or component lookup optimization.EntityCommandBufferfor structural changes, with intentional playback point.EntityCommandBuffer.ParallelWriterfor parallel structural changes — passsortKeycorrectly.- Manage
Dependencyexplicitly when chaining multiple jobs.
Burst Compatibility
- No managed references, no virtual dispatch, no
string, nothrowin hot loops. BurstCompileeverything that can be Burst-compiled. Address compilation failures immediately.[BurstCompile(CompileSynchronously = true)]for tests so failures surface in CI.
NativeContainers
- Allocator choice:
Allocator.Temp(single frame, main thread),Allocator.TempJob(≤4 frames),Allocator.Persistent(long-lived). - Always dispose. Use
usingor explicit.Dispose()inOnDestroy. - Pass
[ReadOnly]and[WriteOnly]attributes to enable parallelism.
Self-Check Before Finalizing
- Is the data representation aligned with read/write patterns?
- Are there avoidable sync points? (
EntityManager.CompleteAllTrackedJobs,.GetSingleton, structural changes outside ECB) - Are structural changes limited and intentional?
- Is this Burst/job friendly? Any non-Burst-compatible types?
- Will the approach scale to large entity counts (target: 10k–100k)?
- Have I memoized any per-frame allocations into job-owned containers?
- Have I confirmed Unity-side state with
mcp__ai-game-developer__gameobject-component-get(for authoring) ortests-run(for behavior)? - Have I saved a
memory_lesson_savefor non-obvious decisions?
Anti-Patterns
EntityManager.CreateEntity/AddComponentinsideOnUpdatewithout ECB- Capturing managed lambdas in
SystemBase.Entities.ForEachhot paths - Using
SystemBasewhereISystemwould work - Implicit system ordering ("it happens to run before X")
- Buffers used as unbounded queues without consume logic
- One giant component that everyone reads and writes
- Manager-pattern singletons disguised as ECS singletons
04 同仓库更多 Skills
dots-update-groups
How to choose the right ECS system group and ordering so per-frame and per-fixed-step work is deterministic and never races its own readers. Covers InitializationSystemGroup / SimulationSystemGroup / PresentationSystemGroup / FixedStepSimulationSystemGroup / VariableRateSimulationSystemGroup, UpdateBefore / UpdateAfter peer ordering, OrderFirst / OrderLast group-boundary placement, RateManager, and the cross-group ordering trap. Use when adding an ISystem, fixing "value visible one frame late" bugs, or coupling simulation to a fixed timestep.
dots-transform-patterns
The Entities 1.x transform model — LocalTransform (writable input) vs LocalToWorld (system-owned matrix), Parent/Child hierarchy, reparenting cost, uniform vs non-uniform scale (PostTransformMatrix), and when to opt out with custom transform systems. Use when designing parent/child hierarchies, runtime reparenting (mount/dismount/socket), or choosing transform components at the Baker boundary.
dots-singleton-patterns
Model cross-system shared configuration and game state as a single-entity component with a strict single-writer contract. Covers RequireForUpdate gating, SystemAPI.GetSingleton / GetSingletonRW / HasSingleton / TryGetSingleton, bake-time vs runtime-created vs ECB-system singletons, the "exactly one entity" runtime invariant, and the single-writer rule. Use when designing frame-stable global config, shared mutable state, or any component intended to exist on exactly one entity.
dots-hybrid-bridge
The one-way bridge between DOTS-owned runtime state and Unity-owned presentation. Covers UnityObjectRef for managed asset refs in components, the DOTS-writes / Unity-reads ownership rule, when to use managed IComponentData vs UnityObjectRef vs CompanionComponent, baker-bridge vs runtime-bridge, and SubScene boundary rules. Use when an entity must reference Unity assets (Mesh, Material, AudioClip, ScriptableObject), a MonoBehaviour view must read entity state, or input/UI must trigger ECS work.
dots-event-driven-ecs
Model one-frame events and external requests in ECS without dual ownership, polling, or persistent state leaks. Covers request-tag pattern with ECB, enableable command components (no structural change), event buffers (IBufferElementData), explicit consumer ownership, one-frame latency rules, and the banned C# event / UnityEvent / static bus anti-patterns. Use when input/UI/network must trigger ECS work, when a one-frame signal fans out to N consumers (damage, hit, collision), or when state changes must be processed exactly once.
valid_skill
A complete, well-formed test fixture skill for SkillHub validation testing. Covers basic task routing and code review for Unity projects.