UIHotkeyBar - jimdroberts/FishMMO GitHub Wiki

Description

UI control that provides a hotkey bar for the player. It creates hotkey groups/buttons, listens for network broadcasts to populate hotkey slots, validates references (inventory, equipment, abilities), and handles player input to activate hotkeys.


API Access

Fields

  • public RectTransform parent

    Parent RectTransform that holds instantiated UIHotkeyGroup entries.

  • public UIHotkeyGroup buttonPrefab

    Prefab used to create hotkey group UI elements.

  • public List hotkeys

    List of hotkey groups currently displayed on the bar.

Methods

  • public override void OnStarting()

    Initializes the hotkey bar by adding hotkey slots up to Constants.Configuration.MaximumPlayerHotkeys.

  • public override void OnClientSet()

    Registers network broadcast handlers for HotkeySetBroadcast and HotkeySetMultipleBroadcast.

  • public override void OnClientUnset()

    Unregisters the broadcast handlers when client unsets.

  • private void OnClientHotkeySetBroadcastReceived(HotkeySetBroadcast msg, Channel channel)

    Handles a network broadcast to set a single hotkey slot. Validates slot index and updates/clears the corresponding UIHotkeyButton.

  • private void OnClientHotkeySetMultipleBroadcastReceived(HotkeySetMultipleBroadcast msg, Channel channel)

    Delegates multiple hotkey set broadcasts to the single-slot handler for each sub-message.

  • public override void OnPostSetCharacter()

    Assigns the Character reference to all existing hotkey buttons after the character is set.

  • void Update()

    Unity update loop that validates hotkeys and processes input every frame.

  • public static string GetHotkeyIndexKeyMap(int hotkeyIndex)

    Returns a human-readable key mapping string for the provided hotkey index (e.g., "Left Mouse", "Hotkey 1").

  • private void ValidateHotkeys()

    Validates each hotkey's reference based on its type (Inventory, Equipment, Ability). Clears hotkeys whose referenced item/ability no longer exists and updates icons when references change.

  • private void UpdateInput()

    Checks input using InputManager.GetKey for each hotkey key mapping and activates the hotkey button when pressed.

  • public void AddHotkeys(int amount)

    Instantiates up to amount UIHotkeyGroup elements (bounded by Constants.Configuration.MaximumPlayerHotkeys), initializes their buttons (slot, character, key map, default reference/type), and sets label text.


Basic Usage

Setup

  1. Place a UIHotkeyBar component on a UI object under a Canvas.
  2. Assign parent to a RectTransform container (use a layout group for arrangement).
  3. Assign buttonPrefab to a UIHotkeyGroup prefab that contains UIHotkeyButton and label components.
  4. Ensure that Client and Client.NetworkManager are available so the component can register broadcast handlers.

Example

var hotkeyBar = GetComponent<UIHotkeyBar>();
hotkeyBar.buttonPrefab = hotkeyGroupPrefab;
hotkeyBar.parent = hotkeyParentTransform;
hotkeyBar.AddHotkeys(12);
// After hotkeys are set by server broadcasts, the UI will validate and react to input automatically.

Best Practices

  • Use a layout group for parent to keep hotkeys aligned and responsive.
  • Keep ValidateHotkeys cheap; avoid heavy allocation inside per-frame validation loops.
  • Ensure Constants.Configuration.MaximumPlayerHotkeys matches UI capacity to avoid partial instantiation.
  • Use pooling for hotkey groups if the bar is frequently recreated.
  • Make sure InputManager mappings match the strings returned by GetHotkeyIndexKeyMap to ensure input is detected correctly.
⚠️ **GitHub.com Fallback** ⚠️