Game.UI.InGame.CargoSection
Assembly:
Assembly-CSharp (game client assembly where most game types live)
Namespace:
Game.UI.InGame
Type:
class
Base:
InfoSectionBase
Summary:
CargoSection is a UI info panel section used to display cargo-related data for vehicles and vehicle groups (e.g., delivery trucks, cargo transport vehicles). It collects and categorizes resources (raw materials, processed goods and mail) from a selected entity or from vehicles in a vehicle layout buffer, computes total cargo and capacity, and exposes that data for the UI (including JSON serialization via OnWriteProperties). The class makes use of Unity.Entities (ECS) buffers and NativeList to aggregate resources and relies on ResourceSystem/ResourcePrefabs for resource metadata.
Fields
-
private ResourcePrefabs m_ResourcePrefabs
This holds a reference to resource prefab metadata obtained from the ResourceSystem. It's used when categorizing resources into UIResource entries for display. -
private NativeList<UIResource> rawMaterials
Stores aggregated UIResource entries representing raw materials currently carried. Allocated with Allocator.Persistent in OnCreate and disposed in OnDestroy. -
private NativeList<UIResource> processedGoods
Stores aggregated UIResource entries representing processed goods currently carried. Allocated with Allocator.Persistent in OnCreate and disposed in OnDestroy. -
private NativeList<UIResource> mail
Stores aggregated UIResource entries representing mail currently carried. Allocated with Allocator.Persistent in OnCreate and disposed in OnDestroy.
Properties
-
protected override string group => "CargoSection"
Identifier used by the parent InfoSectionBase to group or label this section in the UI system. -
private int cargo { get; set; }
Total amount of cargo currently aggregated for the selected entity (sum of amounts across categorized resources). -
private int capacity { get; set; }
Total cargo capacity for the selected entity (sum of vehicle capacities when inspecting a group or single vehicle capacity). -
private CargoKey cargoKey { get; set; }
Enum-backed marker (currently only Cargo) used to annotate the output payload in OnWriteProperties. Could be used for different cargo display modes in future. -
private NativeList<UIResource> rawMaterials { get; set; }
(Repeated here as a property as declared) Aggregated list for raw materials; see field description. Important: NativeList must be disposed in OnDestroy to avoid memory leaks. -
private NativeList<UIResource> processedGoods { get; set; }
(Repeated here as a property) Aggregated list for processed goods. -
private NativeList<UIResource> mail { get; set; }
(Repeated here as a property) Aggregated list for mail. -
protected override Entity selectedEntity { get; }
Overrides selection resolution to return the controlling entity when a Controller component is present on the base selected entity (i.e., unwraps Controller -> m_Controller). This ensures the UI inspects the vehicle entity itself when a Controller wrapper is selected. -
protected override Entity selectedPrefab { get; }
Overrides prefab resolution to return the underlying prefab referenced by a Controller -> PrefabRef chain if present; otherwise falls back to base.selectedPrefab.
Constructors
public CargoSection()
Default constructor. The heavy initialization of NativeLists and ResourcePrefabs is done in OnCreate (the constructor is preserved for ECS/serialization but not used to allocate persistent resources).
Methods
-
protected override void Reset()
Clears aggregated NativeLists (rawMaterials, processedGoods, mail), and resets cargo, capacity and cargoKey to defaults. Called to prepare the section for a fresh data collection cycle. -
[Preserve] protected override void OnCreate()
Initializes resources required by the section: - Allocates rawMaterials, processedGoods and mail as NativeList
with Allocator.Persistent. -
Obtains m_ResourcePrefabs from the world ResourceSystem. Note: Allocator.Persistent lists must be disposed in OnDestroy to avoid memory leaks.
-
[Preserve] protected override void OnDestroy()
Disposes the NativeList instances (rawMaterials, processedGoods, mail) and calls base.OnDestroy. Ensures persistent allocations are released. -
private bool Visible()
Determines whether this section should be visible for the current selection. It: - Returns false if the selected entity does not have a Vehicle component.
- Attempts to sum cargo capacity either from a vehicle layout buffer (LayoutElement buffer) or directly from DeliveryTruckData / CargoTransportVehicleData on the selected prefab/entity.
-
Sets capacity accordingly and returns true if capacity > 0. This method is used by OnUpdate to set base.visible.
-
[Preserve] protected override void OnUpdate()
Calls Visible() and assigns its result to base.visible so the UI system shows/hides this section appropriately each frame. -
protected override void OnProcess()
Main data-collection routine. It: - Resets cargoKey to Cargo.
- If the selected entity (or its vehicle buffer members) are DeliveryTruck instances, it extracts resource type(s) and amounts from DeliveryTruck components (accumulating amounts when DeliveryTruckFlags.Loaded is set).
- Otherwise it aggregates Resources buffers (DynamicBuffer
) from the selected entity or its vehicles, using AddResources to combine resources with the same resource id. - Calls UIResource.CategorizeResources for each resource set to populate rawMaterials/processedGoods/mail lists using base.EntityManager and m_ResourcePrefabs.
-
Updates the cargo total. This method branches to handle single vehicle vs vehicle groups and ensures resources from multiple vehicles are combined.
-
private void AddResources(DynamicBuffer<Resources> source, NativeList<Resources> target)
Merges resource entries from a source buffer into the target NativeListby resource id. It: - Skips zero-amount entries.
-
If an existing entry for the resource id exists in target, increases its amount; otherwise adds a new entry. Used by OnProcess to create a combined list of resources before categorization.
-
public override void OnWriteProperties(IJsonWriter writer)
Serializes the collected cargo data to JSON: - Writes cargo and capacity integers.
- Sorts and writes arrays for rawMaterials, processedGoods and mail (each UIResource written via writer.Write).
- Writes cargoKey name as a string. Used by the UI/remote inspector to produce the JSON representation consumed by the UI.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
rawMaterials = new NativeList<UIResource>(Allocator.Persistent);
processedGoods = new NativeList<UIResource>(Allocator.Persistent);
mail = new NativeList<UIResource>(Allocator.Persistent);
m_ResourcePrefabs = base.World.GetOrCreateSystemManaged<ResourceSystem>().GetPrefabs();
}
Notes and Modding Tips: - NativeList allocations use Allocator.Persistent; always ensure OnDestroy disposes them to prevent memory leaks. - The class relies on ECS components (Vehicle, DeliveryTruck, CargoTransportVehicleData, DeliveryTruckData, Resources buffers and LayoutElement buffer). When extending or inspecting similar functionality, replicate the same buffer/component access patterns. - UIResource.CategorizeResources and ResourcePrefabs are used for transforming raw ECS resource data into UI-friendly structures; consult ResourceSystem and UIResource definitions when modifying display categories or icons. - Visible() computes capacity from either a vehicle group (layout buffer) or a single prefab; ensure changes to vehicle prefab components keep capacity fields consistent (m_CargoCapacity).