ScrollTo(VisualElement)
Scroll to a specific child element.
Read time 1 minuteLast updated 6 days ago
Definition
- Type: Method
- Namespace: UnityEngine.UIElements
- Assembly: UnityEngine.UIElementsModule
public void ScrollTo(VisualElement child)
Parameters
The child to scroll to.
Remarks
This example creates a ScrollView that contains multiple labels. A Button is used to scroll to a selected label.
Examples
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]); }}