如何使用 Unity UI 滚动到 ScrollRect 中的特定元素? [英] How to scroll to a specific element in ScrollRect with Unity UI?

查看:59
本文介绍了如何使用 Unity UI 滚动到 ScrollRect 中的特定元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Unity 5.1 为手机游戏构建了一个注册表单.为此,我使用 Unity UI 组件:ScrollRect + Autolayout(垂直布局)+ Text(标签)+ Input Field.这部分工作正常.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆