2015-02-10 08:03:38 +08:00
|
|
|
/// Credit Simie
|
|
|
|
/// Sourced from - http://forum.unity3d.com/threads/flowlayoutgroup.296709/
|
|
|
|
/// Example http://forum.unity3d.com/threads/flowlayoutgroup.296709/
|
2015-09-20 07:24:17 +08:00
|
|
|
/// Update by Martin Sharkbomb - http://forum.unity3d.com/threads/flowlayoutgroup.296709/#post-1977028
|
2020-07-09 03:38:28 +08:00
|
|
|
/// Last item alignment fix by Vicente Russo - https://bitbucket.org/SimonDarksideJ/unity-ui-extensions/issues/22/flow-layout-group-align
|
2017-09-09 08:14:58 +08:00
|
|
|
/// Vertical Flow by Ramon Molossi
|
2015-02-10 08:03:38 +08:00
|
|
|
|
|
|
|
using System.Collections.Generic;
|
2023-03-12 19:44:50 +08:00
|
|
|
using System.Text;
|
2015-02-10 08:03:38 +08:00
|
|
|
|
|
|
|
namespace UnityEngine.UI.Extensions
|
|
|
|
{
|
2017-09-09 08:14:58 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Layout Group controller that arranges children in bars, fitting as many on a line until total size exceeds parent bounds
|
|
|
|
/// </summary>
|
|
|
|
[AddComponentMenu("Layout/Extensions/Flow Layout Group")]
|
2015-02-10 08:03:38 +08:00
|
|
|
public class FlowLayoutGroup : LayoutGroup
|
|
|
|
{
|
2017-09-09 08:14:58 +08:00
|
|
|
public enum Axis { Horizontal = 0, Vertical = 1 }
|
|
|
|
|
2023-03-12 19:44:50 +08:00
|
|
|
private float _layoutHeight;
|
|
|
|
private float _layoutWidth;
|
|
|
|
|
|
|
|
public float SpacingX = 0f;
|
2015-09-20 07:24:17 +08:00
|
|
|
public float SpacingY = 0f;
|
|
|
|
public bool ExpandHorizontalSpacing = false;
|
2015-02-10 08:03:38 +08:00
|
|
|
public bool ChildForceExpandWidth = false;
|
|
|
|
public bool ChildForceExpandHeight = false;
|
2017-09-09 08:14:58 +08:00
|
|
|
public bool invertOrder = false;
|
|
|
|
|
2023-03-12 19:44:50 +08:00
|
|
|
[SerializeField]
|
|
|
|
protected Axis m_StartAxis = Axis.Horizontal;
|
|
|
|
|
|
|
|
public Axis StartAxis { get { return m_StartAxis; } set { SetProperty(ref m_StartAxis, value); } }
|
2017-09-09 08:14:58 +08:00
|
|
|
|
2015-02-10 08:03:38 +08:00
|
|
|
public override void CalculateLayoutInputHorizontal()
|
|
|
|
{
|
2023-03-12 19:44:50 +08:00
|
|
|
if (StartAxis == Axis.Horizontal)
|
|
|
|
{
|
|
|
|
base.CalculateLayoutInputHorizontal();
|
|
|
|
var minWidth = GetGreatestMinimumChildWidth() + padding.left + padding.right;
|
|
|
|
SetLayoutInputForAxis(minWidth, -1, -1, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_layoutWidth = SetLayout(0, true);
|
2017-09-09 08:14:58 +08:00
|
|
|
}
|
|
|
|
|
2015-02-10 08:03:38 +08:00
|
|
|
}
|
2017-09-09 08:14:58 +08:00
|
|
|
|
2015-02-10 08:03:38 +08:00
|
|
|
public override void SetLayoutHorizontal()
|
|
|
|
{
|
2017-09-09 08:14:58 +08:00
|
|
|
SetLayout(0, false);
|
2015-02-10 08:03:38 +08:00
|
|
|
}
|
2017-09-09 08:14:58 +08:00
|
|
|
|
2015-02-10 08:03:38 +08:00
|
|
|
public override void SetLayoutVertical()
|
|
|
|
{
|
2017-09-09 08:14:58 +08:00
|
|
|
SetLayout(1, false);
|
2015-02-10 08:03:38 +08:00
|
|
|
}
|
2017-09-09 08:14:58 +08:00
|
|
|
|
2015-02-10 08:03:38 +08:00
|
|
|
public override void CalculateLayoutInputVertical()
|
|
|
|
{
|
2023-03-12 19:44:50 +08:00
|
|
|
if (StartAxis == Axis.Horizontal)
|
|
|
|
{
|
|
|
|
_layoutHeight = SetLayout(1, true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
base.CalculateLayoutInputHorizontal();
|
|
|
|
var minHeight = GetGreatestMinimumChildHeigth() + padding.bottom + padding.top;
|
|
|
|
SetLayoutInputForAxis(minHeight, -1, -1, 1);
|
2017-09-09 08:14:58 +08:00
|
|
|
}
|
2015-02-10 08:03:38 +08:00
|
|
|
}
|
2017-09-09 08:14:58 +08:00
|
|
|
|
2015-02-10 08:03:38 +08:00
|
|
|
protected bool IsCenterAlign
|
|
|
|
{
|
2023-03-12 19:44:50 +08:00
|
|
|
get => childAlignment == TextAnchor.LowerCenter || childAlignment == TextAnchor.MiddleCenter || childAlignment == TextAnchor.UpperCenter;
|
2015-02-10 08:03:38 +08:00
|
|
|
}
|
2017-09-09 08:14:58 +08:00
|
|
|
|
2015-02-10 08:03:38 +08:00
|
|
|
protected bool IsRightAlign
|
|
|
|
{
|
2023-03-12 19:44:50 +08:00
|
|
|
get => childAlignment == TextAnchor.LowerRight || childAlignment == TextAnchor.MiddleRight || childAlignment == TextAnchor.UpperRight;
|
2015-02-10 08:03:38 +08:00
|
|
|
}
|
2017-09-09 08:14:58 +08:00
|
|
|
|
2015-02-10 08:03:38 +08:00
|
|
|
protected bool IsMiddleAlign
|
|
|
|
{
|
2023-03-12 19:44:50 +08:00
|
|
|
get => childAlignment == TextAnchor.MiddleLeft || childAlignment == TextAnchor.MiddleRight || childAlignment == TextAnchor.MiddleCenter;
|
2015-02-10 08:03:38 +08:00
|
|
|
}
|
2017-09-09 08:14:58 +08:00
|
|
|
|
2015-02-10 08:03:38 +08:00
|
|
|
protected bool IsLowerAlign
|
|
|
|
{
|
2023-03-12 19:44:50 +08:00
|
|
|
get => childAlignment == TextAnchor.LowerLeft || childAlignment == TextAnchor.LowerRight || childAlignment == TextAnchor.LowerCenter;
|
2015-02-10 08:03:38 +08:00
|
|
|
}
|
2017-09-09 08:14:58 +08:00
|
|
|
|
2015-02-10 08:03:38 +08:00
|
|
|
/// <summary>
|
2017-09-09 08:14:58 +08:00
|
|
|
/// Holds the rects that will make up the current bar being processed
|
2015-02-10 08:03:38 +08:00
|
|
|
/// </summary>
|
2023-03-12 19:44:50 +08:00
|
|
|
private readonly IList<RectTransform> _itemList = new List<RectTransform>();
|
2017-09-09 08:14:58 +08:00
|
|
|
|
2015-02-10 08:03:38 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Main layout method
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="width">Width to calculate the layout with</param>
|
|
|
|
/// <param name="axis">0 for horizontal axis, 1 for vertical</param>
|
|
|
|
/// <param name="layoutInput">If true, sets the layout input for the axis. If false, sets child position for axis</param>
|
2017-09-09 08:14:58 +08:00
|
|
|
public float SetLayout(int axis, bool layoutInput)
|
2015-02-10 08:03:38 +08:00
|
|
|
{
|
2017-09-09 08:14:58 +08:00
|
|
|
//container height and width
|
2015-02-10 08:03:38 +08:00
|
|
|
var groupHeight = rectTransform.rect.height;
|
2017-09-09 08:14:58 +08:00
|
|
|
var groupWidth = rectTransform.rect.width;
|
|
|
|
|
|
|
|
float spacingBetweenBars = 0;
|
|
|
|
float spacingBetweenElements = 0;
|
|
|
|
float offset = 0;
|
|
|
|
float counterOffset = 0;
|
|
|
|
float groupSize = 0;
|
|
|
|
float workingSize = 0;
|
2023-03-12 19:44:50 +08:00
|
|
|
if (StartAxis == Axis.Horizontal)
|
|
|
|
{
|
2017-09-09 08:14:58 +08:00
|
|
|
groupSize = groupHeight;
|
|
|
|
workingSize = groupWidth - padding.left - padding.right;
|
2023-03-12 19:44:50 +08:00
|
|
|
if (IsLowerAlign)
|
|
|
|
{
|
2017-09-09 08:14:58 +08:00
|
|
|
offset = (float)padding.bottom;
|
|
|
|
counterOffset = (float)padding.top;
|
2023-03-12 19:44:50 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-09-09 08:14:58 +08:00
|
|
|
offset = (float)padding.top;
|
|
|
|
counterOffset = (float)padding.bottom;
|
|
|
|
}
|
|
|
|
spacingBetweenBars = SpacingY;
|
|
|
|
spacingBetweenElements = SpacingX;
|
2023-03-12 19:44:50 +08:00
|
|
|
}
|
|
|
|
else if (StartAxis == Axis.Vertical)
|
|
|
|
{
|
2017-09-09 08:14:58 +08:00
|
|
|
groupSize = groupWidth;
|
|
|
|
workingSize = groupHeight - padding.top - padding.bottom;
|
2023-03-12 19:44:50 +08:00
|
|
|
if (IsRightAlign)
|
|
|
|
{
|
2017-09-09 08:14:58 +08:00
|
|
|
offset = (float)padding.right;
|
|
|
|
counterOffset = (float)padding.left;
|
2023-03-12 19:44:50 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-09-09 08:14:58 +08:00
|
|
|
offset = (float)padding.left;
|
|
|
|
counterOffset = (float)padding.right;
|
|
|
|
}
|
|
|
|
spacingBetweenBars = SpacingX;
|
|
|
|
spacingBetweenElements = SpacingY;
|
|
|
|
}
|
|
|
|
|
|
|
|
var currentBarSize = 0f;
|
|
|
|
var currentBarSpace = 0f;
|
|
|
|
|
2023-03-12 19:44:50 +08:00
|
|
|
for (var i = 0; i < rectChildren.Count; i++)
|
|
|
|
{
|
2017-09-09 08:14:58 +08:00
|
|
|
int index = i;
|
2023-03-12 19:44:50 +08:00
|
|
|
var child = rectChildren[index];
|
2017-09-09 08:14:58 +08:00
|
|
|
float childSize = 0;
|
|
|
|
float childOtherSize = 0;
|
|
|
|
//get height and width of elements.
|
2023-03-12 19:44:50 +08:00
|
|
|
if (StartAxis == Axis.Horizontal)
|
|
|
|
{
|
|
|
|
if (invertOrder)
|
|
|
|
{
|
|
|
|
index = IsLowerAlign ? rectChildren.Count - 1 - i : i;
|
2017-09-09 08:14:58 +08:00
|
|
|
}
|
2023-03-12 19:44:50 +08:00
|
|
|
child = rectChildren[index];
|
|
|
|
childSize = LayoutUtility.GetPreferredSize(child, 0);
|
|
|
|
childSize = Mathf.Min(childSize, workingSize);
|
|
|
|
childOtherSize = LayoutUtility.GetPreferredSize(child, 1);
|
|
|
|
}
|
|
|
|
else if (StartAxis == Axis.Vertical)
|
|
|
|
{
|
|
|
|
if (invertOrder)
|
|
|
|
{
|
2017-09-09 08:14:58 +08:00
|
|
|
index = IsRightAlign ? rectChildren.Count - 1 - i : i;
|
|
|
|
}
|
2023-03-12 19:44:50 +08:00
|
|
|
child = rectChildren[index];
|
|
|
|
childSize = LayoutUtility.GetPreferredSize(child, 1);
|
|
|
|
childSize = Mathf.Min(childSize, workingSize);
|
|
|
|
childOtherSize = LayoutUtility.GetPreferredSize(child, 0);
|
2017-09-09 08:14:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If adding this element would exceed the bounds of the container,
|
|
|
|
// go to a new bar after processing the current bar
|
2023-03-12 19:44:50 +08:00
|
|
|
if (currentBarSize + childSize > workingSize)
|
|
|
|
{
|
2017-09-09 08:14:58 +08:00
|
|
|
currentBarSize -= spacingBetweenElements;
|
|
|
|
|
|
|
|
// Process current bar elements positioning
|
2023-03-12 19:44:50 +08:00
|
|
|
if (!layoutInput)
|
|
|
|
{
|
|
|
|
if (StartAxis == Axis.Horizontal)
|
|
|
|
{
|
|
|
|
float newOffset = CalculateRowVerticalOffset(groupSize, offset, currentBarSpace);
|
|
|
|
LayoutRow(_itemList, currentBarSize, currentBarSpace, workingSize, padding.left, newOffset, axis);
|
|
|
|
}
|
|
|
|
else if (StartAxis == Axis.Vertical)
|
|
|
|
{
|
|
|
|
float newOffset = CalculateColHorizontalOffset(groupSize, offset, currentBarSpace);
|
|
|
|
LayoutCol(_itemList, currentBarSpace, currentBarSize, workingSize, newOffset, padding.top, axis);
|
2017-09-09 08:14:58 +08:00
|
|
|
}
|
2015-02-10 08:03:38 +08:00
|
|
|
}
|
2017-09-09 08:14:58 +08:00
|
|
|
|
|
|
|
// Clear existing bar
|
2023-03-12 19:44:50 +08:00
|
|
|
_itemList.Clear();
|
2017-09-09 08:14:58 +08:00
|
|
|
|
|
|
|
// Add the current bar space to total barSpace accumulator, and reset to 0 for the next row
|
|
|
|
offset += currentBarSpace;
|
|
|
|
offset += spacingBetweenBars;
|
|
|
|
|
|
|
|
currentBarSpace = 0;
|
|
|
|
currentBarSize = 0;
|
2015-02-10 08:03:38 +08:00
|
|
|
}
|
2017-09-09 08:14:58 +08:00
|
|
|
|
|
|
|
currentBarSize += childSize;
|
2023-03-12 19:44:50 +08:00
|
|
|
_itemList.Add(child);
|
2017-09-09 08:14:58 +08:00
|
|
|
|
2015-02-10 08:03:38 +08:00
|
|
|
// We need the largest element height to determine the starting position of the next line
|
2023-03-12 19:44:50 +08:00
|
|
|
currentBarSpace = childOtherSize > currentBarSpace ? childOtherSize : currentBarSpace;
|
2015-02-10 08:03:38 +08:00
|
|
|
|
2023-03-12 19:44:50 +08:00
|
|
|
currentBarSize += spacingBetweenElements;
|
2015-02-10 08:03:38 +08:00
|
|
|
}
|
2017-09-09 08:14:58 +08:00
|
|
|
|
|
|
|
// Layout the final bar
|
2023-03-12 19:44:50 +08:00
|
|
|
if (!layoutInput)
|
|
|
|
{
|
|
|
|
if (StartAxis == Axis.Horizontal)
|
|
|
|
{
|
|
|
|
float newOffset = CalculateRowVerticalOffset(groupHeight, offset, currentBarSpace);
|
2017-09-09 08:14:58 +08:00
|
|
|
currentBarSize -= spacingBetweenElements;
|
2023-03-12 19:44:50 +08:00
|
|
|
LayoutRow(_itemList, currentBarSize, currentBarSpace, workingSize, padding.left, newOffset, axis);
|
|
|
|
}
|
|
|
|
else if (StartAxis == Axis.Vertical)
|
|
|
|
{
|
2017-09-09 08:14:58 +08:00
|
|
|
float newOffset = CalculateColHorizontalOffset(groupWidth, offset, currentBarSpace);
|
|
|
|
currentBarSize -= spacingBetweenElements;
|
2022-12-24 21:20:19 +08:00
|
|
|
LayoutCol(_itemList, currentBarSpace, currentBarSize, workingSize, newOffset, padding.top, axis);
|
2017-09-09 08:14:58 +08:00
|
|
|
}
|
2015-02-10 08:03:38 +08:00
|
|
|
}
|
2017-09-09 08:14:58 +08:00
|
|
|
|
|
|
|
_itemList.Clear();
|
|
|
|
|
|
|
|
// Add the last bar space to the barSpace accumulator
|
|
|
|
offset += currentBarSpace;
|
|
|
|
offset += counterOffset;
|
|
|
|
|
2023-03-12 19:44:50 +08:00
|
|
|
if (layoutInput)
|
|
|
|
{
|
2017-09-09 08:14:58 +08:00
|
|
|
SetLayoutInputForAxis(offset, offset, -1, axis);
|
2015-02-10 08:03:38 +08:00
|
|
|
}
|
2017-09-09 08:14:58 +08:00
|
|
|
return offset;
|
2015-02-10 08:03:38 +08:00
|
|
|
}
|
2017-09-09 08:14:58 +08:00
|
|
|
|
2015-02-10 08:03:38 +08:00
|
|
|
private float CalculateRowVerticalOffset(float groupHeight, float yOffset, float currentRowHeight)
|
|
|
|
{
|
2023-03-12 19:44:50 +08:00
|
|
|
if (IsLowerAlign)
|
|
|
|
{
|
2017-09-09 08:14:58 +08:00
|
|
|
return groupHeight - yOffset - currentRowHeight;
|
2023-03-12 19:44:50 +08:00
|
|
|
}
|
|
|
|
else if (IsMiddleAlign)
|
|
|
|
{
|
2017-09-09 08:14:58 +08:00
|
|
|
return groupHeight * 0.5f - _layoutHeight * 0.5f + yOffset;
|
2015-02-10 08:03:38 +08:00
|
|
|
}
|
2023-03-12 19:44:50 +08:00
|
|
|
return yOffset;
|
2015-02-10 08:03:38 +08:00
|
|
|
}
|
2017-09-09 08:14:58 +08:00
|
|
|
|
|
|
|
private float CalculateColHorizontalOffset(float groupWidth, float xOffset, float currentColWidth)
|
|
|
|
{
|
2023-03-12 19:44:50 +08:00
|
|
|
if (IsRightAlign)
|
|
|
|
{
|
2017-09-09 08:14:58 +08:00
|
|
|
return groupWidth - xOffset - currentColWidth;
|
2023-03-12 19:44:50 +08:00
|
|
|
}
|
|
|
|
else if (IsCenterAlign)
|
|
|
|
{
|
2017-09-09 08:14:58 +08:00
|
|
|
return groupWidth * 0.5f - _layoutWidth * 0.5f + xOffset;
|
|
|
|
}
|
2023-03-12 19:44:50 +08:00
|
|
|
return xOffset;
|
2017-09-09 08:14:58 +08:00
|
|
|
}
|
|
|
|
|
2015-02-10 08:03:38 +08:00
|
|
|
protected void LayoutRow(IList<RectTransform> contents, float rowWidth, float rowHeight, float maxWidth, float xOffset, float yOffset, int axis)
|
|
|
|
{
|
|
|
|
var xPos = xOffset;
|
2017-09-09 08:14:58 +08:00
|
|
|
|
2023-03-12 19:44:50 +08:00
|
|
|
if (!ChildForceExpandWidth && IsCenterAlign)
|
|
|
|
{
|
2015-02-10 08:03:38 +08:00
|
|
|
xPos += (maxWidth - rowWidth) * 0.5f;
|
2023-03-12 19:44:50 +08:00
|
|
|
}
|
|
|
|
else if (!ChildForceExpandWidth && IsRightAlign)
|
|
|
|
{
|
2015-02-10 08:03:38 +08:00
|
|
|
xPos += (maxWidth - rowWidth);
|
2017-09-09 08:14:58 +08:00
|
|
|
}
|
|
|
|
|
2015-02-10 08:03:38 +08:00
|
|
|
var extraWidth = 0f;
|
2015-09-20 07:24:17 +08:00
|
|
|
var extraSpacing = 0f;
|
2015-02-10 08:03:38 +08:00
|
|
|
|
2023-03-12 19:44:50 +08:00
|
|
|
if (ChildForceExpandWidth)
|
|
|
|
{
|
|
|
|
extraWidth = (maxWidth - rowWidth) / contents.Count;
|
2015-02-10 08:03:38 +08:00
|
|
|
}
|
2023-03-12 19:44:50 +08:00
|
|
|
else if (ExpandHorizontalSpacing)
|
|
|
|
{
|
|
|
|
extraSpacing = (maxWidth - rowWidth) / (contents.Count - 1);
|
|
|
|
if (contents.Count > 1)
|
|
|
|
{
|
|
|
|
if (IsCenterAlign)
|
|
|
|
{
|
|
|
|
xPos -= extraSpacing * 0.5f * (contents.Count - 1);
|
|
|
|
}
|
|
|
|
else if (IsRightAlign)
|
|
|
|
{
|
|
|
|
xPos -= extraSpacing * (contents.Count - 1);
|
2017-09-09 08:14:58 +08:00
|
|
|
}
|
2015-09-20 07:24:17 +08:00
|
|
|
}
|
|
|
|
}
|
2017-09-09 08:14:58 +08:00
|
|
|
|
2023-03-12 19:44:50 +08:00
|
|
|
for (var j = 0; j < contents.Count; j++)
|
|
|
|
{
|
|
|
|
var index = IsLowerAlign ? contents.Count - 1 - j : j;
|
2017-09-09 08:14:58 +08:00
|
|
|
|
2023-03-12 19:44:50 +08:00
|
|
|
var rowChild = contents[index];
|
2017-09-09 08:14:58 +08:00
|
|
|
|
2015-02-10 08:03:38 +08:00
|
|
|
var rowChildWidth = LayoutUtility.GetPreferredSize(rowChild, 0) + extraWidth;
|
|
|
|
var rowChildHeight = LayoutUtility.GetPreferredSize(rowChild, 1);
|
2017-09-09 08:14:58 +08:00
|
|
|
|
2015-02-10 08:03:38 +08:00
|
|
|
if (ChildForceExpandHeight)
|
2023-03-12 19:44:50 +08:00
|
|
|
{
|
2015-02-10 08:03:38 +08:00
|
|
|
rowChildHeight = rowHeight;
|
2023-03-12 19:44:50 +08:00
|
|
|
}
|
2017-09-09 08:14:58 +08:00
|
|
|
|
2015-02-10 08:03:38 +08:00
|
|
|
rowChildWidth = Mathf.Min(rowChildWidth, maxWidth);
|
|
|
|
var yPos = yOffset;
|
|
|
|
|
2023-03-12 19:44:50 +08:00
|
|
|
if (IsMiddleAlign)
|
|
|
|
{
|
2015-02-10 08:03:38 +08:00
|
|
|
yPos += (rowHeight - rowChildHeight) * 0.5f;
|
2023-03-12 19:44:50 +08:00
|
|
|
}
|
|
|
|
else if (IsLowerAlign)
|
|
|
|
{
|
2015-02-10 08:03:38 +08:00
|
|
|
yPos += (rowHeight - rowChildHeight);
|
2017-09-09 08:14:58 +08:00
|
|
|
}
|
2015-02-10 08:03:38 +08:00
|
|
|
|
2023-03-12 19:44:50 +08:00
|
|
|
if (ExpandHorizontalSpacing && j > 0)
|
|
|
|
{
|
2015-09-20 07:24:17 +08:00
|
|
|
xPos += extraSpacing;
|
2017-09-09 08:14:58 +08:00
|
|
|
}
|
2015-09-20 07:24:17 +08:00
|
|
|
|
2023-03-12 19:44:50 +08:00
|
|
|
if (axis == 0)
|
|
|
|
{
|
|
|
|
SetChildAlongAxis(rowChild, 0, xPos, rowChildWidth);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetChildAlongAxis(rowChild, 1, yPos, rowChildHeight);
|
2017-09-09 08:14:58 +08:00
|
|
|
}
|
2015-02-10 08:03:38 +08:00
|
|
|
|
2015-09-20 07:24:17 +08:00
|
|
|
// Don't do horizontal spacing for the last one
|
2023-03-12 19:44:50 +08:00
|
|
|
if (j < contents.Count - 1)
|
|
|
|
{
|
2015-09-20 07:24:17 +08:00
|
|
|
xPos += rowChildWidth + SpacingX;
|
2017-09-09 08:14:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void LayoutCol(IList<RectTransform> contents, float colWidth, float colHeight, float maxHeight, float xOffset, float yOffset, int axis)
|
|
|
|
{
|
|
|
|
var yPos = yOffset;
|
|
|
|
|
2023-03-12 19:44:50 +08:00
|
|
|
if (!ChildForceExpandHeight && IsMiddleAlign)
|
|
|
|
{
|
2017-09-09 08:14:58 +08:00
|
|
|
yPos += (maxHeight - colHeight) * 0.5f;
|
2023-03-12 19:44:50 +08:00
|
|
|
}
|
|
|
|
else if (!ChildForceExpandHeight && IsLowerAlign)
|
|
|
|
{
|
2017-09-09 08:14:58 +08:00
|
|
|
yPos += (maxHeight - colHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
var extraHeight = 0f;
|
|
|
|
var extraSpacing = 0f;
|
|
|
|
|
2023-03-12 19:44:50 +08:00
|
|
|
if (ChildForceExpandHeight)
|
|
|
|
{
|
|
|
|
extraHeight = (maxHeight - colHeight) / contents.Count;
|
2017-09-09 08:14:58 +08:00
|
|
|
}
|
2023-03-12 19:44:50 +08:00
|
|
|
else if (ExpandHorizontalSpacing)
|
|
|
|
{
|
|
|
|
extraSpacing = (maxHeight - colHeight) / (contents.Count - 1);
|
|
|
|
if (contents.Count > 1)
|
|
|
|
{
|
|
|
|
if (IsMiddleAlign)
|
|
|
|
{
|
|
|
|
yPos -= extraSpacing * 0.5f * (contents.Count - 1);
|
|
|
|
}
|
|
|
|
else if (IsLowerAlign)
|
|
|
|
{
|
|
|
|
yPos -= extraSpacing * (contents.Count - 1);
|
2017-09-09 08:14:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-12 19:44:50 +08:00
|
|
|
for (var j = 0; j < contents.Count; j++)
|
|
|
|
{
|
|
|
|
var index = IsRightAlign ? contents.Count - 1 - j : j;
|
2017-09-09 08:14:58 +08:00
|
|
|
|
2023-03-12 19:44:50 +08:00
|
|
|
var rowChild = contents[index];
|
2017-09-09 08:14:58 +08:00
|
|
|
|
2023-03-12 19:44:50 +08:00
|
|
|
var rowChildWidth = LayoutUtility.GetPreferredSize(rowChild, 0);
|
2017-09-09 08:14:58 +08:00
|
|
|
var rowChildHeight = LayoutUtility.GetPreferredSize(rowChild, 1) + extraHeight;
|
|
|
|
|
2023-03-12 19:44:50 +08:00
|
|
|
if (ChildForceExpandWidth)
|
|
|
|
{
|
2017-09-09 08:14:58 +08:00
|
|
|
rowChildWidth = colWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
rowChildHeight = Mathf.Min(rowChildHeight, maxHeight);
|
|
|
|
|
|
|
|
var xPos = xOffset;
|
|
|
|
|
2023-03-12 19:44:50 +08:00
|
|
|
if (IsCenterAlign)
|
|
|
|
{
|
2017-09-09 08:14:58 +08:00
|
|
|
xPos += (colWidth - rowChildWidth) * 0.5f;
|
2023-03-12 19:44:50 +08:00
|
|
|
}
|
|
|
|
else if (IsRightAlign)
|
|
|
|
{
|
2017-09-09 08:14:58 +08:00
|
|
|
xPos += (colWidth - rowChildWidth);
|
|
|
|
}
|
|
|
|
|
2023-03-12 19:44:50 +08:00
|
|
|
if (ExpandHorizontalSpacing && j > 0)
|
|
|
|
{
|
2017-09-09 08:14:58 +08:00
|
|
|
yPos += extraSpacing;
|
|
|
|
}
|
|
|
|
|
2023-03-12 19:44:50 +08:00
|
|
|
if (axis == 0)
|
|
|
|
{
|
|
|
|
SetChildAlongAxis(rowChild, 0, xPos, rowChildWidth);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetChildAlongAxis(rowChild, 1, yPos, rowChildHeight);
|
2017-09-09 08:14:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Don't do vertical spacing for the last one
|
2023-03-12 19:44:50 +08:00
|
|
|
if (j < contents.Count - 1)
|
|
|
|
{
|
2017-09-09 08:14:58 +08:00
|
|
|
yPos += rowChildHeight + SpacingY;
|
|
|
|
}
|
2015-02-10 08:03:38 +08:00
|
|
|
}
|
|
|
|
}
|
2017-09-09 08:14:58 +08:00
|
|
|
|
2015-02-10 08:03:38 +08:00
|
|
|
public float GetGreatestMinimumChildWidth()
|
|
|
|
{
|
|
|
|
var max = 0f;
|
2023-03-12 19:44:50 +08:00
|
|
|
for (var i = 0; i < rectChildren.Count; i++)
|
|
|
|
{
|
2015-02-10 08:03:38 +08:00
|
|
|
var w = LayoutUtility.GetMinWidth(rectChildren[i]);
|
2017-09-09 08:14:58 +08:00
|
|
|
max = Mathf.Max(w, max);
|
|
|
|
}
|
|
|
|
return max;
|
|
|
|
}
|
|
|
|
|
|
|
|
public float GetGreatestMinimumChildHeigth()
|
|
|
|
{
|
|
|
|
var max = 0f;
|
2023-03-12 19:44:50 +08:00
|
|
|
for (var i = 0; i < rectChildren.Count; i++)
|
|
|
|
{
|
2017-09-09 08:14:58 +08:00
|
|
|
var w = LayoutUtility.GetMinHeight(rectChildren[i]);
|
2015-02-10 08:03:38 +08:00
|
|
|
max = Mathf.Max(w, max);
|
|
|
|
}
|
|
|
|
return max;
|
|
|
|
}
|
2022-12-24 21:28:19 +08:00
|
|
|
|
2023-03-12 19:44:50 +08:00
|
|
|
protected override void OnDisable()
|
|
|
|
{
|
|
|
|
m_Tracker.Clear();
|
|
|
|
LayoutRebuilder.MarkLayoutForRebuild(rectTransform);
|
|
|
|
}
|
|
|
|
}
|
2017-09-09 08:14:58 +08:00
|
|
|
}
|