UITarget - jimdroberts/FishMMO GitHub Wiki

Description

UITarget is a UI component in the FishMMO client that displays information about the player's current target. It manages the target's name, health bar, and overhead labels, and subscribes to target controller events to update the UI in real time.


API Access

Fields

  • public TMP_Text NameLabel

    The label displaying the target's name.

  • public Slider HealthSlider

    The slider displaying the target's health.

  • public CharacterAttributeTemplate HealthAttribute

    The health attribute template used to identify health resources.

  • private Cached3DLabel targetLabel

    Cached 3D label for displaying overhead information about the target.

Methods

  • public override void OnPostSetCharacter()

    Called after the character is set. Subscribes to target controller events.

  • public override void OnPreUnsetCharacter()

    Called before the character is unset. Unsubscribes from target controller events and caches the label.

  • public void TargetController_OnChangeTarget(Transform target)

    Handles target change event. Updates UI and overhead label for the new target. Parameters: Transform target – The new target transform.

  • public void TargetController_OnUpdateTarget(Transform target)

    Handles target update event. Reuses change target logic. Parameters: Transform target – The target transform.

  • public void TargetController_OnClearTarget(Transform lastTarget = null)

    Handles target clear event. Hides UI and disables overhead labels. Parameters: Transform lastTarget – The last target transform (optional).

  • private void UpdateTargetLabel(Transform target, ICharacter character, IInteractable interactable)

    Updates the overhead label for the target, displaying name, title, and color. Parameters: Transform target, ICharacter character, IInteractable interactable.


Basic Usage

Setup

  1. Attach UITarget to a UI GameObject in the Unity Editor.
  2. Assign the NameLabel, HealthSlider, and HealthAttribute fields in the Inspector.
  3. Ensure the character implements ITargetController and is set on this control.

Example

// Example: Using UITarget in a scene
UITarget targetUI = GetComponent<UITarget>();
targetUI.NameLabel = ...; // Assign TMP_Text
targetUI.HealthSlider = ...; // Assign Slider
targetUI.HealthAttribute = healthTemplate; // Assign CharacterAttributeTemplate for health
// Set the character implementing ITargetController

Best Practices

  • Always assign all required fields in the Inspector to avoid null references.
  • Ensure the character implements ITargetController for full functionality.
  • Unsubscribe from events in OnPreUnsetCharacter to prevent memory leaks.
  • Use the provided event handlers to keep the UI in sync with the current target.
⚠️ **GitHub.com Fallback** ⚠️