UIControl - jimdroberts/FishMMO GitHub Wiki

Description

Abstract base class for all UI controls in FishMMO client. Provides core functionality for visibility, focus, drag, input field navigation, and integration with UIManager and Client. All custom UI controls should inherit from this class.


API Access

Fields

  • public static readonly Color DEFAULT_COLOR

    Default color for UI controls.

  • public static readonly Color DEFAULT_SELECTED_COLOR

    Default color for selected UI controls.

  • public Canvas MainCanvas

    Reference to the main canvas containing this UI control.

  • public CanvasScaler CanvasScaler

    Reference to the canvas scaler for screen scaling.

  • public RectTransform MainPanel

    Main panel RectTransform for this UI control.

  • public TMP_InputField InputField

    Helper field to check input field focus status in UIManager.

  • public bool StartOpen

    If true, UI starts open when created.

  • public bool IsAlwaysOpen

    If true, UI cannot be closed.

  • public bool HasFocus

    True if the UI currently has focus.

  • public bool FocusOnSelect

    If true, puts the UI on top when selected.

  • public bool CloseOnQuitToMenu

    If true, closes the UI when quitting to menu.

  • public bool CloseOnEscape

    If true, closes the UI when Escape is pressed.

  • public bool CanDrag

    If true, allows the UI to be dragged.

  • public bool ClampToScreen

    If true, clamps the UI to the screen bounds when dragging.

  • public bool IsInputFieldFocused

    Returns true if any input field on this control is focused.

  • public Action OnLoseFocus

    Event called when the UI loses focus.

  • public Client Client { get; private set; }

    Reference to the injected Client instance.

  • public Transform Transform { get; private set; }

    Cached transform of this UI control.

  • public string Name { get; set; }

    Name of the UI control (maps to GameObject name).

  • public bool Visible { get; private set; }

    True if the UI is currently visible.

  • internal CircularBuffer.Node CurrentNode { get; set; }

    Used by UIManager's circle buffer to assist in opening/closing UI windows.

Methods

  • public virtual void OnStarting()

    Called at the end of the MonoBehaviour Awake function. Override for custom startup logic.

  • public void AdjustPositionForPivotChange(RectTransform rectTransform, Vector2 newPivot)

    Adjusts the RectTransform's local position to keep its visual position the same when its pivot is changed.

  • public void AdjustPositionForAnchorChange(RectTransform rectTransform, Vector2 newAnchorMin, Vector2 newAnchorMax)

    Adjusts the RectTransform's position to keep its visual position the same when its anchors are changed.

  • public void ClampUIToScreen(float x, float y, bool ignoreDimensions = false)

    Clamps the UI panel's position to the screen bounds, preventing it from being dragged off-screen.

  • public virtual void OnQuitToLogin()

    Called when quitting to the login menu. Stops all coroutines for this UI control.

  • public void SetClient(Client client)

    Injects the Client instance for network/UI interaction. Handles cleanup and event registration.

  • public virtual void OnClientSet()

    Called when the Client is set via SetClient. Override to handle custom logic on client assignment.

  • public virtual void OnClientUnset()

    Called when the Client is unset or removed. Override to handle custom cleanup logic.

  • public virtual void OnDestroying()

    Called at the start of the MonoBehaviour OnDestroy function. Override for custom cleanup logic.

  • public void UIManager_OnAdd(CircularBuffer.Node node)

    Called by UIManager when this control is added to the circular buffer. Stores the buffer node reference.

  • public void UIManager_OnRemove()

    Called by UIManager when this control is removed from the circular buffer. Clears the buffer node reference.

  • public void OnPointerEnter(PointerEventData eventData)

    Called when the pointer enters the UI control. Sets HasFocus to true.

  • public void OnPointerExit(PointerEventData eventData)

    Called when the pointer exits the UI control. Sets HasFocus to false and invokes OnLoseFocus event.

  • public virtual void ToggleVisibility()

    Toggles the visibility of the UI control.

  • public virtual void Show()

    Shows the UI control if it is not already visible.

  • public virtual void Hide()

    Hides the UI control, unless IsAlwaysOpen is true.

  • public virtual void Hide(bool overrideIsAlwaysOpen)

    Hides the UI control, unless overrideIsAlwaysOpen is true.

  • public virtual void OnResetPosition()

    Resets the UI control's position to its starting position.

  • public void OnPointerDown(PointerEventData data)

    Called when the pointer is pressed down on the UI control. Handles drag and focus logic.

  • public void OnPointerUp(PointerEventData data)

    Called when the pointer is released on the UI control. Ends drag operation.

  • public void OnDrag(PointerEventData data)

    Called when the UI control is dragged. Updates position if dragging is enabled.

  • public void ResetPosition()

    Resets the UI control's position and drag state to the initial values.


Basic Usage

Setup

  1. Inherit from UIControl for all custom UI scripts.
  2. Assign required UI references (MainPanel, Canvas, etc.) in the Unity Inspector.
  3. Use the provided methods for showing, hiding, and managing UI state.
  4. Integrate with UIManager and Client as needed for your UI system.

Example

// Example: Creating a custom UI control
public class MyCustomUI : UIControl
{
    public override void OnStarting()
    {
        // Custom initialization logic
    }

    public void OpenPanel()
    {
        Show();
    }

    public void ClosePanel()
    {
        Hide();
    }
}

Best Practices

  • Always inherit from UIControl for consistency and access to core UI features.
  • Assign all required references in the Inspector to avoid null reference errors.
  • Use the provided visibility and focus methods; avoid manipulating GameObject state directly.
  • Clean up listeners and references in OnDestroying to prevent memory leaks.
  • Use ClampUIToScreen and drag options for user-friendly, movable UI panels.
⚠️ **GitHub.com Fallback** ⚠️