AGENT SKILL

baker-authoring-conversion

Convert designer-facing MonoBehaviour authoring data into unmanaged ECS components at subscene import time, with zero runtime conversion overhead.

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

01 前言 Front Matter

name
baker-authoring-conversion
tags
[ "core", "baking", "hybrid" ]
metadata
{ "tier": 3, "internal-only": true }
description
Convert designer-facing MonoBehaviour authoring data into unmanaged ECS components at subscene import time, with zero runtime conversion overhead.

03 SKILL.md 正文

Baker and Authoring Conversion

Intent

Convert designer-facing MonoBehaviour authoring data into unmanaged ECS components at subscene import time, with zero runtime conversion overhead.

Use When

Any ECS entity that originates from a Unity scene or prefab. All runtime components with inspector-configurable parameters require a Baker.

Avoid When

Runtime-only entities created entirely by code (e.g., spawned from a prefab) — no Baker needed for the spawned instance, only for the prefab source entity.

Senior Pattern

  • Declare a public MonoBehaviour (XyzAuthoring) with inspector-visible fields.
  • Nest a private class Baker : Baker<XyzAuthoring> inside it.
  • In Bake(): call GetEntity(TransformUsageFlags.X) to obtain the entity, then AddComponent(entity, new XyzComponent { ... }).
  • Convert editor units to runtime units at bake time (e.g., degrees → radians).
  • For prefab entity references: GetEntity(authoring.PrefabField, TransformUsageFlags.Dynamic).
  • Use TransformUsageFlags.Dynamic for moving entities, TransformUsageFlags.None for static/data-only entities.

Code Template

public class EnemyAuthoring : MonoBehaviour
{
    public float MoveSpeedDegreesPerSecond = 90f;
    public GameObject ProjectilePrefab;

    class Baker : Baker<EnemyAuthoring>
    {
        public override void Bake(EnemyAuthoring authoring)
        {
            var entity = GetEntity(TransformUsageFlags.Dynamic);
            AddComponent(entity, new EnemyMoveSpeed
            {
                RadiansPerSecond = math.radians(authoring.MoveSpeedDegreesPerSecond)
            });
            AddComponent(entity, new EnemyProjectileRef
            {
                Prefab = GetEntity(authoring.ProjectilePrefab, TransformUsageFlags.Dynamic)
            });
        }
    }
}

Anti-Patterns

  • Doing unit conversion at runtime in OnUpdate instead of at bake time — wasted per-frame cost.
  • Forgetting TransformUsageFlags.Dynamic on an entity that moves — missing LocalTransform at runtime, silent wrong behavior.
  • Not calling GetEntity for a referenced prefab GameObject — the prefab won't be baked into an entity prefab reference.
  • Storing state on the Baker instance — Baker is stateless per-bake, state is discarded after each call.
  • Calling Resources.Load or doing file I/O inside Bake — baking must be deterministic and dependency-tracked.

Runtime Risks

  • Missing TransformUsageFlags.Dynamic: entity has no LocalTransform, transform system skips it, entity doesn't move.
  • Unbaked prefab reference: entity field holds Entity.Null at runtime, causing silent spawn failures.

Performance Notes

Baker runs only at import time (or on re-import due to asset change). Zero runtime cost. Subscene data is loaded directly from disk — no re-baking at runtime.

Architecture Guidance

Baker is the sole bridge between the authoring world and the runtime world. Keep it simple — one Baker per authoring MonoBehaviour. Do all unit/format conversions here. Never let authoring types leak into runtime systems.

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