wave6-enabled-ref-rw-in-job
Read and write the enabled bit of an IEnableableComponent directly inside IJobEntity.Execute() or SystemAPI.Query foreach using EnabledRefRW<T>, avoiding ECB overhead and one-frame delay.
- License 许可
- MIT
- 最近更新
- 2026-07-25
- Stars
- ★ 8
- Forks
- 0
01 前言 Front Matter
- name
- wave6-enabled-ref-rw-in-job
- tags
- [ "enableable", "jobs" ]
- metadata
- { "tier": 3, "internal-only": true }
- description
- Read and write the enabled bit of an IEnableableComponent directly inside IJobEntity.Execute() or SystemAPI.Query foreach using EnabledRefRW<T>, avoiding ECB overhead and one-frame delay.
03 SKILL.md 正文
EnabledRefRW in Jobs — Inline Bit Toggle
Intent
Read and write the enabled bit of an IEnableableComponent directly inside IJobEntity.Execute() or SystemAPI.Query foreach using EnabledRefRW<T>, avoiding ECB overhead and one-frame delay.
Use When
- Enable/disable decision depends on component data computed inside a job (proximity culling, LOD activation, range-based behavior)
- Bulk enable/disable in one pass without ECB one-frame delay
- Toggle must happen in same frame without deferred playback cost
Avoid When
- Enable/disable targets a different entity than the one being iterated — use ECB.SetComponentEnabled
- Only reading enabled state (not writing) — use EnabledRefRO<T>
Senior Pattern
// Main-thread foreach — toggle ALL entities (enabled AND disabled):
foreach (var (posRO, flagEnabled) in
SystemAPI.Query<RefRO<LocalTransform>, EnabledRefRW<ActiveBehavior>>()
.WithOptions(EntityQueryOptions.IgnoreComponentEnabledState))
{
flagEnabled.ValueRW = math.distancesq(posRO.ValueRO.Position, origin) <= radiusSq;
}
// IJobEntity (parallel) — same IgnoreComponentEnabledState requirement:
[BurstCompile]
[WithOptions(EntityQueryOptions.IgnoreComponentEnabledState)]
public partial struct ToggleInRangeJob : IJobEntity
{
public float3 Origin;
public float RadiusSq;
[BurstCompile]
public void Execute(in LocalTransform t, EnabledRefRW<ActiveBehavior> enabled)
{
enabled.ValueRW = math.distancesq(t.Position, Origin) <= RadiusSq;
}
}
// Schedule:
state.Dependency = new ToggleInRangeJob
{
Origin = origin,
RadiusSq = radius * radius
}.ScheduleParallel(state.Dependency);
Anti-Patterns
- Using EnabledRefRW<T> without IgnoreComponentEnabledState when intending to process all entities — default query only returns enabled entities; disabled are silently skipped, enabling them is impossible.
- Writing enabledRef.ValueRW from two parallel workers on the same entity — IJobEntity is safe (each chunk dispatched once); manual parallel splits are not.
- Using ECB.SetComponentEnabled when EnabledRefRW is available in the same job — ECB adds one-frame delay and playback cost unnecessarily.
Runtime Risks
- EnabledRefRW writes are visible immediately within the same job but not to parallel jobs already in flight on the same chunk — IJobEntity guarantees disjoint chunk access so this is safe.
- Mixing EnabledRefRW and ECB.SetComponentEnabled for the same component in the same frame produces ordering ambiguity.
Performance Notes
- Single bit write per entity; no archetype migration; no ECB playback.
- Optimal for bulk proximity-based culling — entire operation is one pass with no structural change cost.
- Burst-compiled toggle loop over 10,000 entities is effectively free compared to structural AddComponent/RemoveComponent equivalent.
Architecture Guidance
- Build query with IgnoreComponentEnabledState in OnCreate and cache it; pass explicitly to ScheduleParallel.
- For cross-entity toggles (entity A decides to enable entity B), use ECB.SetComponentEnabled — EnabledRefRW only works on the entity being executed.
- Producer/consumer pattern: producer writes EnabledRefRW; consumer queries WithAll<T> in [UpdateAfter] — no ECB needed.
Related Skills
[[enableable-component]], [[wave6-ecb-set-component-enabled]], [[wave6-with-disabled-query-filter]], [[wave6-zero-data-enableable-signal]]
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.