Game.UI.InGame.EfficiencySection
Assembly:
Assembly-CSharp
Namespace:
Game.UI.InGame
Type:
class
Base:
InfoSectionBase
Summary:
A UI info section used by the in-game building info panel to display a building's efficiency as a percentage and a list of contributing efficiency factors. It reads the DynamicBuffer
Fields
private struct EfficiencyFactor
This nested struct models a single efficiency contribution and implements IJsonWritable to support JSON serialization. It contains:Game.Buildings.EfficiencyFactor factor
— the enum identifying the type of factor.int value
— the factor's contribution expressed as a delta from 100 (e.g., -10 or +5).-
int result
— the running resulting efficiency percentage after applying the factor. -
private int efficiency { get; set; }
Holds the current computed building efficiency expressed as an integer percent (0..100+). Updated in OnProcess and used when writing properties. -
private List<EfficiencyFactor> factors { get; set; }
A list of computed EfficiencyFactor entries describing the non-zero contributions that produced the current efficiency. Initialized in OnCreate and cleared in Reset/OnProcess.
Properties
-
protected override string group => "EfficiencySection"
The group identifier used by the InfoSectionBase system to categorize this section in the UI. -
private int efficiency { get; set; }
(see Fields) Backing auto-property storing the rounded efficiency percent. -
private List<EfficiencyFactor> factors { get; set; }
(see Fields) Backing auto-property storing the factor list.
Constructors
public EfficiencySection()
Constructor annotated with [Preserve]. The constructor does not perform heavy initialization (initialization occurs in OnCreate). The Preserve attribute ensures Unity's managed code stripping does not remove the type/methods needed by reflection.
Methods
-
protected override void OnCreate()
: System.Void
Called during the section lifecycle creation. Calls base.OnCreate() and initializes the factors list with a reasonable capacity (30). Marked with [Preserve] to avoid stripping. -
protected override void Reset()
: System.Void
Resets the internal state: sets efficiency to 0 and clears the factors list. Called when the section should be reinitialized. -
private bool Visible()
: System.Boolean
Determines whether the section should be visible for the currently selected entity. Visibility conditions: - The selected entity must have Building and Efficiency components.
- The entity must not have Abandoned or Destroyed components.
-
If the building can belong to a company, the company must be valid (CompanyUIUtils.HasCompany is consulted). Returns true when the section should be shown.
-
protected override void OnUpdate()
: System.Void
Updates the section's visibility and dirty flag. If visible, it reads the DynamicBufferin read-only mode and compares the current rounded efficiency (via BuildingUtils.GetEfficiency) to the stored efficiency to set m_Dirty when the displayed value should change. -
protected override void OnProcess()
: System.Void
Core processing method that computes the displayed efficiency and builds the factors list: - Reads the DynamicBuffer
for the selected entity and converts it to a NativeArray which is sorted. - If there are no entries, clears factors and returns.
- If efficiency > 0: iterates factor entries, multiplies a running float
num
by each factor's m_Efficiency (clamped >= 0), computes a per-factor delta (num3 = round(100 * m_Efficiency) - 100, clamped to -99) and the resulting percent (result = max(1, round(num))), and adds non-zero deltas to the factors list. - If overall efficiency is 0 or less: searches for factors with zero m_Efficiency and adds a -100 entry for them; if a factor's enum value is <= 3 it breaks early (special ordering/priority).
-
Updates the stored efficiency percent with math.round(100f * BuildingUtils.GetEfficiency(buffer)).
-
public override void OnWriteProperties(IJsonWriter writer)
: System.Void
Serializes the current efficiency and factors into JSON using the provided writer: - Writes property "efficiency" as the integer efficiency.
-
Writes property "factors" as an array of EfficiencyFactor objects via their IJsonWritable implementation.
-
private void EfficiencyFactor.Write(IJsonWriter writer)
: System.Void
Implementation of IJsonWritable for the nested struct. Writes a JSON object with: - type identifier for EfficiencyFactor,
- "factor" as the enum name (Enum.GetName on Game.Buildings.EfficiencyFactor),
- "value" as the integer delta,
-
"result" as the integer result percent.
-
public EfficiencyFactor(Game.Buildings.EfficiencyFactor factor, int value, int result)
Constructor for the nested EfficiencyFactor struct that sets the three backing fields.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// initialize list to avoid allocations during processing
factors = new List<EfficiencyFactor>(30);
}
Notes and implementation details:
- The section relies on the ECS world / EntityManager to fetch components and buffers (DynamicBuffer