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
- Type: Class
- Namespace: UnityEngine.UIElements
- Assembly: UnityEngine.UIElementsModule
- Inherits from: VisualElement
- Implements: IEventHandler, IResolvedStyle, ITransform, ITransitionAnimations, IExperimentalFeatures, IVisualElementScheduler, ICustomStyle
[UxmlElement(libraryPath = "Containers")][Icon("UIToolkit/Icons/ScrollView.png")]public class ScrollView : VisualElement, IEventHandler, IResolvedStyle, ITransform, ITransitionAnimations, IExperimentalFeatures, IVisualElementScheduler, ICustomStyle
Remarks
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 |
|---|---|
| contentAndVerticalScrollUssClassName | USS class name of content elements in elements of this type. |
| contentUssClassName | USS class name of content elements in elements of this type. |
| horizontalVariantContentUssClassName | USS class name that's added when the ContentContainer is in horizontal mode. ScrollViewMode.Horizontal |
| horizontalVariantUssClassName | USS class name that's added when the ScrollView is in horizontal mode. ScrollViewMode.Horizontal |
| horizontalVariantViewportUssClassName | USS class name that's added when the Viewport is in horizontal mode. ScrollViewMode.Horizontal |
| hScrollerUssClassName | USS class name of horizontal scrollers in elements of this type. |
| scrollingUssClassName | USS 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. |
| ussClassName | USS class name of elements of this type. |
| verticalHorizontalVariantContentUssClassName | USS class name that's added when the ContentContainer is in both horizontal and vertical mode. ScrollViewMode.VerticalAndHorizontal |
| verticalHorizontalVariantUssClassName | USS class name that's added when the ScrollView is in both horizontal and vertical mode. ScrollViewMode.VerticalAndHorizontal |
| verticalHorizontalVariantViewportUssClassName | USS class name that's added when the Viewport is in both horizontal and vertical mode. ScrollViewMode.VerticalAndHorizontal |
| verticalVariantContentUssClassName | USS class name that's added when the ContentContainer is in vertical mode. ScrollViewMode.Vertical |
| verticalVariantUssClassName | USS class name that's added when the ScrollView is in vertical mode. ScrollViewMode.Vertical |
| verticalVariantViewportUssClassName | USS class name that's added when the Viewport is in vertical mode. ScrollViewMode.Vertical |
| viewportUssClassName | USS class name of viewport elements in elements of this type. |
| vScrollerUssClassName | USS class name of vertical scrollers in elements of this type. |
Constructors
Constructor | Description |
|---|---|
| ScrollView() | Constructor. |
| ScrollView(UnityEngine.UIElements.ScrollViewMode) | Constructor. |
Properties
Property | Description |
|---|---|
| contentContainer | Contains full content, potentially partially visible. |
| contentViewport | Represents the visible part of contentContainer. |
| elasticAnimationIntervalMs | The minimum amount of time, in milliseconds, between executions of elastic spring animation. |
| elasticity | The amount of elasticity to use when a user tries to scroll past the boundaries of the scroll view. |
| horizontalPageSize | This 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. |
| horizontalScroller | Horizontal scrollbar. |
| horizontalScrollerVisibility | Specifies whether the horizontal scroll bar is visible. |
| mode | Controls how the ScrollView allows the user to scroll the contents. ScrollViewMode |
| mouseWheelScrollSize | This property controls the scrolling speed only when using a mouse scroll wheel, based on the size of the page. |
| nestedInteractionKind | The behavior to use when scrolling reaches limits of a nested ScrollView. |
| scrollDecelerationRate | Controls the rate at which the scrolling movement slows after a user scrolls using a touch interaction. |
| scrollOffset | The current scrolling position. |
| touchScrollBehavior | The behavior to use when a user tries to scroll past the boundaries of the ScrollView content using a touch interaction. |
| verticalPageSize | This 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. |
| verticalScroller | Gets the vertical scrollbar for the scroll view. |
| verticalScrollerVisibility | Specifies whether the vertical scroll bar is visible. |
Methods
Method | Description |
|---|---|
| ScrollTo | Scroll to a specific child element. |
Enums
Enum | Description |
|---|---|
| ScrollView.NestedInteractionKind | Options for controlling how nested ScrollView handles scrolling when reaching the limits of the scrollable area. |
| ScrollView.TouchScrollBehavior | The behavior to use when a user tries to scroll past the end of the ScrollView content using a touch interaction. |