AGENT SKILL

ijobchunk-chunk-job

Process entities at the chunk level for maximum control — chunk-level change filtering, enable mask handling, direct pointer access, and explicit sort key management.

License 许可
MIT
最近更新
2026-07-25
Stars
★ 8
Forks
0

01 前言 Front Matter

name
ijobchunk-chunk-job
tags
[ "core", "jobs", "chunks" ]
metadata
{ "tier": 3, "internal-only": true }
description
Process entities at the chunk level for maximum control — chunk-level change filtering, enable mask handling, direct pointer access, and explicit sort key management.

03 SKILL.md 正文

IJobChunk — Chunk-Level Job

Intent

Process entities at the chunk level for maximum control — chunk-level change filtering, enable mask handling, direct pointer access, and explicit sort key management.

Use When

Change filtering via chunk.DidChange() is required. Enable mask must be processed explicitly. Direct unsafe pointer access is needed for maximum throughput. Chunk index is required for ECB.ParallelWriter.

Avoid When

IJobEntity provides sufficient control — IJobChunk adds significant boilerplate. Use IJobEntity for the common case; reserve IJobChunk for performance-critical or control-critical paths.

Senior Pattern

  • Implement IJobChunk on an unmanaged struct.
  • Acquire type handles in OnCreate or OnUpdate via SystemAPI.GetComponentTypeHandle<T>.
  • Update type handles each frame (they are version-stamped; stale handles cause wrong data).
  • Pass the query explicitly to job.ScheduleParallel(query, state.Dependency).
  • Use chunk.DidChange(ref typeHandle, lastSystemVersion) for change-filtered paths.
  • Chain result into state.Dependency.

Code Template

[BurstCompile]
struct TransformUpdateJob : IJobChunk
{
    public ComponentTypeHandle<LocalTransform> TransformHandle;
    [ReadOnly] public ComponentTypeHandle<Velocity> VelocityHandle;
    public uint LastSystemVersion;
    public float DeltaTime;

    public void Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex,
        bool useEnabledMask, in v128 chunkEnabledMask)
    {
        if (!chunk.DidChange(ref VelocityHandle, LastSystemVersion))
            return;

        var transforms = chunk.GetNativeArray(ref TransformHandle);
        var velocities = chunk.GetNativeArray(ref VelocityHandle);
        for (int i = 0; i < chunk.Count; i++)
        {
            var t = transforms[i];
            t.Position += velocities[i].Value * DeltaTime;
            transforms[i] = t;
        }
    }
}

[BurstCompile]
public void OnUpdate(ref SystemState state)
{
    state.Dependency = new TransformUpdateJob
    {
        TransformHandle = SystemAPI.GetComponentTypeHandle<LocalTransform>(),
        VelocityHandle  = SystemAPI.GetComponentTypeHandle<Velocity>(true),
        LastSystemVersion = state.LastSystemVersion,
        DeltaTime = SystemAPI.Time.DeltaTime
    }.ScheduleParallel(m_Query, state.Dependency);
}

Anti-Patterns

  • Caching type handles across frames without refreshing — handle version mismatch, reads wrong chunk data.
  • Not passing state.Dependency to Schedule — race condition.
  • Using IJobChunk where IJobEntity would suffice — unnecessary boilerplate.
  • Accessing chunk data without checking useEnabledMask when entities may be disabled.

Runtime Risks

  • Stale type handle: silent wrong data or access violation.
  • Missing dependency chain: data race.
  • Ignoring chunkEnabledMask when IEnableableComponent is in use: processing disabled entities.

Performance Notes

  • Direct pointer via chunk.GetRequiredComponentDataPtrRO/RW is the fastest possible access — avoids NativeArray bounds checks.
  • chunk.DidChange() enables skipping unchanged chunks entirely — major win for sparse-update scenarios.
  • Pair with chunk.Count for manual SIMD if needed.

Architecture Guidance

IJobChunk is the performance ceiling. Use it deliberately for transform pipelines, rendering integration, and high-throughput physics. Document why IJobEntity was insufficient.

04 同仓库更多 Skills

dyCuong03
dyCuong03/unity-agent-team

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.

★ 8
dyCuong03
dyCuong03/unity-agent-team

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.

★ 8
dyCuong03
dyCuong03/unity-agent-team

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.

★ 8
dyCuong03
dyCuong03/unity-agent-team

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.

★ 8
dyCuong03
dyCuong03/unity-agent-team

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.

★ 8
dyCuong03
dyCuong03/unity-agent-team

valid_skill

A complete, well-formed test fixture skill for SkillHub validation testing. Covers basic task routing and code review for Unity projects.

★ 8