AGENT SKILL

wave8-baker-dependency-registration

Register all external asset and component dependencies in a Baker so that incremental baking re-triggers correctly when referenced objects change.

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

01 前言 Front Matter

name
wave8-baker-dependency-registration
tags
[ "baking", "antipattern", "debug" ]
metadata
{ "tier": 3, "internal-only": true }
description
Register all external asset and component dependencies in a Baker so that incremental baking re-triggers correctly when referenced objects change.

03 SKILL.md 正文

Baker Dependency Registration — DependsOn

Intent

Register all external asset and component dependencies in a Baker so that incremental baking re-triggers correctly when referenced objects change.

Use When

  • A Baker reads data from child GameObjects, referenced assets (ScriptableObjects, Textures, etc.), or other components not on the primary authoring GameObject
  • Any data source that is not a direct serialized field on the primary authoring MonoBehaviour

Avoid When

  • The Baker only reads from the primary authoring MonoBehaviour's own serialized fields — Unity registers those automatically
  • The dependency is read via Baker API methods (GetComponent, GetComponentInChildren) which have their own automatic tracking

Senior Pattern

public class EnemyConfigBaker : Baker<EnemyConfigAuthoring>
{
    public override void Bake(EnemyConfigAuthoring authoring)
    {
        // Reading a referenced asset — MUST call DependsOn:
        DependsOn(authoring.ConfigAsset);  // re-bake if ScriptableObject changes

        // Reading child component via raw Unity API — MUST call DependsOn:
        var childRenderer = authoring.GetComponentInChildren<MeshRenderer>();
        DependsOn(childRenderer);          // re-bake if childRenderer changes

        // Reading via Baker API — automatically tracked, DependsOn not needed:
        var rb = GetComponent<Rigidbody>();     // auto-tracked
        var children = GetChildren();           // auto-tracked

        var entity = GetEntity(TransformUsageFlags.Dynamic);
        AddComponent(entity, new EnemyConfig
        {
            Speed        = authoring.ConfigAsset != null ? authoring.ConfigAsset.Speed : 0f,
            HasRenderer  = childRenderer != null
        });
    }
}

Auto-Tracked vs Manual DependsOn

Access method Auto-tracked? DependsOn needed?
Direct serialized field on authoring Yes No
GetComponent<T>() (Baker API) Yes No
authoring.someField.GetComponent<T>() (raw Unity API) No Yes
Referenced ScriptableObject / asset No Yes
authoring.transform.GetChild(0).GetComponent<T>() No Yes
Static fields / singletons Never Cannot register

Anti-Patterns

  • Accessing authoring.transform.GetChild(0).GetComponent<T>() without DependsOn — child change not detected; stale bake with no error.
  • Reading a ScriptableObject field without DependsOn — asset change does not trigger re-bake.
  • Using static fields or singletons inside Baker.Bake — untracked dependencies; non-deterministic baking results.
  • Over-registering DependsOn for fields that are already auto-tracked — causes unnecessary bake invalidation on every change.

Runtime Risks

  • Missing DependsOn causes stale baked data in SubScenes — entity has wrong data until manual re-import.
  • This is a silent correctness bug with no runtime error — only manifests as wrong game behavior after source asset changes.
  • In Editor Play mode, stale bakes are invisible until SubScene reimport; can cause intermittent test failures.

Performance Notes

  • DependsOn adds incremental bake tracking overhead proportional to the number of registered dependencies.
  • Only register dependencies that actually vary between bakes.
  • Unnecessary DependsOn calls cause cascading bake invalidations across the SubScene.

Architecture Guidance

  • Rule: if Baker.Bake reads it via raw Unity API and it is not a direct serialized field on the authoring component, call DependsOn.
  • Baker-registered dependencies are the baking system's incremental cache invalidation mechanism — correctness of baked data depends on complete dependency registration.

Related Skills

[[baker-authoring-conversion]], [[baker-depends-on]], [[baking-system]], [[baking-type-component]]

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