How to scroll to a specific element in ScrollRect with Unity UI?

后端 未结 10 2272
野的像风
野的像风 2021-02-12 04:03

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)

10条回答
  •  花落未央
    2021-02-12 04:30

    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);
        }
    

提交回复
热议问题