protected new void SetChildrenAlongAxis(int axis, bool isVertical) { float size = rectTransform.rect.size[axis]; float innerSize = size - (axis == 0 ? padding.horizontal : padding.vertical); bool alongOtherAxis = (isVertical ^ (axis == 1)); bool controlSize = (axis == 0 ? m_ChildControlWidth : m_ChildControlHeight); bool childForceExpandSize = (axis == 0 ? m_ChildForceExpandWidth : m_ChildForceExpandHeight); //Calculate initial position float pos = (axis == 0 ? padding.left : padding.top); float surplusSpace = size - GetTotalPreferredSize(axis); if (surplusSpace > 0) { if (GetTotalFlexibleSize(axis) == 0) { if (!controlSize || !childForceExpandSize) { pos = GetStartOffset(axis, GetTotalPreferredSize(axis) - (axis == 0 ? padding.horizontal : padding.vertical)); } } } for (int i = 0; i < _Groups.Count; i++) { LinearGroup group = _Groups[i]; var groupSize = group.size; groupSize[axis] = alongOtherAxis ? Mathf.Max(group.GetTotalMinSize(axis), group.GetTotalPreferredSize(axis)) : innerSize; group.size = groupSize; var groupPosition = group.position; groupPosition[axis] = pos; group.position = groupPosition; SetChildrenAlongGroupAxis(axis, isVertical, group); if (alongOtherAxis) { pos += groupSize[axis] + spacingBetween; } } }
protected new void CalcAlongAxis(int axis, bool isVertical) { float combinedPadding = (axis == 0 ? padding.horizontal : padding.vertical); bool controlSize = (axis == 0 ? m_ChildControlWidth : m_ChildControlHeight); bool useScale = (axis == 0 ? m_ChildScaleWidth : m_ChildScaleHeight); bool childForceExpandSize = (axis == 0 ? m_ChildForceExpandWidth : m_ChildForceExpandHeight); float totalMin = combinedPadding; float totalPreferred = combinedPadding; float totalFlexible = 0; bool alongOtherAxis = (isVertical ^ (axis == 1)); var axisSpacing = alongOtherAxis ? spacingBetween : spacing; for (int i = 0; i < _Groups.Count; i++) { LinearGroup group = _Groups[i]; CalcAlongGroupAxis(axis, isVertical, group); float min = group.GetTotalMinSize(axis); float preferred = group.GetTotalPreferredSize(axis); float flexible = group.GetTotalFlexibleSize(axis); totalMin += min + axisSpacing; totalPreferred += preferred + axisSpacing; // Increment flexible size with element's flexible size. totalFlexible += flexible; } if (!alongOtherAxis && rectChildren.Count > 0) { totalMin -= axisSpacing; totalPreferred -= axisSpacing; } totalPreferred = Mathf.Max(totalMin, totalPreferred); SetLayoutInputForAxis(totalMin, totalPreferred, totalFlexible, axis); }
protected void SetChildrenAlongGroupAxis(int axis, bool isVertical, LinearGroup group) { float size = group.size[axis]; bool controlSize = (axis == 0 ? m_ChildControlWidth : m_ChildControlHeight); bool useScale = (axis == 0 ? m_ChildScaleWidth : m_ChildScaleHeight); bool childForceExpandSize = (axis == 0 ? m_ChildForceExpandWidth : m_ChildForceExpandHeight); float alignmentOnAxis = GetAlignmentOnAxis(axis); bool alongOtherAxis = (isVertical ^ (axis == 1)); if (alongOtherAxis) { float pos = group.position[axis]; for (int i = 0; i < group.RectChildren.Count; i++) { RectTransform child = group.RectChildren[i]; float min, preferred, flexible; GetChildSizes(child, axis, controlSize, childForceExpandSize, out min, out preferred, out flexible); float scaleFactor = useScale ? child.localScale[axis] : 1f; float requiredSpace = Mathf.Clamp(size, min, flexible > 0 ? size : preferred); float startOffset = pos + group.GetStartOffset(axis, requiredSpace * scaleFactor, alignmentOnAxis); if (controlSize) { SetChildAlongAxisWithScale(child, axis, startOffset, requiredSpace, scaleFactor); } else { float offsetInCell = (requiredSpace - child.sizeDelta[axis]) * alignmentOnAxis; SetChildAlongAxisWithScale(child, axis, startOffset + offsetInCell, scaleFactor); } } } else { float pos = group.position[axis]; float itemFlexibleMultiplier = 0; float surplusSpace = size - group.GetTotalPreferredSize(axis); var useFlexibleSpacing = false; if (surplusSpace > 0) { if (group.GetTotalFlexibleSize(axis) == 0) { if (!controlSize || !childForceExpandSize) { pos += group.GetStartOffset(axis, group.GetTotalPreferredSize(axis), alignmentOnAxis); } else { useFlexibleSpacing = true; } } else if (group.GetTotalFlexibleSize(axis) > 0) { itemFlexibleMultiplier = surplusSpace / group.GetTotalFlexibleSize(axis); } } float minMaxLerp = 0; if (group.GetTotalMinSize(axis) != group.GetTotalPreferredSize(axis)) { minMaxLerp = Mathf.Clamp01((size - group.GetTotalMinSize(axis)) / (group.GetTotalPreferredSize(axis) - group.GetTotalMinSize(axis))); } var currentSpacing = useFlexibleSpacing && group.RectChildren.Count - 1 > 0 ? Mathf.Max(spacing, surplusSpace / (group.RectChildren.Count - 1)) : spacing; for (int i = 0; i < group.RectChildren.Count; i++) { RectTransform child = group.RectChildren[i]; float min, preferred, flexible; GetChildSizes(child, axis, controlSize, childForceExpandSize, out min, out preferred, out flexible); float scaleFactor = useScale ? child.localScale[axis] : 1f; float childSize = Mathf.Lerp(min, preferred, minMaxLerp); childSize += flexible * itemFlexibleMultiplier; if (controlSize) { SetChildAlongAxisWithScale(child, axis, pos, childSize, scaleFactor); } else { float offsetInCell = (childSize - child.sizeDelta[axis]) * alignmentOnAxis; SetChildAlongAxisWithScale(child, axis, pos + offsetInCell, scaleFactor); } pos += childSize * scaleFactor + currentSpacing; } } }