Game.UI.InGame.TransportUIUtils
Assembly: Game
Namespace: Game.UI.InGame
Type: static class
Base: System.Object
Summary:
Utility helpers for building and querying transport line UI data from ECS entities. This static class provides methods to:
- Count filtered transport lines,
- Build UI-friendly transport line representations (UITransportLineData) from route entities,
- Gather route metadata such as stop count, route length and vehicle/cargo counts,
- Produce a sorted NativeArray of transport lines for UI presentation.
The methods operate directly against the Unity.Entities EntityManager, DynamicBuffers and component data; they assume the provided entities are route/vehicle related entities with the expected components (Route, PrefabRef, RouteWaypoint, RouteSegment, RouteVehicle, etc.). Some methods allocate NativeArray instances that the caller is responsible for disposing.
Fields
- None
This static utility class does not declare instance or static fields.
Properties
- None
Constructors
- None (static class — no constructors available)
Methods
public static int CountLines(NativeArray<UITransportLineData> lines, TransportType type, bool cargo = false)
Counts how many items in the provided NativeArray of UITransportLineData match the specified TransportType and cargo flag.
Parameters:- lines: NativeArray of UITransportLineData to scan (read-only expected).
- type: TransportType to match.
-
cargo: If true, only count cargo transport lines; otherwise count non-cargo lines.
Returns: Number of matching lines.
Notes: This performs a simple linear scan. -
public static NativeArray<UITransportLineData> GetSortedLines(EntityQuery query, EntityManager entityManager, PrefabSystem prefabSystem)
Builds UITransportLineData for every entity matching the provided EntityQuery, sorts them, and returns a NativeArray containing the sorted data.
Parameters: - query: An EntityQuery that selects route entities (e.g., transport line entities).
- entityManager: EntityManager used to read component data and buffers.
- prefabSystem: PrefabSystem used to resolve TransportLinePrefab and other prefab data.
Returns: A NativeArray(allocated with Allocator.Temp). The caller is responsible for disposing the returned NativeArray when finished.
Behavior and notes: - Internally calls query.ToEntityArray(Allocator.TempJob) to get matching entities, maps each entity via BuildTransportLine, sorts the resulting array via NativeArray.Sort(), disposes the temporary entity array, and returns the created sorted array.
-
Because the returned array uses Allocator.Temp, it is intended for short-lived usage (same frame). Dispose it promptly to avoid leaks.
-
public static UITransportLineData BuildTransportLine(Entity entity, EntityManager entityManager, PrefabSystem m_PrefabSystem)
Constructs a UITransportLineData instance for a given transport route entity by reading required components and computing derived values (stop count, vehicles, cargo/capacity, route length, usage, schedule, visibility).
Parameters: - entity: The route entity to build UI data for.
- entityManager: EntityManager used to access component data and buffers.
- m_PrefabSystem: PrefabSystem used to resolve TransportLinePrefab and transport line prefab data.
Returns: A UITransportLineData initialized from the route entity state.
Requirements and notes: - Expects the entity to have Route, PrefabRef and TransportLineData components (calls entityManager.GetComponentData on them).
- Reads Color and HiddenRoute to determine visibility.
- Computes stop count via GetStopCount, route vehicle counts and cargo/capacity via GetRouteVehiclesCount, and route length via GetRouteLength.
-
Determines schedule and active flags using RouteUtils.CheckOption on the Route component.
-
public static int GetStopCount(EntityManager entityManager, Entity entity)
Counts the number of valid transport stops referenced by the route's RouteWaypoint buffer. Taxi stands are excluded.
Parameters: - entityManager: EntityManager to query components.
- entity: Route entity whose RouteWaypoint buffer will be inspected.
Returns: Number of stops counted.
Notes: - Iterates the RouteWaypoint dynamic buffer on the route entity.
-
For each waypoint, uses entityManager.TryGetComponent
() to find the connected entity and then checks for Game.Routes.TransportStop while excluding TaxiStand. -
public static float GetRouteLength(EntityManager entityManager, Entity entity)
Calculates the total route length by summing PathInformation.m_Distance for each RouteSegment referenced by the route's buffer.
Parameters: - entityManager: EntityManager to read PathInformation components.
- entity: Route entity providing RouteSegment buffer.
Returns: Total route distance as float (sum of segment distances).
Notes: -
Uses TryGetComponent
() on each segment entity; missing PathInformation entries are skipped. -
public static int GetRouteVehiclesCount(EntityManager entityManager, Entity entity, ref int cargo, ref int capacity)
Counts vehicles assigned to a route and aggregates cargo and capacity values.
Parameters: - entityManager: EntityManager to read RouteVehicle buffer and vehicle components/buffers.
- entity: Route entity whose RouteVehicle buffer will be inspected.
- cargo (ref): Accumulator for total current cargo/passengers; updated by the method.
- capacity (ref): Accumulator for total capacity across vehicles; updated by the method.
Returns: Number of RouteVehicle entries (route vehicles count).
Notes: -
For each RouteVehicle, calls AddCargo to account for nested layout elements or direct vehicle entities to compute cargo and capacity.
-
private static void AddCargo(EntityManager entityManager, Entity entity, ref int cargo, ref int capacity)
Helper that attempts to read a LayoutElement buffer on the provided entity. If present, iterates LayoutElement entries and processes each vehicle inside; otherwise treats the passed entity itself as a vehicle.
Parameters: - entityManager: EntityManager to access buffers/components.
- entity: Either a layout container or a vehicle entity.
- cargo (ref): Accumulator for current cargo/passengers.
-
capacity (ref): Accumulator for capacity.
-
private static void AddVehicleCargo(EntityManager entityManager, Entity entity, ref int cargo, ref int capacity)
Reads the vehicle's PrefabRef to determine whether it is a public transport or cargo vehicle and updates cargo and capacity accordingly.
Parameters: - entityManager: EntityManager to read the vehicle's prefab and buffers.
- entity: Vehicle entity to inspect.
- cargo (ref): Accumulator for current cargo/passengers/resources; updated in-place.
- capacity (ref): Accumulator for vehicle capacity; updated in-place.
Behavior and notes: - Retrieves PrefabRef from the vehicle entity. If absent, returns without changes.
- If the referenced prefab has PublicTransportVehicleData:
- If the vehicle has a Passenger buffer, adds buffer.Length to cargo.
- Adds PublicTransportVehicleData.m_PassengerCapacity to capacity.
- Else if the referenced prefab has CargoTransportVehicleData:
- If the vehicle has a Resources buffer, sums Resources.m_Amount into cargo.
- Adds CargoTransportVehicleData.m_CargoCapacity to capacity.
- Silently returns if the prefab is neither public transport nor cargo transport vehicle.
Usage Example
// Example: Obtain sorted transport lines for a query and count bus lines (TransportType.Bus)
EntityQuery query = /* build query that matches route entities */;
EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
PrefabSystem prefabSystem = /* get PrefabSystem instance from your context */;
// Get sorted lines (returns a NativeArray allocated with Allocator.Temp)
NativeArray<UITransportLineData> sortedLines = TransportUIUtils.GetSortedLines(query, entityManager, prefabSystem);
try
{
int busCount = TransportUIUtils.CountLines(sortedLines, TransportType.Bus, cargo: false);
// Use sortedLines for UI list population...
}
finally
{
// Make sure to dispose the returned NativeArray to avoid leaks
if (sortedLines.IsCreated)
sortedLines.Dispose();
}
Additional notes: - Many methods use EntityManager.GetComponentData / GetBuffer and assume required components exist on supplied entities. Ensure the EntityQuery or provided entity is the correct route/vehicle entity type to avoid exceptions. - GetSortedLines allocates a NativeArray with Allocator.Temp; callers should dispose it within the same frame or convert to a longer-lived allocator if needed. - Performance: these helpers iterate route buffers and may touch many entities and buffers; call them sparingly and prefer caching results for UI frames where possible.