UIDialogBox - jimdroberts/FishMMO GitHub Wiki

Description

Manages the display and functionality of a dialog box in the UI. Provides a prompt, accept/cancel buttons, and callback support for user interaction. Used in FishMMO client UI for modal dialogs and confirmations.


API Access

Fields

  • public TMP_Text DialogueLabel

    Label for displaying the dialog prompt text.

  • public Button AcceptButton

    Button for accepting the dialog.

  • public Button CancelButton

    Button for cancelling or closing the dialog.

  • public TMP_Text CancelButtonLabel

    Label for the cancel/close button.

  • private Action onAccept

    Callback invoked when the user accepts the dialog.

  • private Action onCancel

    Callback invoked when the user cancels the dialog.

Methods

  • public override void OnStarting()

    Called when the control is starting. Initializes the cancel button label reference.

  • public void Open(string text, Action onAccept = null, Action onCancel = null)

    Opens the dialog box with the specified prompt and callbacks. Parameters: string text – Prompt text to display; Action onAccept – Callback for accept action; Action onCancel – Callback for cancel action.

  • public void OnClick_Accept()

    Called when the accept button is clicked. Invokes accept callback and hides the dialog.

  • public void OnClick_Cancel()

    Called when the cancel button is clicked. Invokes cancel callback and hides the dialog.


Basic Usage

Setup

  1. Assign the required UI elements (DialogueLabel, AcceptButton, CancelButton) in the Unity Inspector.
  2. Attach the UIDialogBox script to a UI GameObject.
  3. Optionally, set up callback methods for accept and cancel actions.
  4. Call Open() to display the dialog with desired prompt and callbacks.

Example

// Example: Showing a dialog box and handling user response
public class DialogExample : MonoBehaviour
{
    public UIDialogBox dialogBox;

    public void ShowDialog()
    {
        dialogBox.Open(
            "Are you sure you want to quit?",
            onAccept: OnAccept,
            onCancel: OnCancel
        );
    }

    void OnAccept()
    {
        // Handle accept logic
        Debug.Log("User accepted.");
    }

    void OnCancel()
    {
        // Handle cancel logic
        Debug.Log("User cancelled.");
    }
}

Best Practices

  • Always assign all required UI references in the Inspector to avoid null reference errors.
  • Use the provided Open() method to show the dialog and set callbacks.
  • Use descriptive prompt text to inform the user of the dialog's purpose.
  • Reset or clear callbacks after dialog is closed to prevent unintended behavior.
  • Customize button labels as needed for different dialog scenarios.
⚠️ **GitHub.com Fallback** ⚠️