Unity UI Toolkit provides built-in support for auto-sizing text elements, allowing text to automatically scale to fit within its container.
For the most up-to-date information, see the official Unity documentation:
Unity Manual: Auto-sizing text elements
Auto-sizing text adjusts the font size dynamically based on the available space, ensuring text remains readable and fits within defined boundaries. This is useful for:
.auto-size-label {
-unity-font-size-auto-sizing: true;
}Control the range of font sizes the auto-sizing algorithm will use:
.auto-size-label {
-unity-font-size-auto-sizing: true;
-unity-font-size-auto-sizing-min-size: 12px;
-unity-font-size-auto-sizing-max-size: 48px;
}Controls the granularity of size adjustments:
.auto-size-label {
-unity-font-size-auto-sizing: true;
-unity-font-size-auto-sizing-step-size: 1px;
}.dynamic-title {
-unity-font-size-auto-sizing: true;
-unity-font-size-auto-sizing-min-size: 16px;
-unity-font-size-auto-sizing-max-size: 64px;
-unity-font-size-auto-sizing-step-size: 2px;
/* Container must have defined dimensions */
width: 300px;
height: 100px;
/* Other styling */
-unity-text-align: middle-center;
white-space: normal;
}<ui:Label
text="This text will auto-size"
class="dynamic-title"
/>Label label = new Label("Dynamic text");
label.style.unityFontSizeAutoSizing = true;
label.style.unityFontSizeAutoSizingMinSize = 12;
label.style.unityFontSizeAutoSizingMaxSize = 48;Container Size Required: Auto-sizing only works when the container has defined dimensions. Without explicit width/height, the text cannot determine how to scale.
White Space: Set white-space: normal if you want text to wrap before scaling down.
Performance: Auto-sizing recalculates on layout changes. Avoid using it on rapidly changing text.
Rich Text: Works with rich text tags, but complex formatting may affect sizing calculations.
Settings
Theme
Light
Contrast
Material
Dark
Dim
Material Dark
System
Sidebar(Light & Contrast only)
Font Family
DM Sans
Wix
Inclusive Sans
AR One Sans
Direction