Game.UI.InGame.VehiclesSection
Assembly: Assembly-CSharp (game assembly)
Namespace: Game.UI.InGame
Type: class
Base: InfoSectionBase
Summary:
VehiclesSection is an in-game UI info section that gathers and exposes information about vehicles owned/managed by a selected entity (or its company). It builds a sortable list of vehicles (UIVehicle nested struct), computes vehicle counts, available vehicles and capacity based on various facility/department data (hospital, police, fire, transport depot, etc.), and exposes the data to the UI writer as JSON. The section reads entity Component data (EntityManager) and uses a NativeList
Fields
-
private DynamicBuffer<OwnedVehicle> m_Buffer
Used to hold the OwnedVehicle buffer for the currently selected entity (or company entity). Populated via EntityManager.TryGetBuffer and iterated to collect vehicles. -
private Entity m_CompanyEntity
If the selected entity belongs to a company, this holds the company entity used as the effective owner for vehicle lookup and capacity lookups.
Properties
-
protected override string group => "VehiclesSection"
Identifier used by the base InfoSection system to group this section in the UI. -
private VehicleLocaleKey vehicleKey { get; set; }
Locale key describing the generic vehicle label used for the current selection (can change to PublicTransportVehicle, HouseholdVehicle, etc.). -
private int vehicleCount { get; set; }
Number of vehicles collected into vehicleList (after filtering parked vehicles). -
private int availableVehicleCount { get; set; }
Number of vehicles currently available (computed via VehicleUIUtils unless fallback logic applies). -
private int vehicleCapacity { get; set; }
Capacity computed from facility prefab/component data (sums capacities from multiple component types and company transport limits). May be overridden to buffer length in specific edge-cases. -
private NativeList<UIVehicle> vehicleList { get; set; }
NativeList storing UIVehicle items. Allocated with Allocator.Persistent in OnCreate and disposed in OnDestroy. Sorted prior to writing out.
Nested types
public readonly struct UIVehicle : IComparable<UIVehicle>
- Properties:
public Entity entity { get; }
— vehicle entity.public VehicleLocaleKey vehicleKey { get; }
— localized vehicle type key (Ambulance, FireEngine, Taxi, etc.).public VehicleStateLocaleKey stateKey { get; }
— current vehicle state locale key.
- Constructor:
public UIVehicle(Entity entity, VehicleLocaleKey vehicleKey, VehicleStateLocaleKey stateKey)
- Method:
public int CompareTo(UIVehicle other)
— sorts primarily by vehicleKey then by stateKey.
Constructors
public VehiclesSection()
Default constructor. Class uses [Preserve] on lifecycle methods to ensure they're kept by the engine.
Methods
protected override void OnCreate()
Initializes the NativeList: -
vehicleList = new NativeList
(50, Allocator.Persistent); Ensure to call base.OnCreate(). Because the NativeList is persistent it must be disposed in OnDestroy. -
protected override void OnDestroy()
Disposes vehicleList and calls base.OnDestroy(). Prevents memory leaks from Allocator.Persistent. -
protected override void Reset()
Resets runtime fields between uses: -
vehicleCount = 0; vehicleCapacity = 0; vehicleList.Clear(); m_Buffer = default; m_CompanyEntity = Entity.Null;
-
private bool Visible()
Determines whether the section is visible by attempting to acquire an OwnedVehicle buffer for the selected entity. If not present, the method attempts to resolve a company entity for the selected entity and read the buffer from the company. Returns true if a buffer is available. -
[Preserve] protected override void OnUpdate()
Sets base.visible = Visible(); called each update frame. -
protected override void OnProcess()
Core processing method that: - Initializes vehicleKey and a UI-localized label.
- Resolves whether to use a company entity for capacity/vehicle checks.
- Reads many possible facility/component types (HospitalData, PoliceStationData, FireStationData, PostFacilityData, MaintenanceDepotData, TransportDepotData, DeathcareFacilityData, GarbageFacilityData, PrisonData, EmergencyShelterData, and TransportCompanyData) and accumulates vehicleCapacity from their capacity fields.
- Flags special cases where vehicles exist but capacity is zero and handles household / public transport naming.
- Iterates m_Buffer to add non-parked vehicles to vehicleList via AddVehicle().
-
Sets tooltip/localization keys, computes vehicleCount, decides availableVehicleCount (either fallback to buffer length or via VehicleUIUtils.GetAvailableVehicles), and sorts vehicleList.
-
public static void AddVehicle(EntityManager entityManager, Entity vehicle, NativeList<UIVehicle> vehicleList)
Static helper that determines a vehicle's VehicleLocaleKey and VehicleStateLocaleKey and appends a UIVehicle to vehicleList. It: - Obtains the vehicle state via VehicleUIUtils.GetStateKey.
- Reads the vehicle's PrefabRef to inspect prefab component types (Car, Helicopter, Ambulance, FireEngine, PostVan, DeliveryTruck, Hearse, GarbageTruck, Taxi, MaintenanceVehicle, PublicTransport, etc.).
- Handles police vehicle specializations via PoliceCarData and public transport specializations via PublicTransportVehicleData (e.g., PrisonVan, EvacuationBus).
-
Adds new UIVehicle(vehicle, vehicleLocaleKey, stateKey) to the list.
-
public override void OnWriteProperties(IJsonWriter writer)
Writes out the section's properties to the JSON writer for the UI: - Writes vehicleKey, vehicleCount, availableVehicleCount, vehicleCapacity.
-
Sorts vehicleList, then writes "vehicleList" as an array with BindVehicle producing each entry.
-
public static void BindVehicle(NameSystem nameSystem, IJsonWriter binder, UIVehicle vehicle)
Writes a single vehicle entry into the JSON writer with fields: - "entity" (vehicle.entity)
- "name" (bound via nameSystem.BindName)
- "vehicleKey" (string name of VehicleLocaleKey)
- "stateKey" (string name of VehicleStateLocaleKey) This produces an object useful for UI consumption.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// pre-allocate the native list for vehicle entries; must dispose in OnDestroy
vehicleList = new NativeList<UIVehicle>(50, Allocator.Persistent);
}
[Preserve]
protected override void OnDestroy()
{
// dispose native list to avoid memory leaks
vehicleList.Dispose();
base.OnDestroy();
}
Additional notes: - vehicleList uses Unity.Collections.NativeList with Allocator.Persistent; always dispose in OnDestroy. - AddVehicle and OnProcess rely on entity component queries (HasComponent / TryGetComponent / GetComponentData) — use correct EntityManager context when calling externally. - OnWriteProperties/BindVehicle format enum values as names (Enum.GetName) for localization keys expected by the UI. - The class supports company-owned vehicles by resolving a company entity (m_CompanyEntity) and reading its buffers/prefab components when applicable.