UISelector - jimdroberts/FishMMO GitHub Wiki

Description

UISelector is a control that presents a list of selectable items to the user, allowing them to choose one option. It manages the creation and destruction of UI elements representing these options, and handles user input to select an option and confirm or cancel their selection. Used in FishMMO client UI for item, character, or option selection dialogs.


API Access

Fields

  • private Action onAccept

    Callback invoked when the user accepts a selection.

  • private int selectedIndex

    Index of the currently selected item.

  • private List cachedObjects

    List of cached objects available for selection.

  • private List ButtonSlots

    List of button slots representing selectable options in the UI.

  • public RectTransform ButtonParent

    Parent RectTransform for dynamically created buttons.

  • public UITooltipButton ButtonPrefab

    Prefab used to instantiate selectable buttons.

Methods

  • public override void OnDestroying()

    Called when the UISelector is being destroyed. Cleans up button slots.

  • public void Open(List cachedObjects, Action onAccept)

    Opens the selector UI with the provided cached objects and accept callback. Parameters: List cachedObjects – List of objects to select from; Action onAccept – Callback invoked when a selection is accepted.

  • private void ClearSlots()

    Clears all button slots and destroys their associated GameObjects.

  • private void UpdateEventSlots()

    Updates the button slots to match the current cached objects, creating new buttons as needed.

  • private void EventEntry_OnLeftClick(int index, object[] optionalParams)

    Called when a button is left-clicked. Updates the selected index. Parameters: int index – Index of the clicked button; object[] optionalParams – Optional parameters (unused).

  • public void OnClick_Accept()

    Called when the accept button is clicked. Invokes the accept callback and closes the selector.

  • public void OnClick_Cancel()

    Called when the cancel button is clicked. Clears slots and closes the selector.


Basic Usage

Setup

  1. Assign the required UI elements (ButtonParent, ButtonPrefab) in the Unity Inspector.
  2. Attach the UISelector script to a UI GameObject.
  3. Prepare a list of objects implementing ICachedObject to be selectable.
  4. Call Open() to display the selector with the list and a callback for selection.

Example

// Example: Using UISelector to select an item from a list
public class SelectorExample : MonoBehaviour
{
    public UISelector selector;
    public List<ICachedObject> items;

    public void ShowSelector()
    {
        selector.Open(items, OnItemSelected);
    }

    void OnItemSelected(int id)
    {
        Debug.Log($"Selected item ID: {id}");
    }
}

Best Practices

  • Always assign all required UI references in the Inspector to avoid null reference errors.
  • Use the provided Open() method to show the selector and set the callback.
  • Clean up button slots when closing the selector to prevent memory leaks.
  • Use descriptive labels for selectable items to improve user experience.
  • Reset or clear callbacks after selector is closed to prevent unintended behavior.
⚠️ **GitHub.com Fallback** ⚠️