Game.TradedResourcesSection
Assembly: Assembly-CSharp
Namespace: Game.UI.InGame
Type: class (CompilerGenerated)
Base: InfoSectionBase
Summary:
TradedResourcesSection is an in-game info section used by the Cities: Skylines 2 UI to show which resources a Cargo Transport Station trades. It collects and categorizes traded resources into three groups (raw materials, processed goods, mail) and exposes them for rendering/serialization. The class uses native containers (Unity.Collections.NativeList
Fields
private ResourcePrefabs m_ResourcePrefabs
Holds a reference to the game's ResourcePrefabs obtained from the ResourceSystem on creation. Used when categorizing UIResource entries so the UI can access prefab data (icons, names, etc.). Populated in OnCreate().
Properties
-
private NativeList<UIResource> rawMaterials { get; set; }
NativeList of UIResource entries representing raw materials traded by the station. Allocated with Allocator.Persistent in OnCreate(), cleared each frame in Reset(), and disposed in OnDestroy(). The list is sorted and written to JSON in OnWriteProperties(). -
private NativeList<UIResource> processedGoods { get; set; }
NativeList of UIResource entries representing processed goods traded by the station. Managed the same way as rawMaterials. -
private NativeList<UIResource> mail { get; set; }
NativeList of UIResource entries representing mail (postal) resources traded by the station. Managed the same way as the other lists.
Notes: - These properties are private and use Unity.Collections.NativeList, so correct allocation and disposal is required to avoid memory leaks. This class allocates with Allocator.Persistent and disposes the lists in OnDestroy().
Constructors
public TradedResourcesSection()
Default constructor. Marked with [Preserve] in the original source to prevent stripping. No custom initialization is done here; initialization occurs in OnCreate().
Methods
-
private bool Visible()
Returns true when the currently selected entity is a CargoTransportStation and is NOT a StorageCompany. Implementation uses base.EntityManager.HasComponent(selectedEntity) and checks absence of Game.Companies.StorageCompany. Used to determine whether the section should be shown for the selected entity. -
[Preserve] protected override void OnUpdate()
Called each update/frame by the UI framework. Sets base.visible based on the result of Visible(). Ensures the section is only visible for applicable entities. -
protected override void Reset()
Clears the three NativeListcontainers (rawMaterials, processedGoods, mail). Intended to be called before populating lists for the current frame/selection. -
[Preserve] protected override void OnCreate()
Allocates the native lists (Allocator.Persistent) and caches m_ResourcePrefabs from the ResourceSystem: - rawMaterials = new NativeList
(Allocator.Persistent); - processedGoods = new NativeList
(Allocator.Persistent); - mail = new NativeList
(Allocator.Persistent); -
m_ResourcePrefabs = base.World.GetOrCreateSystemManaged
().GetPrefabs(); Must be paired with disposal in OnDestroy(). -
protected override void OnProcess()
Populates the resource lists for the currently selected entity. Steps: - Tries to get a PrefabRef from the selected entity.
- Resolves the prefab and attempts to get the CargoTransportStation prefab component.
-
Iterates the prefab's m_TradedResources array and for each resource calls: UIResource.CategorizeResources(EconomyUtils.GetResource(tradedResource), 0, rawMaterials, processedGoods, mail, base.EntityManager, m_ResourcePrefabs); This call categorizes the resource into the appropriate NativeList for later serialization/display.
-
public override void OnWriteProperties(IJsonWriter writer)
Serializes the three resource lists to JSON via the provided writer: - Sorts each list.
- Writes properties "rawMaterials", "processedGoods", and "mail" as arrays.
-
Iterates each NativeList and calls writer.Write(...) for each UIResource. This method is used to produce the data consumed by the UI renderer.
-
[Preserve] protected override void OnDestroy()
Disposes the NativeList instances (rawMaterials, processedGoods, mail) and then calls base.OnDestroy(). Ensures persistent native memory is released when the section is destroyed.
Notes and tips: - The class relies on several game systems (ResourceSystem, PrefabSystem, EntityManager) and utility methods (EconomyUtils.GetResource, UIResource.CategorizeResources). Ensure those systems are available in the context where this section is used. - Because NativeList is allocated with Allocator.Persistent, failing to call Dispose() (which this class does in OnDestroy) will cause a native memory leak. - The Visible() check excludes StorageCompany entities — this reflects that some transport station prefabs can be used as storage companies and should not show traded resources in that case.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// allocate persistent native lists for collecting UIResource entries
rawMaterials = new NativeList<UIResource>(Allocator.Persistent);
processedGoods = new NativeList<UIResource>(Allocator.Persistent);
mail = new NativeList<UIResource>(Allocator.Persistent);
// cache ResourcePrefabs for UIResource categorization (icons, names, etc.)
m_ResourcePrefabs = base.World.GetOrCreateSystemManaged<ResourceSystem>().GetPrefabs();
}
[Preserve]
protected override void OnDestroy()
{
// dispose native lists to avoid leaks
rawMaterials.Dispose();
processedGoods.Dispose();
mail.Dispose();
base.OnDestroy();
}