Version: 2023.2
LanguageEnglish
  • C#

ScrollView.ScrollTo

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Declaration

public void ScrollTo(UIElements.VisualElement child);

Parameters

child The child to scroll to.

Description

Scroll to a specific child element.

This example creates a ScrollView that contains multiple labels. A Button is used to scroll to a selected label.

using UnityEngine;
using UnityEngine.UIElements;

public class ScrollViewScrollToExample : MonoBehaviour { public UIDocument uiDocument; public int numberOfLabels = 100; public int scrollToButton = 50;

Label[] labels;

void Start() { var sv = new ScrollView { name = "My Scroll View" };

labels = new Label[numberOfLabels]; for (int i = 0; i < numberOfLabels; i++) { var label = new Label { text = "Button " + i }; labels[i] = label; sv.Add(label); }

var button = new Button { text = "Scroll to " + scrollToButton }; button.clicked += DoScrollTo;

uiDocument.rootVisualElement.Add(button); uiDocument.rootVisualElement.Add(sv); }

void DoScrollTo() { var sv = uiDocument.rootVisualElement.Q<ScrollView>("My Scroll View"); sv.ScrollTo(labels[scrollToButton]); } }