| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- using UnityEngine;
- using System.Collections.Generic;
- /// <summary>
- /// Layout class to arrange children elements in a grid format.
- /// </summary>
- /// <remarks>
- /// The SlideGridLayoutGroup component is used to layout child layout elements in a uniform grid where all cells have the same size. The size and the spacing between cells is controlled by the SlideGridLayoutGroup itself. The children have no influence on their sizes.
- /// </remarks>
- public class SlideGridLayoutGroup : UnityEngine.UI.LayoutGroup
- {
- /// <summary>
- /// Which corner is the starting corner for the grid.
- /// </summary>
- public enum Corner
- {
- /// <summary>
- /// Upper Left corner.
- /// </summary>
- UpperLeft = 0,
- /// <summary>
- /// Upper Right corner.
- /// </summary>
- UpperRight = 1,
- /// <summary>
- /// Lower Left corner.
- /// </summary>
- LowerLeft = 2,
- /// <summary>
- /// Lower Right corner.
- /// </summary>
- LowerRight = 3
- }
- /// <summary>
- /// The grid axis we are looking at.
- /// </summary>
- /// <remarks>
- /// As the storage is a [][] we make access easier by passing a axis.
- /// </remarks>
- public enum Axis
- {
- /// <summary>
- /// Horizontal axis
- /// </summary>
- Horizontal = 0,
- /// <summary>
- /// Vertical axis.
- /// </summary>
- Vertical = 1
- }
- /// <summary>
- /// Constraint type on either the number of columns or rows.
- /// </summary>
- public enum Constraint
- {
- /// <summary>
- /// Don't constrain the number of rows or columns.
- /// </summary>
- Flexible = 0,
- /// <summary>
- /// Constrain the number of columns to a specified number.
- /// </summary>
- FixedColumnCount = 1,
- /// <summary>
- /// Constraint the number of rows to a specified number.
- /// </summary>
- FixedRowCount = 2
- }
- [SerializeField] protected Corner m_StartCorner = Corner.UpperLeft;
- /// <summary>
- /// Which corner should the first cell be placed in?
- /// </summary>
- public Corner startCorner { get { return m_StartCorner; } set { SetProperty(ref m_StartCorner, value); } }
- [SerializeField] protected Axis m_StartAxis = Axis.Horizontal;
- /// <summary>
- /// Which axis should cells be placed along first
- /// </summary>
- /// <remarks>
- /// When startAxis is set to horizontal, an entire row will be filled out before proceeding to the next row. When set to vertical, an entire column will be filled out before proceeding to the next column.
- /// </remarks>
- public Axis startAxis { get { return m_StartAxis; } set { SetProperty(ref m_StartAxis, value); } }
- [SerializeField] protected Vector2 m_CellSize = new Vector2(100, 100);
- /// <summary>
- /// The size to use for each cell in the grid.
- /// </summary>
- public Vector2 cellSize { get { return m_CellSize; } set { SetProperty(ref m_CellSize, value); } }
- [SerializeField] protected Vector2 m_Spacing = Vector2.zero;
- /// <summary>
- /// The spacing to use between layout elements in the grid on both axises.
- /// </summary>
- public Vector2 spacing { get { return m_Spacing; } set { SetProperty(ref m_Spacing, value); } }
- [SerializeField] protected Constraint m_Constraint = Constraint.Flexible;
- /// <summary>
- /// Which constraint to use for the SlideGridLayoutGroup.
- /// </summary>
- /// <remarks>
- /// Specifying a constraint can make the SlideGridLayoutGroup work better in conjunction with a [[ContentSizeFitter]] component. When SlideGridLayoutGroup is used on a RectTransform with a manually specified size, there's no need to specify a constraint.
- /// </remarks>
- public Constraint constraint { get { return m_Constraint; } set { SetProperty(ref m_Constraint, value); } }
- [SerializeField] protected int m_ConstraintCount = 2;
- /// <summary>
- /// How many cells there should be along the constrained axis.
- /// </summary>
- public int constraintCount { get { return m_ConstraintCount; } set { SetProperty(ref m_ConstraintCount, Mathf.Max(1, value)); } }
- [SerializeField] protected bool m_SnapEnable = false;
- /// <summary>
- /// The size to use for each cell in the grid.
- /// </summary>
- public bool SnapEnable { get { return m_SnapEnable; } set { SetProperty(ref m_SnapEnable, value); } }
- private Dictionary<Transform, Vector3> m_SlideTargetPosDic = new Dictionary<Transform, Vector3>();
- public Dictionary<Transform, Vector3> slideTargePosDic
- {
- get { return m_SlideTargetPosDic; }
- }
- private const float kLerp = .2f;
- private const float kSnap = .2f;
- protected SlideGridLayoutGroup()
- { }
- #if UNITY_EDITOR
- protected override void OnValidate()
- {
- base.OnValidate();
- constraintCount = constraintCount;
- }
- #endif
- /// <summary>
- /// Called by the layout system to calculate the horizontal layout size.
- /// Also see ILayoutElement
- /// </summary>
- public override void CalculateLayoutInputHorizontal()
- {
- base.CalculateLayoutInputHorizontal();
- int minColumns = 0;
- int preferredColumns = 0;
- if (m_Constraint == Constraint.FixedColumnCount)
- {
- minColumns = preferredColumns = m_ConstraintCount;
- }
- else if (m_Constraint == Constraint.FixedRowCount)
- {
- minColumns = preferredColumns = Mathf.CeilToInt(rectChildren.Count / (float)m_ConstraintCount - 0.001f);
- }
- else
- {
- minColumns = 1;
- preferredColumns = Mathf.CeilToInt(Mathf.Sqrt(rectChildren.Count));
- }
- SetLayoutInputForAxis(
- padding.horizontal + (cellSize.x + spacing.x) * minColumns - spacing.x,
- padding.horizontal + (cellSize.x + spacing.x) * preferredColumns - spacing.x,
- -1, 0);
- }
- /// <summary>
- /// Called by the layout system to calculate the vertical layout size.
- /// Also see ILayoutElement
- /// </summary>
- public override void CalculateLayoutInputVertical()
- {
- int minRows = 0;
- if (m_Constraint == Constraint.FixedColumnCount)
- {
- minRows = Mathf.CeilToInt(rectChildren.Count / (float)m_ConstraintCount - 0.001f);
- }
- else if (m_Constraint == Constraint.FixedRowCount)
- {
- minRows = m_ConstraintCount;
- }
- else
- {
- float width = rectTransform.rect.width;
- int cellCountX = Mathf.Max(1, Mathf.FloorToInt((width - padding.horizontal + spacing.x + 0.001f) / (cellSize.x + spacing.x)));
- minRows = Mathf.CeilToInt(rectChildren.Count / (float)cellCountX);
- }
- float minSpace = padding.vertical + (cellSize.y + spacing.y) * minRows - spacing.y;
- SetLayoutInputForAxis(minSpace, minSpace, -1, 1);
- }
- /// <summary>
- /// Called by the layout system
- /// Also see ILayoutElement
- /// </summary>
- public override void SetLayoutHorizontal()
- {
- SetCellsAlongAxis(0);
- }
- /// <summary>
- /// Called by the layout system
- /// Also see ILayoutElement
- /// </summary>
- public override void SetLayoutVertical()
- {
- SetCellsAlongAxis(1);
- }
- private void SetCellsAlongAxis(int axis)
- {
- // Normally a Layout Controller should only set horizontal values when invoked for the horizontal axis
- // and only vertical values when invoked for the vertical axis.
- // However, in this case we set both the horizontal and vertical position when invoked for the vertical axis.
- // Since we only set the horizontal position and not the size, it shouldn't affect children's layout,
- // and thus shouldn't break the rule that all horizontal layout must be calculated before all vertical layout.
- if (axis == 0)
- {
- // Only set the sizes when invoked for horizontal axis, not the positions.
- for (int i = 0; i < rectChildren.Count; i++)
- {
- RectTransform rect = rectChildren[i];
- m_Tracker.Add(this, rect,
- DrivenTransformProperties.Anchors |
- DrivenTransformProperties.AnchoredPosition |
- DrivenTransformProperties.SizeDelta);
- rect.anchorMin = Vector2.up;
- rect.anchorMax = Vector2.up;
- rect.sizeDelta = cellSize;
- }
- return;
- }
- bool needRefresh = false;
- float width = rectTransform.rect.size.x;
- float height = rectTransform.rect.size.y;
- int cellCountX = 1;
- int cellCountY = 1;
- if (m_Constraint == Constraint.FixedColumnCount)
- {
- cellCountX = m_ConstraintCount;
- if (rectChildren.Count > cellCountX)
- cellCountY = rectChildren.Count / cellCountX + (rectChildren.Count % cellCountX > 0 ? 1 : 0);
- }
- else if (m_Constraint == Constraint.FixedRowCount)
- {
- cellCountY = m_ConstraintCount;
- if (rectChildren.Count > cellCountY)
- cellCountX = rectChildren.Count / cellCountY + (rectChildren.Count % cellCountY > 0 ? 1 : 0);
- }
- else
- {
- if (cellSize.x + spacing.x <= 0)
- cellCountX = int.MaxValue;
- else
- cellCountX = Mathf.Max(1, Mathf.FloorToInt((width - padding.horizontal + spacing.x + 0.001f) / (cellSize.x + spacing.x)));
- if (cellSize.y + spacing.y <= 0)
- cellCountY = int.MaxValue;
- else
- cellCountY = Mathf.Max(1, Mathf.FloorToInt((height - padding.vertical + spacing.y + 0.001f) / (cellSize.y + spacing.y)));
- }
- int cornerX = (int)startCorner % 2;
- int cornerY = (int)startCorner / 2;
- int cellsPerMainAxis, actualCellCountX, actualCellCountY;
- if (startAxis == Axis.Horizontal)
- {
- cellsPerMainAxis = cellCountX;
- actualCellCountX = Mathf.Clamp(cellCountX, 1, rectChildren.Count);
- actualCellCountY = Mathf.Clamp(cellCountY, 1, Mathf.CeilToInt(rectChildren.Count / (float)cellsPerMainAxis));
- }
- else
- {
- cellsPerMainAxis = cellCountY;
- actualCellCountY = Mathf.Clamp(cellCountY, 1, rectChildren.Count);
- actualCellCountX = Mathf.Clamp(cellCountX, 1, Mathf.CeilToInt(rectChildren.Count / (float)cellsPerMainAxis));
- }
- Vector2 requiredSpace = new Vector2(
- actualCellCountX * cellSize.x + (actualCellCountX - 1) * spacing.x,
- actualCellCountY * cellSize.y + (actualCellCountY - 1) * spacing.y
- );
- Vector2 startOffset = new Vector2(
- GetStartOffset(0, requiredSpace.x),
- GetStartOffset(1, requiredSpace.y)
- );
- for (int i = 0; i < rectChildren.Count; i++)
- {
- int positionX;
- int positionY;
- if (startAxis == Axis.Horizontal)
- {
- positionX = i % cellsPerMainAxis;
- positionY = i / cellsPerMainAxis;
- }
- else
- {
- positionX = i / cellsPerMainAxis;
- positionY = i % cellsPerMainAxis;
- }
- if (cornerX == 1)
- positionX = actualCellCountX - 1 - positionX;
- if (cornerY == 1)
- positionY = actualCellCountY - 1 - positionY;
- if (i < rectChildren.Count - 1)
- {
- float offsetX = GetStartOffsetSnap(rectChildren[i], 0, startOffset.x + (cellSize[0] + spacing[0]) * positionX, cellSize[0], ref needRefresh);
- SetChildAlongAxis(rectChildren[i], 0, offsetX, cellSize[0]);
- float offsetY = GetStartOffsetSnap(rectChildren[i], 1, startOffset.y + (cellSize[1] + spacing[1]) * positionY, cellSize[1], ref needRefresh);
- SetChildAlongAxis(rectChildren[i], 1, offsetY, cellSize[1]);
- }
- else
- {
- SetChildAlongAxis(rectChildren[i], 0, startOffset.x + (cellSize[0] + spacing[0]) * positionX, cellSize[0]);
- SetChildAlongAxis(rectChildren[i], 1, startOffset.y + (cellSize[1] + spacing[1]) * positionY, cellSize[1]);
- }
-
- if (needRefresh)
- {
- SetDirty();
- }
- }
- }
- protected float GetStartOffsetSnap(RectTransform rectTransform, int axis, float targetPos, float size, ref bool needRefresh)
- {
- if (Application.isPlaying)
- {
- if (m_SnapEnable)
- {
- Vector2 pivot = rectTransform.pivot;
- Vector2 anchoredPosition = rectTransform.anchoredPosition;
- float curPos = (axis != 0) ? -(anchoredPosition[axis] + size * (1 - pivot[axis])) : (anchoredPosition[axis] - size * (1 - pivot[axis]));
- if (Mathf.Abs(curPos - targetPos) > kSnap)
- {
- needRefresh = true;
- if (!m_SlideTargetPosDic.ContainsKey(rectTransform))
- {
- m_SlideTargetPosDic.Add(rectTransform, anchoredPosition);
- }
- Vector3 pos = m_SlideTargetPosDic[rectTransform];
- pos[axis] = (axis != 0) ? -targetPos - size * (1 - pivot[axis]) : targetPos + size * pivot[axis];
- m_SlideTargetPosDic[rectTransform] = pos;
- return Mathf.Lerp(curPos, targetPos, kLerp);
- }
- return targetPos;
- }
- }
- return targetPos;
- }
- }
|