UIDropdown - jimdroberts/FishMMO GitHub Wiki

Description

Manages the dropdown UI control, allowing the addition of buttons and toggles. Supports dynamic creation and removal of dropdown options for user interaction in FishMMO client UI.


API Access

Fields

  • public Button ButtonPrefab

    Prefab used to instantiate new dropdown buttons.

  • public Dictionary<string, Button> Buttons

    Dictionary mapping button names to their Button instances.

  • public Toggle TogglePrefab

    Prefab used to instantiate new dropdown toggles.

  • public Dictionary<string, Toggle> Toggles

    Dictionary mapping toggle names to their Toggle instances.

Methods

  • public override void OnStarting()

    Called when the control is starting. Registers Hide to OnLoseFocus event.

  • public override void OnDestroying()

    Called when the control is being destroyed. Unregisters Hide from OnLoseFocus event.

  • public override void Show()

    Shows the dropdown and moves it to the current mouse position.

  • public override void Hide()

    Hides the dropdown and destroys all buttons and toggles.

  • public void AddButton(string buttonName, UnityAction onClick)

    Adds a new button to the dropdown with the specified name and click callback. Parameters: string buttonName – Name of the button; UnityAction onClick – Callback for button click.

  • public void AddToggle(string toggleName, UnityAction onToggleStateChanged)

    Adds a new toggle to the dropdown with the specified name and state change callback. Parameters: string toggleName – Name of the toggle; UnityAction onToggleStateChanged – Callback for toggle state change.


Basic Usage

Setup

  1. Assign the required UI prefabs (ButtonPrefab, TogglePrefab) in the Unity Inspector.
  2. Attach the UIDropdown script to a UI GameObject.
  3. Optionally, set up event handlers for button and toggle actions.
  4. Use AddButton() and AddToggle() to populate the dropdown dynamically.

Example

// Example: Creating a dropdown with buttons and toggles
public class DropdownExample : MonoBehaviour
{
    public UIDropdown dropdown;

    void Start()
    {
        dropdown.AddButton("Save", OnSaveClicked);
        dropdown.AddButton("Load", OnLoadClicked);
        dropdown.AddToggle("Enable Music", OnMusicToggled);
    }

    void OnSaveClicked()
    {
        Debug.Log("Save clicked");
    }

    void OnLoadClicked()
    {
        Debug.Log("Load clicked");
    }

    void OnMusicToggled(bool enabled)
    {
        Debug.Log($"Music enabled: {enabled}");
    }
}

Best Practices

  • Always assign all required UI prefabs in the Inspector to avoid null reference errors.
  • Use the provided methods to add or remove dropdown options; do not manipulate the Buttons or Toggles dictionaries directly.
  • Clean up listeners and destroy UI elements when hiding the dropdown to prevent memory leaks.
  • Use descriptive names for buttons and toggles to improve user experience.
  • Position the dropdown appropriately for the user's context (e.g., near the mouse cursor).
⚠️ **GitHub.com Fallback** ⚠️