UIResourceBar - jimdroberts/FishMMO GitHub Wiki
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.
-
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).
-
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.
- Inherit from
UIResourceBar
to create a specific resource bar (e.g., health, stamina). - Attach the derived component to a UI GameObject.
- Assign the
slider
andresourceValue
fields in the Inspector. - Set the
Template
to the appropriate resource attribute template.
// 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.
- Always assign the
slider
,resourceValue
, andTemplate
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.