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

后端 未结 10 2280
野的像风
野的像风 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:22

    Vertical adjustment:

    [SerializeField]
    private ScrollRect _scrollRect;
    
    private void ScrollToCurrentElement()
    {
        var siblingIndex = _currentListItem.transform.GetSiblingIndex();
    
        float pos = 1f - (float)siblingIndex / _scrollRect.content.transform.childCount;
    
        if (pos < 0.4)
        {
            float correction = 1f / _scrollRect.content.transform.childCount;
            pos -= correction;
        }
    
        _scrollRect.verticalNormalizedPosition = pos;
    } 
    

提交回复
热议问题