child | The child to scroll to. |
Scroll to a specific child element.
This example creates a ScrollView that contains multiple labels. A Button is added that can be 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]); } }