Skip to content

Game.Prefabs.ZoneSystem

Assembly:
Namespace: Game.Prefabs

Type: class

Base: Game.Common.GameSystemBase

Summary:
Manages zone prefabs and their runtime data for the zone rendering/system. ZoneSystem tracks creation and removal of zone entities, maps ZoneType indices to prefab Entities, maintains per-zone fill/edge color arrays that are uploaded to shaders, and updates color variants (occupied/selected/visible). It uses Unity.Entities queries and a NativeList to store prefab Entity references and coordinates with PrefabSystem to read ZonePrefab data. It also handles editor-mode differences (alpha/visibility) and synchronizes with job readers via a JobHandle.


Fields

  • private EntityQuery m_CreatedQuery
    Query that detects newly created or deleted entities that have ZoneData and PrefabData (used to initialize or remove zone prefabs).

  • private EntityQuery m_PrefabQuery
    Query for all active zone-prefab entities (ZoneData + PrefabData, not Deleted) used for full color updates.

  • private PrefabSystem m_PrefabSystem
    Cached reference to the PrefabSystem used to look up ZonePrefab data from PrefabData.

  • private NativeList<Entity> m_ZonePrefabs
    A NativeList mapping zone type indices -> prefab Entity. Index 0 is unused; indices correspond to ZoneType.m_Index.

  • private int m_ZoneFillColors
    Shader property ID for the global fill colors array ("colossal_ZoneFillColors").

  • private int m_ZoneEdgeColors
    Shader property ID for the global edge colors array ("colossal_ZoneEdgeColors").

  • private bool m_IsEditorMode
    Tracks whether the game is currently in editor mode; affects alpha/visibility of zone colors.

  • private bool m_UpdateColors
    Flag indicating a full color array update is required on next update.

  • private bool m_RemovedZones
    Flag set when zone entries are removed (used to find free indices when assigning new zone indices).

  • private Vector4[] m_FillColorArray
    Managed array of fill colors uploaded to the shader (length 1023 by default).

  • private Vector4[] m_EdgeColorArray
    Managed array of edge colors uploaded to the shader (length 1023 by default).

  • private JobHandle m_PrefabsReaders
    Combined JobHandle of readers that must complete before accessing prefab data. Used to synchronize with jobs that read prefabs.

  • private TypeHandle __TypeHandle
    Internal struct holding cached Entity/ComponentTypeHandle instances for fast chunk access (see nested TypeHandle).

  • private struct TypeHandle
    Contains cached ECS type handles:

  • EntityTypeHandle __Unity_Entities_Entity_TypeHandle
  • ComponentTypeHandle<Deleted> __Game_Common_Deleted_RO_ComponentTypeHandle
  • ComponentTypeHandle<PrefabData> __Game_Prefabs_PrefabData_RO_ComponentTypeHandle
  • ComponentTypeHandle<ZoneData> __Game_Prefabs_ZoneData_RW_ComponentTypeHandle
  • ComponentTypeHandle<ZoneData> __Game_Prefabs_ZoneData_RO_ComponentTypeHandle
    and an __AssignHandles(ref SystemState state) method that populates those handles from a SystemState.

Properties

  • None (no public properties declared)

Constructors

  • public ZoneSystem()
    Default parameterless constructor (preserved). Primary initialization happens in OnCreate.

Methods

  • protected override void OnCreate() : System.Void
    Initializes the system: caches PrefabSystem, builds ECS queries (created/deleted and prefab queries), allocates NativeList and color arrays, and resolves shader property IDs. Called once when the system is created.

  • protected override void OnDestroy() : System.Void
    Disposes resources: completes any outstanding m_PrefabsReaders, disposes m_ZonePrefabs, then calls base.OnDestroy.

  • protected override void OnUpdate() : System.Void
    Main update loop:

  • Ensures m_PrefabsReaders is completed before reading.
  • If m_CreatedQuery is not empty, calls InitializeZonePrefabs to process newly created or deleted zone entities.
  • If m_UpdateColors is set, calls UpdateZoneColors to rebuild shader color arrays.

  • protected override void OnGamePreload(Purpose purpose, GameMode mode) : System.Void
    Called during preload. Detects editor-mode changes and, when toggling editor mode, completes pending readers, flips m_IsEditorMode and marks colors for update if prefabs exist.

  • private void InitializeZonePrefabs() : System.Void
    Processes archetype chunks from m_CreatedQuery to:

  • Handle Deleted entries: clear m_ZonePrefabs mapping for removed zone indices and set m_RemovedZones.
  • For newly created zone-prefab entities: look up ZonePrefab via PrefabSystem, initialize ZoneData fields (AreaType, ZoneType index, min/max heights, flags), assign/expand m_ZonePrefabs to store the entity at the zone type index, call UpdateZoneColors for the prefab to set shader arrays.
  • After processing, uploads m_FillColorArray and m_EdgeColorArray to the shader via Shader.SetGlobalVectorArray.

Notes: This method completes dependencies (CompleteDependency) and uses low-level chunk access via TypeHandle handles. It allocates the archetype chunk array with Allocator.TempJob.

  • private int GetNextIndex() : System.Int32
    Returns the next available zone type index:
  • If m_RemovedZones is true tries to reuse the first Entity.Null slot (search starts at 1).
  • Otherwise returns max(1, m_ZonePrefabs.Length) (ensuring at least index 1). Resets m_RemovedZones when no null slot is found.

  • private void UpdateZoneColors() : System.Void
    Rebuilds color arrays for all active zone prefabs:

  • Iterates m_PrefabQuery chunks, reads PrefabData and ZoneData, looks up ZonePrefab, and calls UpdateZoneColors(ZonePrefab, ZoneData) for each entry.
  • Uploads updated arrays to shader with Shader.SetGlobalVectorArray.
  • Completes dependencies before reading.

  • private void UpdateZoneColors(ZonePrefab zonePrefab, ZoneData zoneData) : System.Void
    Computes the fill and edge color entries for a given prefab/zone:

  • Extracts base colors from ZonePrefab (m_Color and m_Edge).
  • Computes occupied/selected variants via GetZoneColors.
  • Calculates color indices using ZoneUtils.GetColorIndex with appropriate CellFlags (Visible, Occupied, Selected).
  • Adjusts alpha for editor mode.
  • Writes the resulting colors into m_FillColorArray and m_EdgeColorArray at the computed indices.

  • private void GetZoneColors(Color color, out Color occupied, out Color selected) : System.Void
    Given a base color, computes two variants:

  • occupied: HSV with same H, reduced S (S*0.75), same V; alpha halved.
  • selected: HSV with increased saturation (clamped) and alpha boosted (up to interpolation toward 1.0). Uses Color.RGBToHSV and Color.HSVToRGB plus math.* helpers.

  • public ZonePrefabs GetPrefabs() : ZonePrefabs
    Returns a ZonePrefabs wrapper constructed from m_ZonePrefabs.AsArray() (read-only snapshot of the native list).

  • public void AddPrefabsReader(JobHandle handle) : System.Void
    Combines the supplied handle with the internal m_PrefabsReaders via JobHandle.CombineDependencies. External systems should call this to register they are reading prefab data so the ZoneSystem can synchronize.

  • public Entity GetPrefab(ZoneType zoneType) : Entity
    Returns the Entity associated with the given zoneType index (or Entity.Null if index out of range). Safe, bounds-checked lookup into m_ZonePrefabs.

  • private void __AssignQueries(ref SystemState state) : System.Void
    Internal compiler helper that sets up queries (empty implementation here, used at compile time).

  • protected override void OnCreateForCompiler() : System.Void
    Compiler helper: assigns queries and type handles for the system by calling __AssignQueries and the TypeHandle.__AssignHandles. Used by generated/compiled code paths.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    m_PrefabSystem = base.World.GetOrCreateSystemManaged<PrefabSystem>();
    m_CreatedQuery = GetEntityQuery(new EntityQueryDesc
    {
        All = new ComponentType[2]
        {
            ComponentType.ReadWrite<ZoneData>(),
            ComponentType.ReadOnly<PrefabData>()
        },
        Any = new ComponentType[2]
        {
            ComponentType.ReadOnly<Created>(),
            ComponentType.ReadOnly<Deleted>()
        }
    });
    m_PrefabQuery = GetEntityQuery(ComponentType.ReadOnly<ZoneData>(),
                                   ComponentType.ReadOnly<PrefabData>(),
                                   ComponentType.Exclude<Deleted>());
    m_ZonePrefabs = new NativeList<Entity>(Allocator.Persistent);
    m_FillColorArray = new Vector4[1023];
    m_EdgeColorArray = new Vector4[1023];
    m_ZoneFillColors = Shader.PropertyToID("colossal_ZoneFillColors");
    m_ZoneEdgeColors = Shader.PropertyToID("colossal_ZoneEdgeColors");
}

Notes and tips: - ZoneSystem uses unmanaged NativeList and must Dispose() it in OnDestroy to avoid leaks (already handled). - When calling GetPrefab or reading prefab-related data, ensure you respect job synchronization: use AddPrefabsReader to register readers or ensure ZoneSystem.CompleteDependency (it calls CompleteDependency internally). - Shader global arrays are updated via Shader.SetGlobalVectorArray; array length is fixed to 1023 in the implementation — changing that requires matching shader support.