Skip to content

Game.Prefabs.AnimatedPrefabSystem

Assembly: Game (in-game managed assembly; exact assembly name not present in source)
Namespace: Game.Prefabs

Type: class

Base: GameSystemBase

Summary:
AnimatedPrefabSystem is a GameSystemBase that initializes animation clip buffers and root-motion data for CharacterStyle prefabs. On update it iterates newly created prefab entities (those with Created + PrefabData + CharacterStyleData), reads the CharacterStyle prefab definition, populates DynamicBuffer and DynamicBuffer for each prefab instance, computes animation lengths and frame rates, fills activity / layer masks, links prop-layer clips to body clips when applicable, and computes speed ranges for move animations. It also contains logic to normalize and cleanup root-motion data (removing the first-frame offset and normalizing rotations to the horizontal plane). The system obtains references to the PrefabSystem and AnimatedSystem to read prefab assets and resolve animated prop IDs.


Fields

  • private struct TypeHandle
    Contains cached Entity/Component/Buffer type handles used for efficient chunk iteration:
  • __Unity_Entities_Entity_TypeHandle: EntityTypeHandle (read-only)
  • __Game_Prefabs_PrefabData_RO_ComponentTypeHandle: ComponentTypeHandle (read-only)
  • __Game_Prefabs_CharacterStyleData_RW_ComponentTypeHandle: ComponentTypeHandle (read/write)
  • __Game_Prefabs_AnimationClip_RW_BufferTypeHandle: BufferTypeHandle
  • __Game_Prefabs_AnimationMotion_RW_BufferTypeHandle: BufferTypeHandle
  • Provides __AssignHandles(ref SystemState) to initialize these handles from a SystemState.

  • private PrefabSystem m_PrefabSystem
    Reference to the game's PrefabSystem used to resolve PrefabData into concrete prefab assets (CharacterStyle).

  • private AnimatedSystem m_AnimatedSystem
    Reference to the AnimatedSystem used to resolve animated prop IDs (GetPropID).

  • private EntityQuery m_PrefabQuery
    EntityQuery used to find prefab entities with Created + PrefabData + CharacterStyleData that need initialization.

  • private TypeHandle __TypeHandle
    Instance of the nested TypeHandle struct that stores the type handles for chunk iteration.

Properties

  • None (no public properties on this type)

Constructors

  • public AnimatedPrefabSystem()
    Default constructor. The system relies on OnCreate to initialize subsystem references and entity queries.

Methods

  • protected override void OnCreate()
    Initializes the system: caches PrefabSystem and AnimatedSystem via World.GetOrCreateSystemManaged(), builds the entity query for Created + PrefabData + CharacterStyleData and calls RequireForUpdate(m_PrefabQuery) so the system runs only when there are matching entities.

  • protected override void OnUpdate()
    Main work happens here. Steps performed:

  • Converts m_PrefabQuery into an ArchetypeChunk array (Allocator.Temp) and sets up Entity/Component/Buffer type handles via the stored TypeHandle and InternalCompilerInterface.
  • Calls CompleteDependency() to ensure prior jobs are finished.
  • Iterates each ArchetypeChunk and each entity in the chunk:
    • Resolves CharacterStyle prefab from PrefabData via m_PrefabSystem.GetPrefab().
    • Resets and populates CharacterStyleData fields (activity masks, rest-pose clip index, bone/shape counts).
    • Resizes and fills DynamicBuffer based on prefab.m_Animations:
    • Initializes clip metadata: layer mapping (Colossal.Animations.AnimationLayer -> internal AnimationLayer), prop IDs, root motion bone index, prop clip linking, activity/layer masks.
    • Computes animation lengths and frame rates differently for RandomLoop/Move vs other playback modes to avoid 1-frame artifacts and to align to 16-frame boundaries for non-looping clips.
    • Tracks move animation motion speeds to compute speed ranges.
    • Resizes and fills DynamicBuffer by copying and cleaning up CharacterStyle.AnimationMotion arrays via CleanUpRootMotion.
    • Builds a temporary dictionary keyed by (ActivityType, AnimationType, AnimatedPropID) to map prop-layer clips to corresponding body clips where appropriate.
  • Disposes the temporary native archetype chunk array.
  • Notes:

    • Allocator.Temp is used for the archetype chunk array and must be disposed (the code does this).
    • The method constructs a dictionary for prop-to-body mapping within the scope of the update to avoid repeated lookups.
  • private void CleanUpRootMotion(CharacterStyle.AnimationMotion[] source, NativeArray<AnimationMotion> target)
    Copies root-motion entries from the managed CharacterStyle.AnimationMotion[] to the native AnimationMotion buffer and performs normalization:

  • Converts start/end offsets/rotations.
  • Subtracts the first-frame offsets/rotations so motions are relative to the first frame.
  • Forces Y-offsets to zero (flatten to ground plane).
  • Recomputes start/end rotations to be horizontal-only using LookRotationSafe on the forward vectors with up vector.
  • Used to prepare consistent root-motion data for runtime sampling and movement speed calculation.

  • [MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
    Compiler helper that currently builds (and immediately disposes) an EntityQueryBuilder with Allocator.Temp. Present for compiled-system boilerplate and query assignment.

  • protected override void OnCreateForCompiler()
    Compiler-time helper that calls __AssignQueries and delegates to __TypeHandle.__AssignHandles(ref SystemState) to prepare handles when the system is created by the compiler/runtime.

  • private void TypeHandle.__AssignHandles(ref SystemState state)
    (See TypeHandle field) Assigns all Entity/Component/Buffer type handles from the provided SystemState.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // Typical initialization used by the system:
    m_PrefabSystem = base.World.GetOrCreateSystemManaged<PrefabSystem>();
    m_AnimatedSystem = base.World.GetOrCreateSystemManaged<AnimatedSystem>();
    m_PrefabQuery = GetEntityQuery(
        ComponentType.ReadOnly<Created>(),
        ComponentType.ReadOnly<PrefabData>(),
        ComponentType.ReadWrite<CharacterStyleData>());
    RequireForUpdate(m_PrefabQuery);
}

Additional notes / tips for modders: - This system runs only for prefabs that have just been Created and have a PrefabData + CharacterStyleData component — it initializes per-instance animation buffers based on prefab asset data. - The system uses Allocator.Temp for short-lived native arrays; if you copy/adapt logic, ensure you dispose temporary native collections appropriately and avoid long-lived Temp allocations. - CleanUpRootMotion flattens motion to ground-plane and makes motion relative to the first-frame transform; if you add new root-motion handling, be aware of these assumptions (Y is zeroed, rotations reoriented horizontally). - The system links prop-layer animations to body-layer counterparts via a dictionary keyed by (ActivityType, AnimationType, AnimatedPropID) — when adding custom props/animations ensure your AnimatedPropID resolution (AnimatedSystem.GetPropID) matches expectations.