UITooltipButton - jimdroberts/FishMMO GitHub Wiki
UITooltipButton
is a UI button component in the FishMMO client that supports tooltips, event callbacks for left/right/Ctrl+clicks, and optional parameters. It manages icon and label display, tooltip handling, and event wiring for flexible UI interactions.
-
private Sprite cachedSprite
Cached sprite for restoring the icon when clearing the button.
-
private string cachedLabel
Cached label text for restoring the tooltip label when clearing the button.
-
private UITooltip currentUITooltip
Reference to the currently displayed UITooltip instance.
-
public Image Icon
The icon image displayed on the button.
-
public TMP_Text TooltipLabel
The label text displayed for the tooltip.
-
public string ExtraTooltipInfo
Additional information to append to the tooltip.
-
public object[] OptionalParams
Optional parameters passed to click event handlers.
-
public Action<int, object[]> OnLeftClick
Event invoked on left mouse click.
-
public Action<int, object[]> OnRightClick
Event invoked on right mouse click.
-
public Action<int, object[]> OnCtrlClick
Event invoked on Ctrl+click (left or right).
-
public int Index { get; private set; }
Index of the button, used for event callbacks.
-
public ITooltip Tooltip { get; private set; }
Tooltip data associated with this button.
-
public IPlayerCharacter Character { get; private set; }
Player character associated with this button, if any.
-
protected override void Awake()
Unity Awake callback. Caches initial icon and label for later restoration.
-
protected override void OnDestroy()
Unity OnDestroy callback. Clears event handlers and resets state.
-
protected override void OnDisable()
Unity OnDisable callback. Hides tooltip if active.
-
public void Initialize(...)
Initializes the button with event handlers, tooltip, and optional parameters.
-
public void Initialize(IPlayerCharacter character, ITooltip tooltip)
Initializes the button with a player character and tooltip.
-
public override void OnPointerEnter(PointerEventData eventData)
Handles pointer enter event to show tooltip.
-
public override void OnPointerExit(PointerEventData eventData)
Handles pointer exit event to hide tooltip.
-
public override void OnPointerClick(PointerEventData eventData)
Handles pointer click event, invoking appropriate event handler based on mouse button and modifier keys.
-
private void ClearTooltip()
Hides the currently displayed tooltip, if any.
-
public virtual void Clear()
Clears the button state, restoring cached icon and label, and hiding tooltip.
- Attach
UITooltipButton
to a UI Button GameObject in the Unity Editor. - Assign the
Icon
andTooltipLabel
fields in the Inspector. - Use
Initialize
to set up event handlers, tooltip data, and optional parameters.
// Example: Setting up a UITooltipButton
UITooltipButton button = GetComponent<UITooltipButton>();
button.Icon = ...; // Assign Image
button.TooltipLabel = ...; // Assign TMP_Text
button.Initialize(0, OnLeftClickHandler, OnRightClickHandler, tooltipData, "Extra info");
void OnLeftClickHandler(int index, object[] parameters) {
// Handle left click
}
void OnRightClickHandler(int index, object[] parameters) {
// Handle right click
}
- Always assign the
Icon
andTooltipLabel
fields in the Inspector to avoid null references. - Use the
Initialize
method to set up event handlers and tooltip data. - Use
Clear()
to reset the button state when reusing or disabling the button. - Handle all click events to provide a responsive UI experience.