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

后端 未结 10 2294
野的像风
野的像风 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条回答
  •  Happy的楠姐
    2021-02-12 04:09

    The preconditions for my version of this problem:

    • The element I want to scroll to should be fully visible (with minimal clearance)
    • The element is a direct child of the scrollRect's content
    • Keep scoll position if element is already fully visible
    • I only care about the vertical dimension

    This is what worked best for me (thanks for the other inspirations):

    // ScrollRect scrollRect;
    // RectTransform element;
    // Fully show `element` inside `scrollRect` with at least 25px clearance
    scrollArea.EnsureVisibility(element, 25);
    

    Using this extension method:

    public static void EnsureVisibility(this ScrollRect scrollRect, RectTransform child, float padding=0)
    {
        Debug.Assert(child.parent == scrollRect.content,
            "EnsureVisibility assumes that 'child' is directly nested in the content of 'scrollRect'");
    
        float viewportHeight = scrollRect.viewport.rect.height;
        Vector2 scrollPosition = scrollRect.content.anchoredPosition;
    
        float elementTop = child.anchoredPosition.y;
        float elementBottom = elementTop - child.rect.height;
    
        float visibleContentTop = -scrollPosition.y - padding;
        float visibleContentBottom = -scrollPosition.y - viewportHeight + padding;
    
        float scrollDelta =
            elementTop > visibleContentTop ? visibleContentTop - elementTop :
            elementBottom < visibleContentBottom ? visibleContentBottom - elementBottom :
            0f;
    
        scrollPosition.y += scrollDelta;
        scrollRect.content.anchoredPosition = scrollPosition;
    }
    

提交回复
热议问题