/// <summary> /// Append an item to the scroll view, assuming a vertical item layout. /// </summary> /// <param name="scroll">The scroll view object to operate on.</param> /// <param name="contentChild">The child object to append. Must support <see cref="RectTransform"/> /// <param name="subtractVerticalScrollBar">Allow for the width of the vertical scroll bar /// such that it does not overlap the children's width?</param> /// <remarks> /// The child is arranged below all the exising child items. /// </remarks> public static void AppendContentV(this ScrollRect scroll, RectTransform contentChild, bool subtractVerticalScrollBar = false) { if (contentChild == null || scroll.content == null) { return; } float totalHeight = 0; float height = 0; float scrollWidth = (subtractVerticalScrollBar) ? scroll.GetVerticalScrollbarWidth() : 0; RectTransform rectXForm; Transform child; for (int i = 0; i < scroll.content.childCount; ++i) { child = scroll.content.GetChild(i); rectXForm = (child != null) ? child.GetComponent <RectTransform>() : null; if (rectXForm == null || !rectXForm.gameObject.activeSelf) { continue; } // Preserve height. totalHeight += rectXForm.rect.height; } // Preserve content height. height = contentChild.rect.height; contentChild.SetParent(scroll.content.transform, false); contentChild.anchorMin = new Vector2(0, 1); contentChild.anchorMax = new Vector2(1, 1); contentChild.pivot = new Vector2(0, 1); contentChild.offsetMin = new Vector2(0, -totalHeight - height); contentChild.offsetMax = new Vector2(scrollWidth, -totalHeight); if (contentChild.gameObject.activeSelf) { totalHeight += height; } RectTransform scrollContentRect = (scroll.content.transform as RectTransform); scrollContentRect.sizeDelta = new Vector2(scrollContentRect.sizeDelta.x, totalHeight); }