child | 要滚动到的子元素。 |
滚动到特定的子元素。
此示例创建一个包含多个标签的 ScrollView。使用 Button 滚动到选定的标签。
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]); } }