UIResourceBar - jimdroberts/FishMMO GitHub Wiki

Description

UIResourceBar is an abstract class for FishMMO client UI that represents a resource bar (such as health or stamina) for a character. It manages the display and updating of a resource's value using a slider and text, and subscribes to character attribute events to keep the UI in sync.


API Access

Fields

  • public Slider slider

    The slider UI element representing the resource value visually.

  • public TMP_Text resourceValue

    The text UI element displaying the current and maximum resource value.

  • public CharacterAttributeTemplate Template

    The attribute template used to identify which resource this bar represents (e.g., health, stamina).

Methods

  • public override void OnPreSetCharacter()

    Called before the character is set. Unsubscribes from attribute update events and updates the bar.

  • public override void OnPostSetCharacter()

    Called after the character is set. Subscribes to attribute update events and updates the bar.

  • public void CharacterAttribute_OnAttributeUpdated(CharacterAttribute attribute)

    Called when the resource attribute is updated. Updates the slider and text to reflect the new value. Parameters: CharacterAttribute attribute – The updated character attribute.


Basic Usage

Setup

  1. Inherit from UIResourceBar to create a specific resource bar (e.g., health, stamina).
  2. Attach the derived component to a UI GameObject.
  3. Assign the slider and resourceValue fields in the Inspector.
  4. Set the Template to the appropriate resource attribute template.

Example

// Example: Creating a health bar by inheriting from UIResourceBar
public class UIHealthBar : UIResourceBar { }

// In the Unity Editor:
// 1. Attach UIHealthBar to a UI GameObject.
// 2. Assign the slider and resourceValue fields.
// 3. Set the Template to the health attribute template.

Best Practices

  • Always assign the slider, resourceValue, and Template fields in the Inspector.
  • Inherit from UIResourceBar for each specific resource type to keep logic organized.
  • Ensure event subscriptions are properly managed to avoid memory leaks or stale UI updates.
⚠️ **GitHub.com Fallback** ⚠️