如何使用Unity UI滚动到ScrollRect中的特定元素? [英] How to scroll to a specific element in ScrollRect with Unity UI?
问题描述
我使用Unity 5.1为手机游戏建立了注册表. 为此,我使用 Unity UI 组件:ScrollRect +自动布局(垂直布局)+文本(标签)+输入字段. 这部分工作正常.
I built a registration form for a mobile game using Unity 5.1. To do that, I use Unity UI components: ScrollRect + Autolayout (Vertical layout) + Text (labels) + Input Field. This part works fine.
但是,当打开键盘时,所选字段位于键盘下方.是否可以通过编程方式滚动表单以使选定字段显示在视图中?
But, when keyboard is opened, the selected field is under keyboard. Is there a way to programmatically scroll the form to bring the selected field into view?
我尝试使用ScrollRect.verticalNormalizedPosition
,滚动一些内容效果很好,但是我无法使所选字段出现在我想要的位置.
I have tried using ScrollRect.verticalNormalizedPosition
and it works fine to scroll some, however I am not able to make selected field appear where I want.
感谢您的帮助!
推荐答案
我将为您提供我的代码段,因为我希望自己能有所帮助.希望这会有所帮助!
I am going to give you a code snippet of mine because i feel like being helpful. Hope this helps!
protected ScrollRect scrollRect;
protected RectTransform contentPanel;
public void SnapTo(RectTransform target)
{
Canvas.ForceUpdateCanvases();
contentPanel.anchoredPosition =
(Vector2)scrollRect.transform.InverseTransformPoint(contentPanel.position)
- (Vector2)scrollRect.transform.InverseTransformPoint(target.position);
}
这篇关于如何使用Unity UI滚动到ScrollRect中的特定元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!