ecb-multiplayback
Create a reusable ECB with PlaybackPolicy.MultiPlayback that can be played back multiple times — for streaming scenarios where the same entity creation sequence must execute on every subscene load.
- License 许可
- MIT
- 最近更新
- 2026-07-25
- Stars
- ★ 8
- Forks
- 0
01 前言 Front Matter
- name
- ecb-multiplayback
- tags
- [ "ecb" ]
- metadata
- { "tier": 3, "internal-only": true }
- description
- Create a reusable ECB with PlaybackPolicy.MultiPlayback that can be played back multiple times — for streaming scenarios where the same entity creation sequence must execute on every subscene load.
03 SKILL.md 正文
ECB MultiPlayback — Reusable Command Buffers
Intent
Create a reusable ECB with PlaybackPolicy.MultiPlayback that can be played back multiple times — for streaming scenarios where the same entity creation sequence must execute on every subscene load.
Use When
Subscene streaming systems that must replay a fixed set of entity creation commands each time a section loads. Any pattern where an ECB is serialised into a component and replayed on demand.
Avoid When
The ECB is only played back once — SinglePlayback (the default) is sufficient and cheaper. Avoid Allocator.Temp or TempJob for MultiPlayback ECBs — they must use Allocator.Persistent.
Senior Pattern
new EntityCommandBuffer(Allocator.Persistent, PlaybackPolicy.MultiPlayback)— persistent lifetime, multi-replay safe.- Store the ECB in a component field (e.g.,
struct PostLoadCommandBuffer : IComponentData { public EntityCommandBuffer CommandBuffer; }). - Call
ecb.Playback(entityManager)on each subscene load event. - Dispose the ECB explicitly when the owning entity is destroyed (OnDestroy hook or cleanup system).
Code Template
public struct SectionPostLoadCommands : IComponentData
{
public EntityCommandBuffer CommandBuffer;
}
// At bake time or init: create the reusable ECB
var ecb = new EntityCommandBuffer(Allocator.Persistent, PlaybackPolicy.MultiPlayback);
var setupEntity = ecb.CreateEntity();
ecb.AddComponent(setupEntity, new SectionConfig { TileOffset = offset });
state.EntityManager.AddComponentData(sectionEntity,
new SectionPostLoadCommands { CommandBuffer = ecb });
// On each section load:
var commands = SystemAPI.GetComponent<SectionPostLoadCommands>(sectionEntity);
commands.CommandBuffer.Playback(state.EntityManager);
// On section unload / entity destroy:
commands.CommandBuffer.Dispose();
Anti-Patterns
- Using PlaybackPolicy.SinglePlayback (default) and calling Playback twice — second call throws a safety exception.
- Using Allocator.Temp or TempJob with MultiPlayback — the ECB is freed before the second playback.
- Forgetting to Dispose a Persistent ECB — memory leak that persists until world destruction.
- Using MultiPlayback for a single-use ECB — unnecessary overhead and complexity.
Runtime Risks
- SinglePlayback called twice: InvalidOperationException on second Playback.
- Missing Dispose on Persistent ECB: memory leak.
Performance Notes
Allocator.Persistent is heap-allocated — higher overhead than Temp. Use only when the multi-replay semantics are genuinely required. For the streaming use case, the cost is negligible (one ECB per subscene section).
Architecture Guidance
MultiPlayback ECB = a reusable command script. Treat it as a data asset stored in a component, not as an ephemeral command buffer. Scope its lifetime explicitly to the subscene section or the system that owns it.
Related Skills
[[entity-command-buffer]]
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.