private static Vector2 CalculateVisibleAnchoredPosition(ScrollRect sr, RectTransform contentRtx, Vector3 childWorldPos, Vector2 childSize, Vector2 childPivot, Rect viewRect) { if ((sr.horizontal && sr.vertical) || (!sr.horizontal && !sr.vertical)) { MfLog.Error(LC.Trace, "Tried to scroll to a child on a ScrollRect that can scroll both horizontal and " + "vertical!"); return(contentRtx.anchoredPosition); } LayoutRebuilder.ForceRebuildLayoutImmediate((RectTransform)sr.transform); if (sr.horizontal) { // This will not work when invoked during an Awake() float xDist = sr.ChildPosInContentRect(childWorldPos).x; float targetXPosLeft = xDist + (childSize.x * childPivot.x) // Add the influence of a pivot on child if one present - (contentRtx.rect.width * contentRtx.pivot.x) // Subtract the influence of the content's pivot and the anchor of the parent rects - (((RectTransform)contentRtx.parent.transform).rect.width * contentRtx.anchorMin.x) + viewRect.x; float targetXPosRight = targetXPosLeft + viewRect.width - childSize.x; return(sr.GetClosestContentAnchoredPositionBounded(new Vector2(targetXPosLeft, sr.content.anchoredPosition.y), new Vector2(targetXPosRight, sr.content.anchoredPosition.y))); } else { float yDist = sr.ChildPosInContentRect(childWorldPos).y; float targetYPosTop = yDist - (childSize.y * (1.0f - childPivot.y)) // Subtract the influence of a pivot on child if one present + (contentRtx.rect.height * (1.0f - contentRtx.pivot.y)) // Add the influence of the content's pivot and the anchor of the parent rects + (((RectTransform)contentRtx.parent.transform).rect.height * (1.0f - contentRtx.anchorMin.y)) - viewRect.y; float targetYPosBottom = targetYPosTop - viewRect.height + childSize.y; return(sr.GetClosestContentAnchoredPositionBounded(new Vector2(sr.content.anchoredPosition.x, targetYPosTop), new Vector2(sr.content.anchoredPosition.x, targetYPosBottom))); } }