Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


ScrollView

Displays its contents inside a scrollable frame. For more information, refer to the ScrollView user manual page.
Read time 12 minutesLast updated 2 days ago

Definition

[UxmlElement(libraryPath = "Containers")][Icon("UIToolkit/Icons/ScrollView.png")]public class ScrollView : VisualElement, IEventHandler, IResolvedStyle, ITransform, ITransitionAnimations, IExperimentalFeatures, IVisualElementScheduler, ICustomStyle

Remarks

Both the ListView and the TreeView contain a ScrollView that you can manipulate through C# code.
This example creates a ScrollView that contains multiple labels and uses a Button to scroll to a selected label.

Examples

using UnityEngine;using UnityEngine.UIElements;[RequireComponent(typeof(UIDocument))]public class ScrollViewScrollToExample : MonoBehaviour{ // To use this example in runtime, add a UIDocument component to your Scene and attach a UXML file to the UIDocument component. // The UXML file can be empty. public UIDocument uiDocument; public int numberOfPotions = 100; public int scrollToButton = 50; Label[] labels; void OnEnable() { var scrollView = new ScrollView { name = "MyPotionsInventory" }; var inventoryColor = new Color(0.65f, 0.3f, 0.2f, 1f); // Create a list of labels representing potions, with a special label for the potion we want to scroll to. labels = new Label[numberOfPotions]; for (int i = 0; i < numberOfPotions; i++) { var label = new Label { text = " Unnamed Potion #" + (i) }; label.style.backgroundColor = inventoryColor; if (scrollToButton == i) { label.text = "Very Important Potion"; label.style.color = Color.yellow; } labels[i] = label; scrollView.Add(label); } // Add a title for the list and make it noticeable. var title = new Label { text = "Potions Inventory" }; title.style.unityFontStyleAndWeight = FontStyle.Bold; title.style.fontSize = 20; title.style.color = inventoryColor; title.style.unityTextAlign = TextAnchor.MiddleCenter; var button = new Button { text = "Click to locate the Very Important Potion in the inventory" }; button.clicked += DoScrollTo; uiDocument.rootVisualElement.Add(title); uiDocument.rootVisualElement.Add(button); uiDocument.rootVisualElement.Add(scrollView); } void DoScrollTo() { var scrollView = uiDocument.rootVisualElement.Q<ScrollView>("MyPotionsInventory"); scrollView.ScrollTo(labels[scrollToButton]); }}

Static Fields

Value

Description

contentAndVerticalScrollUssClassNameUSS class name of content elements in elements of this type.
contentUssClassNameUSS class name of content elements in elements of this type.
horizontalVariantContentUssClassNameUSS class name that's added when the ContentContainer is in horizontal mode. ScrollViewMode.Horizontal
horizontalVariantUssClassNameUSS class name that's added when the ScrollView is in horizontal mode. ScrollViewMode.Horizontal
horizontalVariantViewportUssClassNameUSS class name that's added when the Viewport is in horizontal mode. ScrollViewMode.Horizontal
hScrollerUssClassNameUSS class name of horizontal scrollers in elements of this type.
scrollingUssClassNameUSS class name applied to the ScrollView when it is in navigation-driven scrolling mode. In this mode, directional NavigationMoveEvents scroll the content instead of moving focus. This class is toggled by NavigationSubmitEvent when the ScrollView itself is focused, and is cleared when focus leaves the ScrollView.
ussClassNameUSS class name of elements of this type.
verticalHorizontalVariantContentUssClassNameUSS class name that's added when the ContentContainer is in both horizontal and vertical mode. ScrollViewMode.VerticalAndHorizontal
verticalHorizontalVariantUssClassNameUSS class name that's added when the ScrollView is in both horizontal and vertical mode. ScrollViewMode.VerticalAndHorizontal
verticalHorizontalVariantViewportUssClassNameUSS class name that's added when the Viewport is in both horizontal and vertical mode. ScrollViewMode.VerticalAndHorizontal
verticalVariantContentUssClassNameUSS class name that's added when the ContentContainer is in vertical mode. ScrollViewMode.Vertical
verticalVariantUssClassNameUSS class name that's added when the ScrollView is in vertical mode. ScrollViewMode.Vertical
verticalVariantViewportUssClassNameUSS class name that's added when the Viewport is in vertical mode. ScrollViewMode.Vertical
viewportUssClassNameUSS class name of viewport elements in elements of this type.
vScrollerUssClassNameUSS class name of vertical scrollers in elements of this type.

Constructors

Constructor

Description

ScrollView()Constructor.
ScrollView(UnityEngine.UIElements.ScrollViewMode)Constructor.

Properties

Property

Description

contentContainerContains full content, potentially partially visible.
contentViewportRepresents the visible part of contentContainer.
elasticAnimationIntervalMsThe minimum amount of time, in milliseconds, between executions of elastic spring animation.
elasticityThe amount of elasticity to use when a user tries to scroll past the boundaries of the scroll view.
horizontalPageSizeThis property controls the speed of the horizontal scrolling when using a keyboard or the on-screen scrollbar buttons (arrows and handle), based on the size of the page.
horizontalScrollerHorizontal scrollbar.
horizontalScrollerVisibilitySpecifies whether the horizontal scroll bar is visible.
modeControls how the ScrollView allows the user to scroll the contents. ScrollViewMode
mouseWheelScrollSizeThis property controls the scrolling speed only when using a mouse scroll wheel, based on the size of the page.
nestedInteractionKindThe behavior to use when scrolling reaches limits of a nested ScrollView.
scrollDecelerationRateControls the rate at which the scrolling movement slows after a user scrolls using a touch interaction.
scrollOffsetThe current scrolling position.
touchScrollBehaviorThe behavior to use when a user tries to scroll past the boundaries of the ScrollView content using a touch interaction.
verticalPageSizeThis property controls the speed of the vertical scrolling when using a keyboard or the on-screen scrollbar buttons (arrows and handle), based on the size of the page.
verticalScrollerGets the vertical scrollbar for the scroll view.
verticalScrollerVisibilitySpecifies whether the vertical scroll bar is visible.

Methods

Method

Description

ScrollToScroll to a specific child element.

Enums

Enum

Description

ScrollView.NestedInteractionKindOptions for controlling how nested ScrollView handles scrolling when reaching the limits of the scrollable area.
ScrollView.TouchScrollBehaviorThe behavior to use when a user tries to scroll past the end of the ScrollView content using a touch interaction.

Inheritance

Inherited Members