logo SERENITY
Game Systems Foundation
Search Results for

    Show / Hide Table of Contents

    Class UnityUiUtils

    Helpers for Unity UI (uGUI) operations

    Inheritance
    object
    UnityUiUtils
    Inherited Members
    object.ToString()
    object.Equals(object)
    object.Equals(object, object)
    object.ReferenceEquals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    Namespace: Serenity.Global.Infrastructure.Utils
    Assembly: Serenity.UnityGlobal.Infrastructure.dll
    Syntax
    public static class UnityUiUtils

    Methods

    AddReferenceResolutionScaler(GameObject, Vector2, float)

    Adds a UnityEngine.UI.CanvasScaler that scales the canvas with the screen from a reference resolution, following the width at matchWidthOrHeight 0 and the height at 1. Does nothing and returns null without a canvas object.

    Declaration
    public static CanvasScaler AddReferenceResolutionScaler(GameObject canvasObject, Vector2 referenceResolution, float matchWidthOrHeight)
    Parameters
    Type Name Description
    GameObject canvasObject
    Vector2 referenceResolution
    float matchWidthOrHeight
    Returns
    Type Description
    CanvasScaler

    CalculateBoundingRect(RectTransform[])

    Declaration
    public static RectTransform CalculateBoundingRect(RectTransform[] rectTransforms)
    Parameters
    Type Name Description
    RectTransform[] rectTransforms
    Returns
    Type Description
    RectTransform

    IsStretchedX(RectTransform)

    Declaration
    public static bool IsStretchedX(RectTransform rt)
    Parameters
    Type Name Description
    RectTransform rt
    Returns
    Type Description
    bool

    IsStretchedY(RectTransform)

    Declaration
    public static bool IsStretchedY(RectTransform rt)
    Parameters
    Type Name Description
    RectTransform rt
    Returns
    Type Description
    bool

    MeasureStackedRowExtent(RectTransform, int, float)

    Measures how tall the first rowCount visible rows of a stacked container are, including the layout group's spacing and vertical padding.

    Declaration
    public static float MeasureStackedRowExtent(RectTransform container, int rowCount, float fallbackSpacing)
    Parameters
    Type Name Description
    RectTransform container

    The container whose children are measured.

    int rowCount

    How many rows to include.

    float fallbackSpacing

    Spacing used when the container carries no layout group.

    Returns
    Type Description
    float

    The measured height, or 0 when there is nothing to measure.

    Remarks

    Only active children count: a pooled list keeps its surplus rows alive but deactivated, and measuring those would clamp the viewport to a height the player never sees filled.

    PinScrollViewportHeight(ScrollRect, float)

    Pins a scroll viewport to a fixed height so its layout parent cannot stretch it back open.

    Declaration
    public static void PinScrollViewportHeight(ScrollRect scrollRect, float height)
    Parameters
    Type Name Description
    ScrollRect scrollRect

    The viewport's ScrollRect.

    float height

    The height to pin, in pixels. Ignored when not positive.

    SafeSetHeight(RectTransform, float)

    Declaration
    public static void SafeSetHeight(RectTransform rt, float height)
    Parameters
    Type Name Description
    RectTransform rt
    float height

    SafeSetWidth(RectTransform, float)

    Declaration
    public static void SafeSetWidth(RectTransform rt, float width)
    Parameters
    Type Name Description
    RectTransform rt
    float width

    StretchToParent(RectTransform, Vector2, Vector2)

    Stretches a rect over its whole parent, keeping the given insets from the bottom-left and the top-right edges. Does nothing for a null rect.

    Declaration
    public static void StretchToParent(RectTransform rt, Vector2 offsetMin = null, Vector2 offsetMax = null)
    Parameters
    Type Name Description
    RectTransform rt

    The rect to stretch.

    Vector2 offsetMin

    Inset from the parent's bottom-left corner.

    Vector2 offsetMax

    Inset from the parent's top-right corner (negative values pull it inwards).

    WaitForComponent<T>(MonoBehaviour, GameObject, Action<T>, float)

    Declaration
    public static IEnumerator WaitForComponent<T>(MonoBehaviour context, GameObject root, Action<T> onFound, float timeout = 2) where T : Component
    Parameters
    Type Name Description
    MonoBehaviour context
    GameObject root
    Action<T> onFound
    float timeout
    Returns
    Type Description
    IEnumerator
    Type Parameters
    Name Description
    T

    WrapInVerticalScrollViewport(RectTransform, string)

    Wraps a vertically stacked container in a masked, clamped UnityEngine.UI.ScrollRect viewport, so a list that outgrows its panel scrolls instead of spilling off-screen.

    Declaration
    public static ScrollRect WrapInVerticalScrollViewport(RectTransform content, string viewportName)
    Parameters
    Type Name Description
    RectTransform content

    The container holding the rows. Must already have a parent.

    string viewportName

    Name given to the created viewport object.

    Returns
    Type Description
    ScrollRect

    The configured ScrollRect, or null when content is null or unparented.

    Remarks

    The container is reparented under a new viewport object that takes its place in the parent's layout, keeping the sibling order the panel authored. A UnityEngine.UI.ContentSizeFitter is added to the container when it has none, because a ScrollRect's content must size itself for the scrollable extent to be known at all.

    Shared by every panel that renders a growable row list — the leaderboard's ranked entries and the achievements listing — because the viewport wiring is one piece of knowledge, and two hand-written copies drift into two different scroll behaviours.

    In this article
    © 2026 Serenity. All Rights Reserved