Game.UI.InGame.UIResource
Assembly: Game (Assembly-CSharp equivalent — the type is defined in the game's code)
Namespace: Game.UI.InGame
Type: readonly struct (implements IJsonWritable, IComparable
Base: System.ValueType
Summary:
Represents a single resource entry used by the in-game UI. Contains the resource identifier, an integer amount, a UI-facing status (Deficit/Normal/Surplus/None), and a flag indicating whether the resource is a raw material. The struct provides several constructors for building instances from different game resource types and storage contexts, JSON serialization via IJsonWritable, comparison logic for sorting, and a helper to categorize resources into lists (raw materials, processed goods, mail). The struct is immutable (readonly) and lightweight for use in UI lists and NativeList collections.
Fields
- (No private instance fields declared; the struct exposes its data via read-only properties. It also contains two nested enums: ResourceStatus and StorageType.)
- ResourceStatus: None, Deficit, Normal, Surplus — UI level status used to indicate stock situation.
- StorageType: None, Company, Warehouse, Cargo — used to determine threshold logic when constructing a UIResource with a storage context.
Properties
-
public Resource key { get; }
The resource identifier (enum Resource). Used as the unique key for equality and hashing. -
public int amount { get; }
The current amount / stock level of the resource. -
public ResourceStatus status { get; }
UI status describing stock level (None/Deficit/Normal/Surplus). Status is set by certain constructors that receive a StorageType and uses game threshold constants to determine deficit/surplus. -
public bool isRawMaterial { get; }
True when the underlying ResourcePrefab's ResourceData indicates the resource is a raw material. Determined at construction time using the provided EntityManager and ResourcePrefabs.
Constructors
-
public UIResource(Resource resource, int amount, EntityManager entityManager, ResourcePrefabs prefabs)
Creates a UIResource with the given resource and amount. Status is initialized to ResourceStatus.None. Determines isRawMaterial by querying the ResourceData component from prefabs[resource] via EntityManager. If the component is missing, isRawMaterial defaults to false. -
public UIResource(Resources resource, EntityManager entityManager, ResourcePrefabs prefabs)
Convenience constructor that takes a Resources container (resource + amount) and delegates to the primary constructor. -
public UIResource(Resource resource, int amount, StorageType storageType, EntityManager entityManager, ResourcePrefabs prefabs)
Constructs a UIResource and computes the ResourceStatus based on the provided storageType and amount. Uses game constants (StorageCompanySystem.kStationExportStartAmount, kStationLowStockAmount for Cargo; kStorageExportStartAmount, kStorageLowStockAmount for Warehouse) to set Surplus / Deficit / Normal. Additionally, if the resource matches a specific bitmask ((Resource)28672uL — a game-specific flag check, e.g., mail-related resources), the status is forced to Normal. -
public UIResource(Resources resource, StorageType storageType, EntityManager entityManager, ResourcePrefabs prefabs)
Convenience constructor taking a Resources instance plus a storage type; delegates to the corresponding Resource + amount constructor.
Methods
public void Write(IJsonWriter writer)
Implements IJsonWritable. Writes a simple JSON object containing:- type (full type name)
- key (string name of the Resource enum)
- amount (integer)
-
status (string name of the ResourceStatus enum)
Useful for logging, debugging, or exporting UI state. -
public int CompareTo(UIResource other)
Implements IComparable. Comparison logic (used for sorting UI lists) follows these priorities: - Checks a game-specific bitmask ((Resource)28672uL) on resource keys to group certain special resources separately.
- Compares isRawMaterial flags so raw materials and processed goods are grouped.
-
Compares amounts in descending order (larger amounts come first).
Note: the method uses the mask and boolean comparisons to produce a deterministic ordering used by the UI. -
public bool Equals(UIResource other)
Implements IEquatable. Equality is based solely on the resource key (the Resource enum value). -
public override bool Equals(object obj)
Standard override; returns true if obj is a UIResource with the same key. -
public override int GetHashCode()
Hash code derived from the resource key's hash code. -
public static void CategorizeResources(Resource resource, int amount, NativeList<UIResource> rawMaterials, NativeList<UIResource> processedGoods, NativeList<UIResource> mail, EntityManager entityManager, ResourcePrefabs resourcePrefabs, StorageType storageType = StorageType.None)
Static helper that classifies a resource into one of three NativeList collections: - If the resource matches the special bitmask ((Resource)28672uL) it's added to the mail list.
- If ResourceData exists for the resource prefab and its m_IsMaterial flag is true, it is added to rawMaterials.
- Otherwise it is added to processedGoods.
The created UIResource entries respect the optional storageType and use the provided EntityManager and resourcePrefabs to determine isRawMaterial and status.
Usage Example
// Example: create UIResource instances and categorize them into NativeList collections.
// (Assumes entityManager and resourcePrefabs are available in context.)
var rawMaterials = new NativeList<UIResource>(Allocator.Temp);
var processed = new NativeList<UIResource>(Allocator.Temp);
var mail = new NativeList<UIResource>(Allocator.Temp);
// Single resource (Resource enum) with amount, using Warehouse thresholds:
UIResource r = new UIResource(Resource.Wood, 120, UIResource.StorageType.Warehouse, entityManager, resourcePrefabs);
// Add into appropriate list using the helper:
UIResource.CategorizeResources(r.key, r.amount, rawMaterials, processed, mail, entityManager, resourcePrefabs, UIResource.StorageType.Warehouse);
// Serialize for debugging:
var writer = /* obtain or implement an IJsonWriter */;
r.Write(writer);
// Cleanup NativeList when done:
rawMaterials.Dispose();
processed.Dispose();
mail.Dispose();
Notes and implementation details:
- The constructors rely on EntityManager.TryGetComponent