UIHotkeyBar - jimdroberts/FishMMO GitHub Wiki
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.
-
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.
-
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
andHotkeySetMultipleBroadcast
. -
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 byConstants.Configuration.MaximumPlayerHotkeys
), initializes their buttons (slot, character, key map, default reference/type), and sets label text.
- Place a
UIHotkeyBar
component on a UI object under a Canvas. - Assign
parent
to aRectTransform
container (use a layout group for arrangement). - Assign
buttonPrefab
to aUIHotkeyGroup
prefab that containsUIHotkeyButton
and label components. - Ensure that
Client
andClient.NetworkManager
are available so the component can register broadcast handlers.
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.
- 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 byGetHotkeyIndexKeyMap
to ensure input is detected correctly.