Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


Scroller

A vertical or horizontal scrollbar. For more information, refer to UXML element Scroller.
Read time 9 minutesLast updated 5 days ago

Definition

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

Remarks

Each ScrollView has a Scroller for each axis, controlled by ScrollerVisibility. For more information, refer to ScrollView. If a Scroller is used in ScrollView, its _highValue and _lowValue are automatically overridden to match the ScrollView content size on GeometryChangedEvent. A Scroller contains a Slider and two RepeatButtons for scrolling.
The following example creates a scroller independently from a ScrollView:

Examples

using System;using UnityEngine;using UnityEngine.UIElements;[RequireComponent(typeof(UIDocument))]public class Scroller_Example : 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 minXValue = 20; public int maXValue = 80; private void Start() { var ballBlocker = new VisualElement(); ballBlocker.style.width = 20; ballBlocker.style.height = 10; ballBlocker.style.backgroundColor = Color.red; var horizontalScroller = new Scroller(minXValue, maXValue, null, SliderDirection.Horizontal); var minLabel = new Label($"Minimum Value: {minXValue}"); var maxLabel = new Label($"Maximum Value: {maXValue}"); var ballBlockerLabel = new Label($"Ball Blocker Position: {ballBlocker.style.left}"); ballBlocker.style.left = horizontalScroller.value; // The width of the Scroller is independent of its value range. horizontalScroller.style.width = 200; horizontalScroller.valueChanged += (value) => { ballBlocker.style.left = value; ballBlockerLabel.text = $"Ball Blocker Position: {ballBlocker.style.left}"; }; uiDocument.rootVisualElement.Add(ballBlocker); uiDocument.rootVisualElement.Add(horizontalScroller); uiDocument.rootVisualElement.Add(minLabel); uiDocument.rootVisualElement.Add(maxLabel); uiDocument.rootVisualElement.Add(ballBlockerLabel); }}

Static Fields

Value

Description

highButtonUssClassNameUSS class name of high buttons in elements of this type.
horizontalVariantUssClassNameUSS class name of elements of this type, when they are displayed horizontally.
lowButtonUssClassNameUSS class name of low buttons in elements of this type.
sliderUssClassNameUSS class name of slider elements in elements of this type.
ussClassNameUSS class name of elements of this type.
verticalVariantUssClassNameUSS class name of elements of this type, when they are displayed vertically.

Constructors

Properties

Property

Description

directionDirection of this scrollbar.
highButtonTop or right scroll button.
highValueMaximum value.
lowButtonBottom or left scroll button.
lowValueMinimum value.
sliderThe slider used by this scroller.
valueValue that defines the slider position. It lies between _lowValue and _highValue.

Methods

Method

Description

AdjustUpdates the slider element size as a ratio of total range. A value greater than or equal to 1 will disable the Scroller.
ScrollPageDownWill change the value according to the current slider pageSize.
ScrollPageUpWill change the value according to the current slider pageSize.

Events

Member

Description

valueChangedEvent sent when the slider value has changed.

Inheritance

Inherited Members