UIFactions - jimdroberts/FishMMO GitHub Wiki

Description

A UI control responsible for creating, updating, and displaying player faction information in the world UI. Manages instantiation of UIFactionDescription prefabs, updates progress/value displays based on faction values, and cleans up UI elements when the character or client changes.


API Access

Fields

  • public RectTransform FactionDescriptionParent

    Parent transform used to instantiate faction description UI elements.

  • public UIFactionDescription FactionDescriptionPrefab

    Prefab used to create new faction description UI elements.

  • private Dictionary<int, UIFactionDescription> factions

    Internal mapping of faction template ID -> instantiated faction description UI.

Methods

  • public override void OnStarting()

    Called when the UI is starting. Subscribes to character set events and clears UI on local client stop.

  • public override void OnDestroying()

    Called when the UI is being destroyed. Unsubscribes from events and clears all faction UI elements.

  • public override void OnPostSetCharacter()

    Called after the character reference is set. Subscribes to faction update events on the faction controller.

  • public override void OnPreUnsetCharacter()

    Called before the character reference is unset. Unsubscribes from faction update events and clears UI.

  • public override void OnQuitToLogin()

    Called when quitting to login. Clears all faction UI elements.

  • private void CharacterControl_OnSetCharacter(IPlayerCharacter character)

    Handler invoked when the character is set. Iterates existing factions on the controller and updates UI elements for each.

  • **public void FactionController_OnUpdateFaction(ICharacter character, Faction faction)**n

    Primary update method invoked when a faction changes. Creates a UIFactionDescription if necessary and updates label, icon, progress bar, fill color, and numeric value. Uses faction.Value to compute display color and normalize progress between template min/max.

  • private float Normalize(float x, float min, float max)

    Returns normalized (0..1) value for x between min and max. Division safe-usage is assumed from caller context.

  • public void ClearAll()

    Destroys all instantiated UIFactionDescription GameObjects and clears the internal dictionary.


Basic Usage

Setup

  1. Add the UIFactions component to a UI GameObject in your scene (usually under a UI canvas).
  2. Assign FactionDescriptionParent to the RectTransform that will hold instantiated descriptions (e.g., a vertical layout group).
  3. Assign FactionDescriptionPrefab to a UIFactionDescription prefab that contains Label, Image, Progress, and Value fields.
  4. Ensure the player IPlayerCharacter (and its IFactionController) is available so the control can subscribe and receive faction updates.

Example

// Example setup (typically done in the Editor by assigning references)
var uiFactions = GetComponent<UIFactions>();
uiFactions.FactionDescriptionPrefab = factionDescriptionPrefab; // assigned prefab
uiFactions.FactionDescriptionParent = factionListParent; // assigned RectTransform

// UIFactions will automatically subscribe to the character and update UI when factions change.

Best Practices

  • Provide a UIFactionDescription prefab that exposes a label, icon, progress bar (with a fill image) and a numeric value text for consistent UI updates.
  • Keep FactionDescriptionParent organized with a layout component (VerticalLayoutGroup/ContentSizeFitter) so added elements flow correctly.
  • Avoid modifying the internal factions dictionary externally; use the faction controller API to update faction values so the UI receives the proper events.
  • When creating custom faction templates, ensure FactionTemplate.Minimum and FactionTemplate.Maximum are set properly to avoid division errors when normalizing.
  • Reuse prefabs where possible and minimize expensive operations inside FactionController_OnUpdateFaction to keep UI updates smooth during frequent faction changes.
⚠️ **GitHub.com Fallback** ⚠️