Skip to content

Game.Prefabs.UIInitializeSystem

Assembly: Game
Namespace: Game.Prefabs

Type: class (CompilerGenerated)

Base: GameSystemBase

Summary:
UIInitializeSystem is a small cleanup/initialization ECS system that runs against prefab entities. It finds prefabs that have been marked Deleted and that also contain UI-related components (UIObjectData or UIAssetCategoryData) and removes any references to those prefabs from runtime buffers (UIGroupElement and UnlockRequirement) so UI menus/groups do not keep dangling references. The system also exposes an iterator property to enumerate policy prefabs via the PrefabSystem.


Fields

  • private EntityQuery m_PrefabQuery
    Query used to find prefab entities that match: have PrefabData and Deleted, and have either UIObjectData or UIAssetCategoryData. Required for the system's update (RequireForUpdate is called with this query in OnCreate).

  • private PrefabSystem m_PrefabSystem
    Cached reference to the game's PrefabSystem (retrieved from World in OnCreate). Used by the policies enumerator to resolve PrefabData -> PolicyPrefab.

  • private EntityQuery m_PolicyQuery
    Query used to find prefabs that represent policies (have PrefabData and PolicyData). Used by the policies property to enumerate PolicyPrefab instances.

  • private TypeHandle __TypeHandle
    Instance of the compiler-generated nested TypeHandle struct used to store Entity/Component type handles for chunk access (EntityTypeHandle, ComponentTypeHandle, ComponentTypeHandle).

  • (nested) private struct TypeHandle

  • [ReadOnly] public EntityTypeHandle __Unity_Entities_Entity_TypeHandle
    Entity type handle used to read Entity values from chunks.
  • [ReadOnly] public ComponentTypeHandle<UIObjectData> __Game_Prefabs_UIObjectData_RO_ComponentTypeHandle
    Read-only handle to access UIObjectData components in chunks.
  • [ReadOnly] public ComponentTypeHandle<UIAssetCategoryData> __Game_Prefabs_UIAssetCategoryData_RO_ComponentTypeHandle
    Read-only handle to access UIAssetCategoryData components in chunks.
    The nested struct provides an __AssignHandles method which populates these handles from a SystemState.

Properties

  • public IEnumerable<PolicyPrefab> policies { get; }
    Lazily enumerates PolicyPrefab instances for every PrefabData returned by m_PolicyQuery. Implementation notes:
  • If m_PolicyQuery is not empty, it reads a NativeArray (Allocator.TempJob) and yields a PolicyPrefab by calling m_PrefabSystem.GetPrefab(prefabData).
  • The NativeArray is explicitly disposed after enumeration.
  • Consumers should enumerate carefully (the iterator allocates temporary native memory and expects the PrefabSystem to be available).

Constructors

  • public UIInitializeSystem()
    Default constructor (marked CompilerGenerated). The system relies on OnCreate to perform initialization.

Methods

  • [Preserve] protected override void OnCreate()
  • Calls base.OnCreate().
  • Caches PrefabSystem via base.World.GetOrCreateSystemManaged().
  • Builds two EntityQuery instances:
    • m_PrefabQuery: All = { PrefabData (ReadOnly), Deleted (ReadOnly) }, Any = { UIObjectData (ReadOnly), UIAssetCategoryData (ReadOnly) }.
    • m_PolicyQuery: All = { PrefabData (ReadOnly), PolicyData (ReadOnly) }.
  • Calls RequireForUpdate(m_PrefabQuery) so the system only updates when that query has matching entities.

  • [Preserve] protected override void OnUpdate()

  • Converts m_PrefabQuery to an ArchetypeChunk array (Allocator.TempJob) and iterates chunks.
  • Uses InternalCompilerInterface.GetEntityTypeHandle and GetComponentTypeHandle to obtain handles from the cached TypeHandle and the system's CheckedStateRef.
  • Calls CompleteDependency() before reading chunk data to ensure safety with jobs.
  • For each chunk:
    • Reads Entity array, UIObjectData array, and UIAssetCategoryData array from the chunk.
    • For UIObjectData entries: tries to get dynamic buffers for the group (UIGroupElement) and unlock requirements (UnlockRequirement) using EntityManager.TryGetBuffer and, if present, calls RemoveFrom to remove references to the prefab entity.
    • For UIAssetCategoryData entries: similar logic but targets the menu buffer (UIGroupElement) and its unlock requirements buffer.
  • Ensures nativeArray.Dispose(base.Dependency) in a finally block.

  • private void RemoveFrom(Entity entity, DynamicBuffer<UIGroupElement> uiGroupElements)

  • Iterates the UIGroupElement buffer and removes the first entry where element.m_Prefab == entity.
  • Removes with RemoveAtSwapBack(i) and breaks after first removal.

  • private void RemoveFrom(Entity entity, DynamicBuffer<UnlockRequirement> unlockRequirements)

  • Iterates the UnlockRequirement buffer and removes the first entry where requirement.m_Prefab == entity.
  • Removes with RemoveAtSwapBack(i) and breaks after first removal.

  • [MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)

  • Compiler-generated helper that currently only creates and disposes an EntityQueryBuilder(Allocator.Temp). Present for compatibility with generated code patterns.

  • protected override void OnCreateForCompiler()

  • Compiler hook used to assign queries and type handles for ahead-of-time generated code. Calls __AssignQueries(ref base.CheckedStateRef) and __TypeHandle.__AssignHandles(ref base.CheckedStateRef) in addition to base.OnCreateForCompiler().

  • private void TypeHandle.__AssignHandles(ref SystemState state)

  • Populates the EntityTypeHandle and ComponentTypeHandle fields via state.GetEntityTypeHandle() and state.GetComponentTypeHandle(isReadOnly: true).

Implementation notes and caveats: - The system operates on the Entity Component System (ECS) runtime and uses temporary native memory (Allocator.TempJob). Ensure enumeration and lifetime are handled correctly to avoid leaks or crashes. - RemoveFrom methods use RemoveAtSwapBack, so buffer order is not preserved — acceptable for simple membership lists but important to be aware of. - The class is marked CompilerGenerated; some members and patterns (TypeHandle, __AssignQueries, OnCreateForCompiler) are typical artifacts of code generation and may be present for compatibility with DOTS/ECS compiler transforms. - OnUpdate calls CompleteDependency() to ensure main-thread safety before reading managed data (EntityManager.TryGetBuffer, PrefabSystem calls, etc.). If you modify to add jobs, respect dependencies.

Usage Example

// Example: enumerate policy prefabs from another system (safely, on main thread)
[Preserve]
protected override void OnUpdate()
{
    var uiInit = World.GetExistingSystemManaged<Game.Prefabs.UIInitializeSystem>();
    if (uiInit != null)
    {
        foreach (var policyPrefab in uiInit.policies)
        {
            // Do something with policyPrefab, e.g. register it to a UI list
        }
    }
}