Entity Baking converts authored State Machine data into runtime ECS data. Use it when an Entity workflow needs to create agents from the same State Machine prefab, such as spawning 100 instances from one baked configuration.

Prerequisites

  • Meet the State Designer requirements.
  • Add and enable the State Machine component on the prefab.
  • Enable Start When Enabled for the automatic-start example below. For manual startup, such as authority gating, leave it disabled and follow Networking.
  • Have the State Machine prefab ready to assign to the spawner.

The Entity sample scene includes an EntitySpawner component with a working example.

Set up the baked prefab

  1. Create an EntitySpawner authoring component with a field for the State Machine prefab.
  2. Add a Baker that converts the assigned prefab to an entity prefab and stores it in SpawnData.
  3. Add the EntitySpawner component to an authoring GameObject in a SubScene and assign the State Machine prefab. Keep the SubScene loaded at runtime, for example with Auto Load Scene enabled. An authoring GameObject in an ordinary scene does not run this Baker.

Entity Spawner assigns the StateMachineAgent prefab to Spawn Prefab.

The Sub Scene component references the open EditorialSpawnerSubScene with Auto Load Scene enabled.

The enabled State Machine component on StateMachineAgent has Start When Enabled checked in Options.

The authoring component and its Baker can be defined as follows:

using Unity.Entities;
using UnityEngine;

public struct SpawnData : IComponentData
{
    [Tooltip("The entity prefab that should be spawned.")]
    public Entity Prefab;
}

public class EntitySpawner : MonoBehaviour
{
    [Tooltip("A reference to the state machine prefab.")]
    [SerializeField] protected GameObject m_SpawnPrefab;

    /// <summary>
    /// Bakes the spawn data.
    /// </summary>
    private class Baker : Baker<EntitySpawner>
    {
        /// <summary>
        /// Bakes the data.
        /// </summary>
        /// <param name="authoring">The parent authoring component.</param>
        public override void Bake(EntitySpawner authoring)
        {
            if (authoring.m_SpawnPrefab == null) {
                return;
            }
            var entity = GetEntity(TransformUsageFlags.Dynamic);
            var entityPrefab = GetEntity(authoring.m_SpawnPrefab, TransformUsageFlags.Dynamic);
            AddComponent(entity, new SpawnData
            {
                Prefab = entityPrefab,
            });
        }
    }
}

Spawn the entities

Create a system that waits for SpawnData, runs once, and instantiates the baked State Machine prefab:

using Unity.Collections;
using Unity.Entities;

public partial struct EntitySpawnerSystem : ISystem
{
    /// <summary>
    /// Sets the system requirements.
    /// </summary>
    /// <param name="state">The current SystemState.</param>
    private void OnCreate(ref SystemState state)
    {
        state.RequireForUpdate<SpawnData>();
    }

    /// <summary>
    /// Spawns the entities.
    /// </summary>
    /// <param name="state">The current SystemState.</param>
    private void OnUpdate(ref SystemState state)
    {
        state.Enabled = false;

        var spawner = SystemAPI.GetSingleton<SpawnData>();
        using var entities = state.EntityManager.Instantiate(spawner.Prefab, 100, Allocator.Temp);

        // Any custom setup for each entity. This can include adding new components, enabling components, setting entity data, etc.
    }
}

How it runs

During baking, the EntitySpawner Baker stops if no prefab is assigned. Otherwise, it converts the assigned State Machine prefab to an entity prefab and stores that reference in SpawnData.

The EntitySpawnerSystem waits until SpawnData exists. On its first update, it disables itself and instantiates 100 entities from the baked prefab. The returned entity collection can then be used for per-agent setup such as adding components, enabling components, or setting entity data.

In Play Mode:

Run the scene containing the configured EntitySpawner and verify that:

  • 100 entity instances are created from the assigned State Machine prefab.
  • The spawn happens once rather than on every system update.
  • Each created entity can receive any additional setup required by the project.

If no entities are created, first confirm that the authoring GameObject is in a loaded SubScene, a prefab is assigned, and the State Machine component is enabled when baked. Start When Enabled controls automatic execution of the baked graph; when it is disabled, startup must be requested after runtime initialization as shown on Networking.

Related: