Search Unity

`ScrollTo` doesn't work with Horizontal mode

Discussion in 'UI Toolkit' started by ErnestSurys, Aug 27, 2019.

  1. ErnestSurys

    ErnestSurys

    Joined:
    Jan 18, 2018
    Posts:
    94
    Hi,
    The `ScrollTo` method of `ScrollView` class works only in Vertical mode. I took a peek at the CsReference code:
    Code (CSharp):
    1.  
    2.         public void ScrollTo(VisualElement child)
    3.         {
    4.             if (child == null)
    5.             {
    6.                 throw new ArgumentNullException(nameof(child));
    7.             }
    8.  
    9.             // Child not in content view, no need to continue.
    10.             if (!contentContainer.Contains(child))
    11.                 throw new ArgumentException("Cannot scroll to null child");
    12.  
    13.             float yTransform = contentContainer.transform.position.y * -1;
    14.             float viewMin = contentViewport.layout.yMin + yTransform;
    15.             float viewMax = contentViewport.layout.yMax + yTransform;
    16.  
    17.             float childBoundaryMin = child.layout.yMin;
    18.             float childBoundaryMax = child.layout.yMax;
    19.             if ((childBoundaryMin >= viewMin && childBoundaryMax <= viewMax) || float.IsNaN(childBoundaryMin) || float.IsNaN(childBoundaryMax))
    20.                 return;
    21.  
    22.             bool scrollUpward = false;
    23.             float deltaDistance = childBoundaryMax - viewMax;
    24.             if (deltaDistance < -1)
    25.             {
    26.                 // Direction = upward
    27.                 deltaDistance = viewMin - childBoundaryMin;
    28.                 scrollUpward = true;
    29.             }
    30.  
    31.             float deltaOffset = deltaDistance * verticalScroller.highValue / scrollableHeight;
    32.  
    33.             verticalScroller.value = scrollOffset.y + (scrollUpward ? -deltaOffset : deltaOffset);
    34.             UpdateContentViewTransform();
    35.         }
    and the code suggests that it is by design but there's not even an exception that warns us about the wrong flag.
     
  2. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    499
    ErnestSurys likes this.