using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using System.Linq;
using System;
namespace EasyLayout {
///
/// Grid constraints.
/// Flexible - Don't constrain the number of rows or columns.
/// FixedColumnCount - Constraint the number of columns to a specified number.
/// FixedRowCount - Constraint the number of rows to a specified number.
///
public enum GridConstraints
{
Flexible = 0,
FixedColumnCount = 1,
FixedRowCount = 2,
}
///
/// Padding.
///
[Serializable]
public struct Padding
{
///
/// The left padding.
///
[SerializeField]
public float Left;
///
/// The right padding.
///
[SerializeField]
public float Right;
///
/// The top padding.
///
[SerializeField]
public float Top;
///
/// The bottom padding.
///
[SerializeField]
public float Bottom;
///
/// Initializes a new instance of the struct.
///
/// Left.
/// Right.
/// Top.
/// Bottom.
public Padding(float left, float right, float top, float bottom)
{
Left = left;
Right = right;
Top = top;
Bottom = bottom;
}
///
/// Returns a string that represents the current object.
///
/// A string that represents the current object.
public override string ToString()
{
return String.Format("Padding(left: {0}, right: {1}, top: {2}, bottom: {3})",
Left,
Right,
Top,
Bottom
);
}
}
///
/// Children size.
/// DoNothing - Don't change size of children.
/// SetPreferred - Set size of children to preferred.
/// SetMaxFromPreferred - Set size of children to maximum size of children preferred.
/// FitContainer - Stretch size of children to fit container.
///
[Flags]
public enum ChildrenSize {
DoNothing = 0,
SetPreferred = 1,
SetMaxFromPreferred = 2,
FitContainer = 3,
}
///
/// Anchors.
/// UpperLeft - UpperLeft.
/// UpperCenter - UpperCenter.
/// UpperRight - UpperRight.
/// MiddleLeft - MiddleLeft.
/// MiddleCenter - MiddleCenter.
/// MiddleRight - MiddleRight.
/// LowerLeft - LowerLeft.
/// LowerCenter - LowerCenter.
/// LowerRight - LowerRight.
///
[Flags]
public enum Anchors {
UpperLeft = 0,
UpperCenter = 1,
UpperRight = 2,
MiddleLeft = 3,
MiddleCenter = 4,
MiddleRight = 5,
LowerLeft = 6,
LowerCenter = 7,
LowerRight = 8,
}
///
/// Stackings.
/// Horizontal - Horizontal.
/// Vertical - Vertical.
///
[Flags]
public enum Stackings {
Horizontal = 0,
Vertical = 1,
}
///
/// Horizontal aligns.
/// Left - Left.
/// Center - Center.
/// Right - Right.
///
[Flags]
public enum HorizontalAligns {
Left = 0,
Center = 1,
Right = 2,
}
///
/// Inner aligns.
/// Top - Top.
/// Middle - Middle.
/// Bottom - Bottom.
///
[Flags]
public enum InnerAligns {
Top = 0,
Middle = 1,
Bottom = 2,
}
///
/// Layout type to use.
/// Compact - Compact.
/// Grid - Grid.
///
[Flags]
public enum LayoutTypes {
Compact = 0,
Grid = 1,
}
///
/// EasyLayout.
/// Warning: using RectTransform relative size with positive size delta (like 100% + 10) with ContentSizeFitter can lead to infinite increased size.
///
[ExecuteInEditMode]
[RequireComponent(typeof(RectTransform))]
[AddComponentMenu("UI/UIWidgets/EasyLayout")]
public class EasyLayout : UnityEngine.UI.LayoutGroup {
///
/// The group position.
///
[SerializeField]
public Anchors GroupPosition = Anchors.UpperLeft;
///
/// The stacking type.
///
[SerializeField]
public Stackings Stacking = Stackings.Horizontal;
///
/// The type of the layout.
///
[SerializeField]
public LayoutTypes LayoutType = LayoutTypes.Compact;
///
/// Which constraint to use for the Grid layout.
///
[SerializeField]
public GridConstraints GridConstraint = GridConstraints.Flexible;
///
/// How many cells there should be along the constrained axis.
///
[SerializeField]
public int GridConstraintCount = 1;
///
/// The row align.
///
[SerializeField]
public HorizontalAligns RowAlign = HorizontalAligns.Left;
///
/// The inner align.
///
[SerializeField]
public InnerAligns InnerAlign = InnerAligns.Top;
///
/// The cell align.
///
[SerializeField]
public Anchors CellAlign = Anchors.UpperLeft;
///
/// The spacing.
///
[SerializeField]
public Vector2 Spacing = new Vector2(5, 5);
///
/// Symmetric margin.
///
[SerializeField]
public bool Symmetric = true;
///
/// The margin.
///
[SerializeField]
public Vector2 Margin = new Vector2(5, 5);
///
/// The padding.
///
[SerializeField]
public Padding PaddingInner = new Padding();
///
/// The margin top.
///
[SerializeField]
public float MarginTop = 5f;
///
/// The margin bottom.
///
[SerializeField]
public float MarginBottom = 5f;
///
/// The margin left.
///
[SerializeField]
public float MarginLeft = 5f;
///
/// The margin right.
///
[SerializeField]
public float MarginRight = 5f;
///
/// The right to left stacking.
///
[SerializeField]
public bool RightToLeft = false;
///
/// The top to bottom stacking.
///
[SerializeField]
public bool TopToBottom = true;
///
/// The skip inactive.
///
[SerializeField]
public bool SkipInactive = true;
///
/// The filter.
///
public Func,IEnumerable> Filter = null;
///
/// How to control width of the children.
///
public ChildrenSize ChildrenWidth;
///
/// How to control height of the children.
///
public ChildrenSize ChildrenHeight;
///
/// Control width of children.
///
[SerializeField]
[Obsolete("Use ChildrenWidth with ChildrenSize.SetPreferred instead.")]
public bool ControlWidth;
///
/// Control height of children.
///
[SerializeField]
[Obsolete("Use ChildrenHeight with ChildrenSize.SetPreferred instead.")]
public bool ControlHeight;
///
/// Sets width of the chidren to maximum width from them.
///
[SerializeField]
[Obsolete("Use ChildrenWidth with ChildrenSize.SetMaxFromPreferred instead.")]
public bool MaxWidth;
///
/// Sets height of the chidren to maximum height from them.
///
[SerializeField]
[Obsolete("Use ChildrenHeight with ChildrenSize.SetMaxFromPreferred instead.")]
public bool MaxHeight;
Vector2 _blockSize;
///
/// Gets or sets the size of the inner block.
///
/// The size of the inner block.
public Vector2 BlockSize {
get {
return _blockSize;
}
protected set {
_blockSize = value;
}
}
Vector2 _uiSize;
///
/// Gets or sets the UI size.
///
/// The UI size.
public Vector2 UISize {
get {
return _uiSize;
}
protected set {
_uiSize = value;
}
}
///
/// Gets the minimum height.
///
/// The minimum height.
public override float minHeight
{
get
{
//CalculateLayoutSize();
return BlockSize[1];
}
}
///
/// Gets the minimum width.
///
/// The minimum width.
public override float minWidth
{
get
{
//CalculateLayoutSize();
return BlockSize[0];
}
}
///
/// Gets the preferred height.
///
/// The preferred height.
public override float preferredHeight
{
get
{
//CalculateLayoutSize();
return BlockSize[1];
}
}
///
/// Gets the preferred width.
///
/// The preferred width.
public override float preferredWidth
{
get
{
//CalculateLayoutSize();
return BlockSize[0];
}
}
static readonly List groupPositions = new List{
new Vector2(0.0f, 1.0f),//Anchors.UpperLeft
new Vector2(0.5f, 1.0f),//Anchors.UpperCenter
new Vector2(1.0f, 1.0f),//Anchors.UpperRight
new Vector2(0.0f, 0.5f),//Anchors.MiddleLeft
new Vector2(0.5f, 0.5f),//Anchors.MiddleCenter
new Vector2(1.0f, 0.5f),//Anchors.MiddleRight
new Vector2(0.0f, 0.0f),//Anchors.LowerLeft
new Vector2(0.5f, 0.0f),//Anchors.LowerCenter
new Vector2(1.0f, 0.0f),//Anchors.LowerRight
};
static readonly List rowAligns = new List{
0.0f,//HorizontalAligns.Left
0.5f,//HorizontalAligns.Center
1.0f,//HorizontalAligns.Right
};
static readonly List innerAligns = new List{
0.0f,//InnerAligns.Top
0.5f,//InnerAligns.Middle
1.0f,//InnerAligns.Bottom
};
///
/// Raises the disable event.
///
protected override void OnDisable()
{
propertiesTracker.Clear();
base.OnDisable();
}
///
/// Raises the rect transform removed event.
///
void OnRectTransformRemoved()
{
SetDirty();
}
///
/// Sets the layout horizontal.
///
public override void SetLayoutHorizontal()
{
RepositionUIElements();
}
///
/// Sets the layout vertical.
///
public override void SetLayoutVertical()
{
RepositionUIElements();
}
///
/// Calculates the layout input horizontal.
///
public override void CalculateLayoutInputHorizontal()
{
base.CalculateLayoutInputHorizontal();
CalculateLayoutSize();
}
///
/// Calculates the layout input vertical.
///
public override void CalculateLayoutInputVertical()
{
CalculateLayoutSize();
}
///
/// Gets the length.
///
/// The length.
/// User interface.
/// If set to true scaled.
public float GetLength(RectTransform ui, bool scaled=true)
{
if (scaled)
{
return Stacking==Stackings.Horizontal ? EasyLayoutUtilites.ScaledWidth(ui) : EasyLayoutUtilites.ScaledHeight(ui);
}
return Stacking==Stackings.Horizontal ? ui.rect.width : ui.rect.height;
}
///
/// Calculates the size of the group.
///
/// The group size.
/// Group.
Vector2 CalculateGroupSize(List> group)
{
float width = 0f;
if (LayoutType==LayoutTypes.Compact)
{
for (int i = 0; i < group.Count; i++)
{
float row_width = Spacing.x * (group[i].Count - 1);
for (int j = 0; j < group[i].Count; j++)
{
row_width += EasyLayoutUtilites.ScaledWidth(group[i][j]);
}
width = Mathf.Max(width, row_width);
}
}
else
{
GetMaxColumnsWidths(group, MaxColumnsWidths);
for (int i = 0; i < MaxColumnsWidths.Count; i++)
{
width += MaxColumnsWidths[i];
}
width += MaxColumnsWidths.Count * Spacing.x - Spacing.x;
}
float height = Spacing.y * (group.Count - 1);
for (int i = 0; i < group.Count; i++)
{
float row_height = 0f;
for (int j = 0; j < group[i].Count; j++)
{
row_height = Mathf.Max(row_height, EasyLayoutUtilites.ScaledHeight(group[i][j]));
}
height += row_height;
}
width += PaddingInner.Left + PaddingInner.Right;
height += PaddingInner.Top + PaddingInner.Bottom;
return new Vector2(width, height);
}
///
/// Marks layout to update.
///
public void NeedUpdateLayout()
{
UpdateLayout();
}
///
/// Updates the size of the block.
///
void UpdateBlockSize()
{
if (Symmetric)
{
BlockSize = new Vector2(UISize.x + Margin.x * 2, UISize.y + Margin.y * 2);
}
else
{
BlockSize = new Vector2(UISize.x + MarginLeft + MarginRight, UISize.y + MarginTop + MarginBottom);
}
}
///
/// Calculates the size of the layout.
///
public void CalculateLayoutSize()
{
var group = GroupUIElements();
if (group.Count==0)
{
UISize = new Vector2(0, 0);
UpdateBlockSize();
return ;
}
UISize = CalculateGroupSize(group);
UpdateBlockSize();
}
///
/// Repositions the user interface elements.
///
void RepositionUIElements()
{
var group = GroupUIElements();
if (group.Count==0)
{
UISize = new Vector2(0, 0);
UpdateBlockSize();
//LayoutRebuilder.MarkLayoutForRebuild(rectTransform);
return ;
}
UISize = CalculateGroupSize(group);
UpdateBlockSize();
var anchor_position = groupPositions[(int)GroupPosition];
var start_position = new Vector2(
rectTransform.rect.width * (anchor_position.x - rectTransform.pivot.x),
rectTransform.rect.height * (anchor_position.y - rectTransform.pivot.y)
);
start_position.x -= anchor_position.x * UISize.x;
start_position.y += (1 - anchor_position.y) * UISize.y;
start_position.x += GetMarginLeft() * ((1 - anchor_position.x) * 2 - 1);
start_position.y += GetMarginTop() * ((1 - anchor_position.y) * 2 - 1);
start_position.x += PaddingInner.Left;
start_position.y -= PaddingInner.Top;
SetPositions(group, start_position, UISize);
//LayoutRebuilder.MarkLayoutForRebuild(rectTransform);
}
///
/// Updates the layout.
///
public void UpdateLayout()
{
CalculateLayoutInputHorizontal();
RepositionUIElements();
}
///
/// Gets the user interface element position.
///
/// The user interface position.
/// User interface.
/// Position.
/// Align.
Vector2 GetUIPosition(RectTransform ui, Vector2 position, Vector2 align)
{
var pivot_fix_x = EasyLayoutUtilites.ScaledWidth(ui) * ui.pivot.x;
var pivox_fix_y = EasyLayoutUtilites.ScaledHeight(ui) * ui.pivot.y;
var new_x = position.x + pivot_fix_x + align.x;
var new_y = position.y - EasyLayoutUtilites.ScaledHeight(ui) + pivox_fix_y - align.y;
return new Vector2(new_x, new_y);
}
void GetRowsWidths(List> group, List widths)
{
widths.Clear();
for (int i = 0; i < group.Count; i++)
{
float width = 0f;
for (int j = 0; j < group[i].Count; j++)
{
width += EasyLayoutUtilites.ScaledWidth(group[i][j]);
}
widths.Add(width + (group[i].Count - 1) * Spacing.x);
}
}
void GetMaxColumnsWidths(List> group, List maxColumnsWidths)
{
maxColumnsWidths.Clear();
for (var i = 0; i < group.Count; i++)
{
for (var j = 0; j < group[i].Count; j++)
{
if (maxColumnsWidths.Count <= j)
{
maxColumnsWidths.Add(0);
}
maxColumnsWidths[j] = Mathf.Max(maxColumnsWidths[j], EasyLayoutUtilites.ScaledWidth(group[i][j]));
}
}
}
void GetColumnsHeights(List> group, List heights)
{
heights.Clear();
for (int i = 0; i < group.Count; i++)
{
float height = 0;
for (int j = 0; j < group[i].Count; j++)
{
height += EasyLayoutUtilites.ScaledHeight(group[i][j]);
}
heights.Add(height + (group[i].Count - 1) * Spacing.y);
}
}
float GetMaxRowHeight(List row)
{
float height = 0;
for (int i = 0; i < row.Count; i++)
{
height = Mathf.Max(height, EasyLayoutUtilites.ScaledHeight(row[i]));
}
return height;
}
void GetMaxRowsHeights(List> group, List heights)
{
heights.Clear();
var transposed_group = EasyLayoutUtilites.Transpose(group);
for (int i = 0; i < transposed_group.Count; i++)
{
heights.Add(GetMaxRowHeight(transposed_group[i]));
}
}
Vector2 GetMaxCellSize(List> group)
{
float x = 0f;
float y = 0f;
for (int i = 0; i < group.Count; i++)
{
for (int j = 0; j < group[i].Count; j++)
{
x = Mathf.Max(x, EasyLayoutUtilites.ScaledWidth(group[i][j]));
y = Mathf.Max(y, EasyLayoutUtilites.ScaledHeight(group[i][j]));
}
}
return new Vector2(x, y);
}
Vector2 GetMaxCellSize(List row)
{
float x = 0f;
float y = 0f;
for (int i = 0; i < row.Count; i++)
{
x = Mathf.Max(x, EasyLayoutUtilites.ScaledWidth(row[i]));
y = Mathf.Max(y, EasyLayoutUtilites.ScaledHeight(row[i]));
}
return new Vector2(x, y);
}
Vector2 GetAlignByWidth(RectTransform ui, float maxWidth, Vector2 cellMaxSize, float emptyWidth)
{
if (LayoutType==LayoutTypes.Compact)
{
return new Vector2(
emptyWidth * rowAligns[(int)RowAlign],
(cellMaxSize.y - EasyLayoutUtilites.ScaledHeight(ui)) * innerAligns[(int)InnerAlign]
);
}
else
{
var cell_align = groupPositions[(int)CellAlign];
return new Vector2(
(maxWidth - EasyLayoutUtilites.ScaledWidth(ui)) * cell_align.x,
(cellMaxSize.y - EasyLayoutUtilites.ScaledHeight(ui)) * (1 - cell_align.y)
);
}
}
Vector2 GetAlignByHeight(RectTransform ui, float maxHeight, Vector2 cellMaxSize, float emptyHeight)
{
if (LayoutType==LayoutTypes.Compact)
{
return new Vector2(
(cellMaxSize.x - EasyLayoutUtilites.ScaledWidth(ui)) * innerAligns[(int)InnerAlign],
emptyHeight * rowAligns[(int)RowAlign]
);
}
else
{
var cell_align = groupPositions[(int)CellAlign];
return new Vector2(
(cellMaxSize.x - EasyLayoutUtilites.ScaledWidth(ui)) * (1 - cell_align.x),
(maxHeight - EasyLayoutUtilites.ScaledHeight(ui)) * cell_align.y
);
}
}
List RowsWidths = new List();
List MaxColumnsWidths = new List();
List ColumnsHeights = new List();
List MaxRowsHeights = new List();
RectTransform currentUIElement;
void SetPositions(List> group, Vector2 startPosition, Vector2 groupSize)
{
if (Stacking==Stackings.Horizontal)
{
SetPositionsHorizontal(group, startPosition, groupSize);
}
else
{
SetPositionsVertical(group, startPosition, groupSize);
}
}
void SetPositionsHorizontal(List> group, Vector2 startPosition, Vector2 groupSize)
{
var position = startPosition;
GetRowsWidths(group, RowsWidths);
GetMaxColumnsWidths(group, MaxColumnsWidths);
var align = new Vector2(0, 0);
for (int coord_x = 0; coord_x < group.Count; coord_x++)
{
var row_cell_max_size = GetMaxCellSize(group[coord_x]);
for (int coord_y = 0; coord_y < group[coord_x].Count; coord_y++)
{
currentUIElement = group[coord_x][coord_y];
align = GetAlignByWidth(currentUIElement, MaxColumnsWidths[coord_y], row_cell_max_size, groupSize.x - RowsWidths[coord_x]);
var new_position = GetUIPosition(currentUIElement, position, align);
if (currentUIElement.localPosition.x != new_position.x || currentUIElement.localPosition.y != new_position.y)
{
currentUIElement.localPosition = new_position;
}
position.x += ((LayoutType==LayoutTypes.Compact)
? EasyLayoutUtilites.ScaledWidth(currentUIElement)
: MaxColumnsWidths[coord_y]) + Spacing.x;
}
position.x = startPosition.x;
position.y -= row_cell_max_size.y + Spacing.y;
}
}
void SetPositionsVertical(List> group, Vector2 startPosition, Vector2 groupSize)
{
var position = startPosition;
group = EasyLayoutUtilites.Transpose(group);
GetColumnsHeights(group, ColumnsHeights);
GetMaxRowsHeights(group, MaxRowsHeights);
var align = new Vector2(0, 0);
for (int coord_y = 0; coord_y < group.Count; coord_y++)
{
var column_cell_max_size = GetMaxCellSize(group[coord_y]);
for (int coord_x = 0; coord_x < group[coord_y].Count; coord_x++)
{
currentUIElement = group[coord_y][coord_x];
align = GetAlignByHeight(currentUIElement, MaxRowsHeights[coord_x], column_cell_max_size, groupSize.y - ColumnsHeights[coord_y]);
var new_position = GetUIPosition(currentUIElement, position, align);
if (currentUIElement.localPosition.x != new_position.x || currentUIElement.localPosition.y != new_position.y)
{
currentUIElement.localPosition = new_position;
}
position.y -= ((LayoutType==LayoutTypes.Compact)
? EasyLayoutUtilites.ScaledHeight(currentUIElement)
: MaxRowsHeights[coord_x]) + Spacing.y;
}
position.y = startPosition.y;
position.x += column_cell_max_size.x + Spacing.x;
}
}
float max_width = -1;
float max_height = -1;
void ResizeElements(List elements)
{
propertiesTracker.Clear();
if (ChildrenWidth==ChildrenSize.DoNothing && ChildrenHeight==ChildrenSize.DoNothing)
{
return ;
}
if (elements==null)
{
return ;
}
if (elements.Count==0)
{
return ;
}
if (LayoutType==LayoutTypes.Grid && GridConstraint!=GridConstraints.Flexible)
{
//elements.ForEach(Add2Tracker);
//return ;
}
max_width = (ChildrenWidth==ChildrenSize.SetMaxFromPreferred) ? elements.Select(EasyLayoutUtilites.GetPreferredWidth).Max() : -1f;
max_height = (ChildrenHeight==ChildrenSize.SetMaxFromPreferred) ? elements.Select(EasyLayoutUtilites.GetPreferredHeight).Max() : -1f;
elements.ForEach(ResizeChild);
}
DrivenRectTransformTracker propertiesTracker;
void Add2Tracker(RectTransform child)
{
DrivenTransformProperties driven_properties = DrivenTransformProperties.None;
if (ChildrenWidth!=ChildrenSize.DoNothing)
{
driven_properties |= DrivenTransformProperties.SizeDeltaX;
}
if (ChildrenHeight!=ChildrenSize.DoNothing)
{
driven_properties |= DrivenTransformProperties.SizeDeltaY;
}
propertiesTracker.Add(this, child, driven_properties);
}
void ResizeChild(RectTransform child)
{
DrivenTransformProperties driven_properties = DrivenTransformProperties.None;
if (ChildrenWidth!=ChildrenSize.DoNothing)
{
driven_properties |= DrivenTransformProperties.SizeDeltaX;
var width = (max_width!=-1) ? max_width : EasyLayoutUtilites.GetPreferredWidth(child);
child.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width);
}
if (ChildrenHeight!=ChildrenSize.DoNothing)
{
driven_properties |= DrivenTransformProperties.SizeDeltaY;
var height = (max_height!=-1) ? max_height : EasyLayoutUtilites.GetPreferredHeight(child);
child.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height);
}
propertiesTracker.Add(this, child, driven_properties);
}
bool IsIgnoreLayout(Transform rect)
{
#if UNITY_4_6 || UNITY_4_7
var ignorer = rect.GetComponent(typeof(ILayoutIgnorer)) as ILayoutIgnorer;
#else
var ignorer = rect.GetComponent();
#endif
return (ignorer!=null) && ignorer.ignoreLayout;
}
List GetUIElements()
{
var elements = rectChildren;
if (!SkipInactive)
{
elements = new List();
foreach (Transform child in transform)
{
if (!IsIgnoreLayout(child))
{
elements.Add(child as RectTransform);
}
}
}
if (Filter!=null)
{
var temp = Filter(elements.Convert(GetGameObject));
var result = temp.Select(GetRectTransform).ToList();
ResizeElements(result);
return result;
}
ResizeElements(elements);
return elements;
}
GameObject GetGameObject(RectTransform element)
{
return element.gameObject;
}
RectTransform GetRectTransform(GameObject go)
{
return go.transform as RectTransform;
}
///
/// Gets the margin top.
///
public float GetMarginTop()
{
return Symmetric ? Margin.y : MarginTop;
}
///
/// Gets the margin bottom.
///
public float GetMarginBottom()
{
return Symmetric ? Margin.y : MarginBottom;
}
///
/// Gets the margin left.
///
public float GetMarginLeft()
{
return Symmetric ? Margin.x : MarginLeft;
}
///
/// Gets the margin right.
///
public float GetMarginRight()
{
return Symmetric ? Margin.y : MarginRight;
}
void ReverseList(List list)
{
list.Reverse();
}
void ClearUIElementsGroup()
{
for (int i = 0; i < uiElementsGroup.Count; i++)
{
uiElementsGroup[i].Clear();
ListCache.Push(uiElementsGroup[i]);
}
uiElementsGroup.Clear();
}
static Stack> ListCache = new Stack>();
///
/// Gets the List.
///
/// The rect transform list.
public List GetRectTransformList()
{
if (ListCache.Count > 0)
{
return ListCache.Pop();
}
else
{
return new List();
}
}
List> uiElementsGroup = new List>();
List> GroupUIElements()
{
var base_length = GetLength(rectTransform, false);
base_length -= (Stacking==Stackings.Horizontal) ? (GetMarginLeft() + GetMarginRight()) : (GetMarginTop() + GetMarginBottom());
var ui_elements = GetUIElements();
ClearUIElementsGroup();
if (LayoutType==LayoutTypes.Compact)
{
EasyLayoutCompact.Group(ui_elements, base_length, this, uiElementsGroup);
if (Stacking==Stackings.Vertical)
{
uiElementsGroup = EasyLayoutUtilites.Transpose(uiElementsGroup);
}
}
else
{
GridConstraintCount = Mathf.Max(1, GridConstraintCount);
if (GridConstraint==GridConstraints.Flexible)
{
EasyLayoutGrid.GroupFlexible(ui_elements, base_length, this, uiElementsGroup);
}
else if (GridConstraint==GridConstraints.FixedRowCount)
{
if (Stacking==Stackings.Vertical)
{
EasyLayoutGrid.GroupByRowsVertical(ui_elements, this, GridConstraintCount, uiElementsGroup);
}
else
{
EasyLayoutGrid.GroupByRowsHorizontal(ui_elements, this, GridConstraintCount, uiElementsGroup);
}
}
else if (GridConstraint==GridConstraints.FixedColumnCount)
{
if (Stacking==Stackings.Vertical)
{
EasyLayoutGrid.GroupByColumnsVertical(ui_elements, this, GridConstraintCount, uiElementsGroup);
}
else
{
EasyLayoutGrid.GroupByColumnsHorizontal(ui_elements, this, GridConstraintCount, uiElementsGroup);
}
}
}
if (!TopToBottom)
{
uiElementsGroup.Reverse();
}
if (RightToLeft)
{
uiElementsGroup.ForEach(ReverseList);
}
var width = rectTransform.rect.width - (GetMarginLeft() + GetMarginRight());
var height = rectTransform.rect.height - (GetMarginTop() + GetMarginBottom());
if (LayoutType==LayoutTypes.Grid)
{
if (ChildrenWidth==ChildrenSize.FitContainer)
{
EasyLayoutUtilites.ResizeColumnWidthToFit(width, uiElementsGroup, Spacing.x, PaddingInner.Left + PaddingInner.Right);
}
if (ChildrenHeight==ChildrenSize.FitContainer)
{
EasyLayoutUtilites.ResizeRowHeightToFit(height, uiElementsGroup, Spacing.y, PaddingInner.Top + PaddingInner.Bottom);
}
}
else
{
if (Stacking==Stackings.Horizontal)
{
if (ChildrenWidth==ChildrenSize.FitContainer)
{
EasyLayoutUtilites.ResizeWidthToFit(width, uiElementsGroup, Spacing.x);
}
if (ChildrenHeight==ChildrenSize.FitContainer)
{
EasyLayoutUtilites.ResizeRowHeightToFit(height, uiElementsGroup, Spacing.y, PaddingInner.Top + PaddingInner.Bottom);
}
}
else
{
if (ChildrenHeight==ChildrenSize.FitContainer)
{
EasyLayoutUtilites.ResizeHeightToFit(height, uiElementsGroup, Spacing.y);
}
if (ChildrenWidth==ChildrenSize.FitContainer)
{
EasyLayoutUtilites.ResizeColumnWidthToFit(width, uiElementsGroup, Spacing.x, PaddingInner.Left + PaddingInner.Right);
}
}
}
return uiElementsGroup;
}
///
/// Awake this instance.
///
protected override void Awake()
{
base.Awake();
Upgrade();
}
[SerializeField]
int version = 0;
#pragma warning disable 0618
///
/// Upgrade to keep compatibility between versions.
///
public virtual void Upgrade()
{
//upgrade to 1.6
if (version==0)
{
if (ControlWidth)
{
ChildrenWidth = (MaxWidth) ? ChildrenSize.SetMaxFromPreferred : ChildrenSize.SetPreferred;
}
if (ControlHeight)
{
ChildrenHeight = (MaxHeight) ? ChildrenSize.SetMaxFromPreferred : ChildrenSize.SetPreferred;
}
}
version = 1;
}
#pragma warning restore 0618
}
}