Skip to content

Game.UI.InGame.DistrictsSection

Assembly: Game
Namespace: Game.UI.InGame

Type: class

Base: InfoSectionBase

Summary:
DistrictsSection is an in-game UI info section used to display and manage service districts associated with the currently selected entity (typically a service building). It integrates with the game's ECS (Unity.Entities) to query district data, listens to tool changes to maintain selection state, exposes UI bindings for toggling tools and removing districts, and writes out the district list for the UI. The class uses several tool systems (ToolSystem, AreaToolSystem, DefaultToolSystem, SelectionToolSystem) and Prefab/Config queries to toggle the district creation tool and selection tool. It also maintains a NativeList to collect district entities for the selected item and properly disposes it on destroy.


Fields

  • private ToolSystem m_ToolSystem
    Handles switching between and interacting with different editing tools (selection/default/area).

  • private AreaToolSystem m_AreaToolSystem
    Tool used for creating/editing area-based districts.

  • private DefaultToolSystem m_DefaultToolSystem
    Fallback/default tool used when no special tool is active.

  • private SelectionToolSystem m_SelectionToolSystem
    Tool used to select districts for the selected entity; selection type is set to SelectionType.ServiceDistrict when in use.

  • private EntityQuery m_ConfigQuery
    EntityQuery for retrieving area/district configuration (AreasConfigurationData) singleton.

  • private EntityQuery m_DistrictQuery
    EntityQuery for querying existing District components (excludes Temp).

  • private EntityQuery m_DistrictPrefabQuery
    EntityQuery for district prefabs (DistrictData) used to determine whether district prefabs are available.

  • private EntityQuery m_DistrictModifiedQuery
    EntityQuery described to detect created/deleted District events (used to request UI updates).

  • private ValueBinding<bool> m_Selecting
    Binding that reflects whether the selection tool (for service districts) is currently active for this UI section.

  • private NativeList<Entity> districts { get; set; }
    A NativeList used to accumulate district entities associated with the selectedEntity each frame. Allocated with Allocator.Persistent and disposed in OnDestroy.

  • private bool districtMissing { get; set; }
    Internal flag indicating whether any districts exist at all (based on m_DistrictQuery.IsEmptyIgnoreFilter).

Properties

  • protected override string group => "DistrictsSection"
    Property overriding the InfoSectionBase.group to provide the UI group name used for binding keys and bindings.

  • private NativeList<Entity> districts { get; set; }
    (See Fields) Exposed as a private auto-property; the list is cleared during Reset() and filled in OnProcess().

  • private bool districtMissing { get; set; }
    (See Fields) Written in OnUpdate() and serialized to UI via OnWriteProperties.

Constructors

  • public DistrictsSection()
    Default parameterless ctor. The real initialization happens in OnCreate.

Methods

  • protected override void Reset()
    Clears the internal districts NativeList. Called by the base lifecycle to reset per-frame/section state.

  • protected override void OnCreate()
    Initializes the section: grabs references to required systems from the Entity World (ToolSystem, AreaToolSystem, DefaultToolSystem, SelectionToolSystem), subscribes to tool change events, allocates the NativeList (Allocator.Persistent), creates EntityQueries (config, district, district prefab, district modification detection), and registers UI bindings/triggers:

  • "removeDistrict" -> RemoveServiceDistrict(Entity)
  • "toggleSelectionTool" -> ToggleSelectionTool()
  • "toggleDistrictTool" -> ToggleDistrictTool()
  • "selecting" ValueBinding for reflecting selection tool state

  • private void OnToolChanged(ToolBaseSystem tool)
    Handler for tool changes. Updates m_Selecting binding and resets selection state on the SelectionToolSystem when the section had been selecting but tool changed away from the service-district selection tool.

  • protected override void OnDestroy()
    Disposes the NativeList and calls base.OnDestroy to clean up.

  • private bool Visible()
    Returns whether the section should be visible: true if the selectedEntity has a ServiceDistrict buffer component and there are district prefabs available (m_DistrictPrefabQuery not empty). Used to set base.visible in OnUpdate.

  • protected override void OnPreUpdate()
    Checks m_DistrictModifiedQuery for changes (created/deleted events). If any are present, requests an update of the UI (RequestUpdate()) so the displayed district list stays in sync.

  • protected override void OnUpdate()
    Updates base.visible using Visible() and updates districtMissing based on whether m_DistrictQuery.IsEmptyIgnoreFilter.

  • protected override void OnProcess()
    Reads the ServiceDistrict buffer from selectedEntity and populates the private districts NativeList with the referenced district Entity values so they can be serialized to the UI.

  • public override void OnWriteProperties(IJsonWriter writer)
    Serializes the UI state for the frontend:

  • Writes boolean "districtMissing".
  • Writes an array "districts" where each entry contains:

    • "name": bound via m_NameSystem.BindName(writer, entity)
    • "entity": the Entity id This is used by the UI layer to render district entries.
  • public void RemoveServiceDistrict(Entity district)
    Removes the specified district Entity from the ServiceDistrict buffer on selectedEntity. If removal occurred, requests an Info UI update via m_InfoUISystem.RequestUpdate().

  • private void ToggleSelectionTool()
    Toggles the selection tool: if the active tool is the selection tool it switches back to default; otherwise sets the SelectionToolSystem to select ServiceDistricts for this selectedEntity and activates the selection tool.

  • private void ToggleDistrictTool()
    Toggles the area/district creation tool: if currently active, revert to default; otherwise reads the AreasConfigurationPrefab singleton to get the default district prefab and assigns it to the AreaToolSystem.prefab, then activates the AreaToolSystem.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();

    // bind to tool systems and create native list (simplified, matching the real class)
    m_ToolSystem = base.World.GetOrCreateSystemManaged<ToolSystem>();
    m_AreaToolSystem = base.World.GetOrCreateSystemManaged<AreaToolSystem>();
    m_DefaultToolSystem = base.World.GetOrCreateSystemManaged<DefaultToolSystem>();
    m_SelectionToolSystem = base.World.GetOrCreateSystemManaged<SelectionToolSystem>();

    // subscribe to tool changes so UI reflects selection tool state
    m_ToolSystem.EventToolChanged = (Action<ToolBaseSystem>)Delegate.Combine(
        m_ToolSystem.EventToolChanged,
        new Action<ToolBaseSystem>(OnToolChanged)
    );

    // allocate list used to collect district entities for serialization to UI
    districts = new NativeList<Entity>(Allocator.Persistent);

    // create entity queries and add UI bindings (remove, toggle tools, selecting flag)
}

Notes and tips: - Ensure districts NativeList is disposed (the class does this in OnDestroy). - The class relies on the existence of a ServiceDistrict dynamic buffer on the selected entity; it is not safe to call OnProcess or OnWriteProperties for entities without that buffer. - ToggleDistrictTool reads the areas configuration singleton; if your mod introduces custom area prefabs you can influence what is set as the area tool's prefab.