Game.UI.UISystemBase
Assembly: Assembly-CSharp
Namespace: Game.UI
Type: abstract class
Base: GameSystemBase
Summary:
UISystemBase is a base class for game systems that interact with the user interface. It centralizes management of UI bindings (IBinding) and update bindings (IUpdateBinding), ensuring they are registered with the game's UI binding manager when created and removed when destroyed. It also integrates with the game lifecycle (OnCreate, OnDestroy, OnUpdate, OnGamePreload) and provides a virtual gameMode property so derived systems can opt-in to specific GameMode(s). Methods and lifecycle hooks are marked with [Preserve] where necessary to avoid IL2CPP/code-stripping issues in Cities: Skylines 2.
Fields
-
private List<IBinding> m_Bindings
Holds all IBinding instances added by the system. These bindings are registered with GameManager.instance.userInterface.bindings in AddBinding and removed in OnDestroy. Initialized in OnCreate. -
private List<IUpdateBinding> m_UpdateBindings
Holds IUpdateBinding instances that require per-frame updates. These are a subset of m_Bindings and are updated each frame from OnUpdate. Initialized in OnCreate.
Properties
public virtual GameMode gameMode { get; }
Returns the GameMode(s) this system targets. Default implementation returns GameMode.All. Derived classes should override to restrict the system to specific modes. The value is used in OnGamePreload to enable/disable the system depending on the current game mode.
Constructors
protected UISystemBase()
Protected constructor (marked [Preserve] on class methods where required). Nothing special happens in the constructor; initialization of lists is performed in OnCreate.
Methods
[Preserve] protected override void OnCreate()
Initializes internal collections:- Creates m_Bindings = new List
() -
Creates m_UpdateBindings = new List
() Also calls base.OnCreate(). This is where derived systems should perform UI binding setup (or they can do it later). -
[Preserve] protected override void OnDestroy()
Cleans up registered bindings by removing each IBinding from GameManager.instance.userInterface.bindings via RemoveBinding, and calls base.OnDestroy(). Ensures no stale bindings remain after the system is destroyed. -
[Preserve] protected override void OnUpdate()
Iterates through m_UpdateBindings and calls Update() on each IUpdateBinding. This provides per-frame update behavior for bindings that require it. -
protected void AddBinding(IBinding binding)
Registers a binding with both the system and the game's UI binding manager: - Adds the binding to m_Bindings.
-
Calls GameManager.instance.userInterface.bindings.AddBinding(binding). Use this to ensure the system tracks the binding and it is available to the UI.
-
protected void AddUpdateBinding(IUpdateBinding binding)
Convenience helper that: - Calls AddBinding(binding) to register it.
-
Adds the binding to m_UpdateBindings so it will be updated every frame via OnUpdate.
-
protected override void OnGamePreload(Purpose purpose, GameMode mode)
Called during game preload. Calls base.OnGamePreload(...) and sets base.Enabled based on whether this system's gameMode overlaps the supplied mode: - base.Enabled = (gameMode & mode) != 0 This toggles whether the system is enabled for the current game mode.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// Example: create and register a binding
var myBinding = new MyBinding(...); // implements IUpdateBinding or IBinding
AddBinding(myBinding);
// If the binding needs per-frame updates:
var myUpdateBinding = new MyUpdateBinding(...); // implements IUpdateBinding
AddUpdateBinding(myUpdateBinding);
}
Notes and tips: - Use AddBinding to ensure your bindings are tracked and automatically removed on system destruction. - Use AddUpdateBinding for bindings that implement IUpdateBinding so their Update() method is invoked each frame. - Override gameMode to restrict the system to specific GameMode values (e.g., GameMode.Editor, GameMode.Campaign). - [Preserve] attributes on lifecycle methods help prevent stripping under IL2CPP; keep them on overrides used by the game engine.