当占用大量空间的内容需要在小区域中显示时,可使用滚动矩形。滚动矩形提供了滚动此内容的功能。
通常情况下,滚动矩形与遮罩 (Mask) 相结合来创建滚动视图,在产生的视图中只有滚动矩形内的可滚动内容为可见状态。此外,滚动矩形还可与一个或两个可拖动以便水平或垂直滚动的滚动条 (Scrollbar) 组合使用。
属性: | 功能: |
---|---|
Content | 这是对需要滚动的 UI 元素(例如大型图像)的矩形变换的引用。 |
Horizontal | 启用水平滚动 |
Vertical | 启用垂直滚动 |
Movement Type | Unrestricted、Elastic 或 Clamped。使用 Elastic 或 Clamped 可强制内容保持在滚动矩形的边界内。Elastic 模式在内容到达滚动矩形边缘时弹回内容 |
Elasticity | 这是弹性模式中使用的反弹量。 |
Inertia | 如果设置 Inertia,则拖动指针再松开时内容将继续移动。如果未设置 Inertia,则只有进行拖动时内容才移动。 |
Deceleration Rate | 设置 Inertia 的情况下,减速率 (Deceleration Rate) 决定了内容停止移动的速度。速率为 0 将立即停止移动。值为 1 表示移动永不减速。 |
Scroll Sensitivity | 对滚轮和触控板滚动事件的敏感性。 |
Viewport | 对作为内容矩形变换父项的视口矩形变换的引用。 |
Horizontal Scrollbar | 对水平滚动条元素的引用(可选)。 |
Visibility | 滚动条是否应在不需要时自动隐藏以及(可选)是否还展开视口。 |
Spacing | 滚动条与视口之间的空间。 |
Vertical Scrollbar | 对垂直滚动条元素的引用(可选)。 |
Visibility | 滚动条是否应在不需要时自动隐藏以及(可选)是否还展开视口。 |
Spacing | 滚动条与视口之间的空间。 |
属性: | 功能: |
---|---|
On Value Changed | 滚动矩形的滚动位置发生变化时调用的 UnityEvent。该事件可将当前滚动位置作为 Vector2 类型动态参数发送。 |
滚动视图中的重要元素包括视口、滚动内容以及可选的一个或两个滚动条。
下图显示了视口是滚动视图根节点的子项的设置。使用 GameObject > UI > Scroll View 菜单选项时,默认情况下会采用此设置。
要滚动内容,必须从 ScrollRect 的边界内部而不是内容本身接收输入。
使用 Unrestricted 移动类型时要小心,因为可能会以无法挽回的方式失去对内容的控制。使用 Elastic 或 Constrained 移动类型时,最好调整内容位置以确保其在 ScrollRect 的边界内开始移动,否则当 RectTransform 尝试将内容恢复到其边界内时,可能会发生意外行为。
可选择性地将滚动矩形链接到水平和/或垂直滚动条。这些控件通常作为视口的同级放置在层级视图中,并且当存在这些控件时,应分别拖动到滚动矩形的 Horizontal Scrollbar 和 Vertical Scrollbar 属性中。请注意,此类水平滚动条上的 Direction 属性应设置为 Left To Right,而垂直滚动条上应设置为 Bottom To Top。
如果由于滚动条大小不超过视口而不需要滚动内容,可选择让滚动条自动隐藏。请注意,自动隐藏仅在播放模式下发生。在编辑模式下,始终显示滚动条。这样可以防止在不应该的情况下将场景标记为“脏”,并且还有助于让创建的内容即使在显示滚动条时仍有空间。
如果其中一个滚动条或两个滚动条的可见性行为都设置为 Auto Hide And Expand View,则在隐藏滚动条时会自动展开视口,以便占用滚动条原本使用的额外空间。如果使用此设置,视图的位置和大小将由滚动矩形决定,并且水平滚动条的宽度以及垂直滚动条的高度也由滚动矩形决定。使用此设置时,视口以及滚动条都必须是滚动矩形根游戏对象的子项。
ScrollRect
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.