UIInventoryButton - jimdroberts/FishMMO GitHub Wiki

Description

UI button representing a single inventory slot. Handles drag-and-drop assignment, swapping items between slots, unequipping equipment to inventory, right-click activation, and clearing slot visuals.


API Access

Fields

  • (inherits fields from UIReferenceButton)

    This class extends UIReferenceButton and reuses its Icon, AmountText, CooldownText and reference handling behavior.

Methods

  • public override void OnLeftClick()

    Handles left-click and drag-and-drop interactions: swaps items between inventory slots, unequips equipment to inventory if applicable, or begins dragging the current slot if empty.

  • public override void OnRightClick()

    Activates the inventory item via IInventoryController.Activate when right-clicked.

  • public override void Clear()

    Clears the button visuals: icon, cooldown text, and amount text.


Basic Usage

Setup

  1. Use UIInventoryButton as the button prefab for inventory slots and ensure it is configured as a ReferenceButtonType.Inventory on instantiation.
  2. Provide Icon, AmountText, and CooldownText via UIReferenceButton base class references in the prefab.
  3. Ensure IInventoryController and UIDragObject UI systems are available and wired up at runtime for swap and drag behavior to work.

Example

var btn = Instantiate(inventoryButtonPrefab, parent).GetComponent<UIInventoryButton>();
btn.Character = playerCharacter;
btn.ReferenceID = slotIndex;
btn.Type = ReferenceButtonType.Inventory;

// Dragging and clicking the button will be handled by the component.

Best Practices

  • Validate references in the chore that creates inventory buttons to avoid null icons or texts.
  • Keep OnLeftClick logic concise; it runs as part of UI interactions and triggers network broadcasts.
  • Ensure controller methods like CanSwapItemSlots are robust to prevent invalid swap broadcasts.
  • Use server-side validation for inventory actions; the UI should only suggest intent (it broadcasts requests, server enforces rules).
⚠️ **GitHub.com Fallback** ⚠️