SlideGridLayoutGroup.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. /// <summary>
  4. /// Layout class to arrange children elements in a grid format.
  5. /// </summary>
  6. /// <remarks>
  7. /// 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.
  8. /// </remarks>
  9. public class SlideGridLayoutGroup : UnityEngine.UI.LayoutGroup
  10. {
  11. /// <summary>
  12. /// Which corner is the starting corner for the grid.
  13. /// </summary>
  14. public enum Corner
  15. {
  16. /// <summary>
  17. /// Upper Left corner.
  18. /// </summary>
  19. UpperLeft = 0,
  20. /// <summary>
  21. /// Upper Right corner.
  22. /// </summary>
  23. UpperRight = 1,
  24. /// <summary>
  25. /// Lower Left corner.
  26. /// </summary>
  27. LowerLeft = 2,
  28. /// <summary>
  29. /// Lower Right corner.
  30. /// </summary>
  31. LowerRight = 3
  32. }
  33. /// <summary>
  34. /// The grid axis we are looking at.
  35. /// </summary>
  36. /// <remarks>
  37. /// As the storage is a [][] we make access easier by passing a axis.
  38. /// </remarks>
  39. public enum Axis
  40. {
  41. /// <summary>
  42. /// Horizontal axis
  43. /// </summary>
  44. Horizontal = 0,
  45. /// <summary>
  46. /// Vertical axis.
  47. /// </summary>
  48. Vertical = 1
  49. }
  50. /// <summary>
  51. /// Constraint type on either the number of columns or rows.
  52. /// </summary>
  53. public enum Constraint
  54. {
  55. /// <summary>
  56. /// Don't constrain the number of rows or columns.
  57. /// </summary>
  58. Flexible = 0,
  59. /// <summary>
  60. /// Constrain the number of columns to a specified number.
  61. /// </summary>
  62. FixedColumnCount = 1,
  63. /// <summary>
  64. /// Constraint the number of rows to a specified number.
  65. /// </summary>
  66. FixedRowCount = 2
  67. }
  68. [SerializeField] protected Corner m_StartCorner = Corner.UpperLeft;
  69. /// <summary>
  70. /// Which corner should the first cell be placed in?
  71. /// </summary>
  72. public Corner startCorner { get { return m_StartCorner; } set { SetProperty(ref m_StartCorner, value); } }
  73. [SerializeField] protected Axis m_StartAxis = Axis.Horizontal;
  74. /// <summary>
  75. /// Which axis should cells be placed along first
  76. /// </summary>
  77. /// <remarks>
  78. /// 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.
  79. /// </remarks>
  80. public Axis startAxis { get { return m_StartAxis; } set { SetProperty(ref m_StartAxis, value); } }
  81. [SerializeField] protected Vector2 m_CellSize = new Vector2(100, 100);
  82. /// <summary>
  83. /// The size to use for each cell in the grid.
  84. /// </summary>
  85. public Vector2 cellSize { get { return m_CellSize; } set { SetProperty(ref m_CellSize, value); } }
  86. [SerializeField] protected Vector2 m_Spacing = Vector2.zero;
  87. /// <summary>
  88. /// The spacing to use between layout elements in the grid on both axises.
  89. /// </summary>
  90. public Vector2 spacing { get { return m_Spacing; } set { SetProperty(ref m_Spacing, value); } }
  91. [SerializeField] protected Constraint m_Constraint = Constraint.Flexible;
  92. /// <summary>
  93. /// Which constraint to use for the SlideGridLayoutGroup.
  94. /// </summary>
  95. /// <remarks>
  96. /// 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.
  97. /// </remarks>
  98. public Constraint constraint { get { return m_Constraint; } set { SetProperty(ref m_Constraint, value); } }
  99. [SerializeField] protected int m_ConstraintCount = 2;
  100. /// <summary>
  101. /// How many cells there should be along the constrained axis.
  102. /// </summary>
  103. public int constraintCount { get { return m_ConstraintCount; } set { SetProperty(ref m_ConstraintCount, Mathf.Max(1, value)); } }
  104. [SerializeField] protected bool m_SnapEnable = false;
  105. /// <summary>
  106. /// The size to use for each cell in the grid.
  107. /// </summary>
  108. public bool SnapEnable { get { return m_SnapEnable; } set { SetProperty(ref m_SnapEnable, value); } }
  109. private Dictionary<Transform, Vector3> m_SlideTargetPosDic = new Dictionary<Transform, Vector3>();
  110. public Dictionary<Transform, Vector3> slideTargePosDic
  111. {
  112. get { return m_SlideTargetPosDic; }
  113. }
  114. private const float kLerp = .2f;
  115. private const float kSnap = .2f;
  116. protected SlideGridLayoutGroup()
  117. { }
  118. #if UNITY_EDITOR
  119. protected override void OnValidate()
  120. {
  121. base.OnValidate();
  122. constraintCount = constraintCount;
  123. }
  124. #endif
  125. /// <summary>
  126. /// Called by the layout system to calculate the horizontal layout size.
  127. /// Also see ILayoutElement
  128. /// </summary>
  129. public override void CalculateLayoutInputHorizontal()
  130. {
  131. base.CalculateLayoutInputHorizontal();
  132. int minColumns = 0;
  133. int preferredColumns = 0;
  134. if (m_Constraint == Constraint.FixedColumnCount)
  135. {
  136. minColumns = preferredColumns = m_ConstraintCount;
  137. }
  138. else if (m_Constraint == Constraint.FixedRowCount)
  139. {
  140. minColumns = preferredColumns = Mathf.CeilToInt(rectChildren.Count / (float)m_ConstraintCount - 0.001f);
  141. }
  142. else
  143. {
  144. minColumns = 1;
  145. preferredColumns = Mathf.CeilToInt(Mathf.Sqrt(rectChildren.Count));
  146. }
  147. SetLayoutInputForAxis(
  148. padding.horizontal + (cellSize.x + spacing.x) * minColumns - spacing.x,
  149. padding.horizontal + (cellSize.x + spacing.x) * preferredColumns - spacing.x,
  150. -1, 0);
  151. }
  152. /// <summary>
  153. /// Called by the layout system to calculate the vertical layout size.
  154. /// Also see ILayoutElement
  155. /// </summary>
  156. public override void CalculateLayoutInputVertical()
  157. {
  158. int minRows = 0;
  159. if (m_Constraint == Constraint.FixedColumnCount)
  160. {
  161. minRows = Mathf.CeilToInt(rectChildren.Count / (float)m_ConstraintCount - 0.001f);
  162. }
  163. else if (m_Constraint == Constraint.FixedRowCount)
  164. {
  165. minRows = m_ConstraintCount;
  166. }
  167. else
  168. {
  169. float width = rectTransform.rect.width;
  170. int cellCountX = Mathf.Max(1, Mathf.FloorToInt((width - padding.horizontal + spacing.x + 0.001f) / (cellSize.x + spacing.x)));
  171. minRows = Mathf.CeilToInt(rectChildren.Count / (float)cellCountX);
  172. }
  173. float minSpace = padding.vertical + (cellSize.y + spacing.y) * minRows - spacing.y;
  174. SetLayoutInputForAxis(minSpace, minSpace, -1, 1);
  175. }
  176. /// <summary>
  177. /// Called by the layout system
  178. /// Also see ILayoutElement
  179. /// </summary>
  180. public override void SetLayoutHorizontal()
  181. {
  182. SetCellsAlongAxis(0);
  183. }
  184. /// <summary>
  185. /// Called by the layout system
  186. /// Also see ILayoutElement
  187. /// </summary>
  188. public override void SetLayoutVertical()
  189. {
  190. SetCellsAlongAxis(1);
  191. }
  192. private void SetCellsAlongAxis(int axis)
  193. {
  194. // Normally a Layout Controller should only set horizontal values when invoked for the horizontal axis
  195. // and only vertical values when invoked for the vertical axis.
  196. // However, in this case we set both the horizontal and vertical position when invoked for the vertical axis.
  197. // Since we only set the horizontal position and not the size, it shouldn't affect children's layout,
  198. // and thus shouldn't break the rule that all horizontal layout must be calculated before all vertical layout.
  199. if (axis == 0)
  200. {
  201. // Only set the sizes when invoked for horizontal axis, not the positions.
  202. for (int i = 0; i < rectChildren.Count; i++)
  203. {
  204. RectTransform rect = rectChildren[i];
  205. m_Tracker.Add(this, rect,
  206. DrivenTransformProperties.Anchors |
  207. DrivenTransformProperties.AnchoredPosition |
  208. DrivenTransformProperties.SizeDelta);
  209. rect.anchorMin = Vector2.up;
  210. rect.anchorMax = Vector2.up;
  211. rect.sizeDelta = cellSize;
  212. }
  213. return;
  214. }
  215. bool needRefresh = false;
  216. float width = rectTransform.rect.size.x;
  217. float height = rectTransform.rect.size.y;
  218. int cellCountX = 1;
  219. int cellCountY = 1;
  220. if (m_Constraint == Constraint.FixedColumnCount)
  221. {
  222. cellCountX = m_ConstraintCount;
  223. if (rectChildren.Count > cellCountX)
  224. cellCountY = rectChildren.Count / cellCountX + (rectChildren.Count % cellCountX > 0 ? 1 : 0);
  225. }
  226. else if (m_Constraint == Constraint.FixedRowCount)
  227. {
  228. cellCountY = m_ConstraintCount;
  229. if (rectChildren.Count > cellCountY)
  230. cellCountX = rectChildren.Count / cellCountY + (rectChildren.Count % cellCountY > 0 ? 1 : 0);
  231. }
  232. else
  233. {
  234. if (cellSize.x + spacing.x <= 0)
  235. cellCountX = int.MaxValue;
  236. else
  237. cellCountX = Mathf.Max(1, Mathf.FloorToInt((width - padding.horizontal + spacing.x + 0.001f) / (cellSize.x + spacing.x)));
  238. if (cellSize.y + spacing.y <= 0)
  239. cellCountY = int.MaxValue;
  240. else
  241. cellCountY = Mathf.Max(1, Mathf.FloorToInt((height - padding.vertical + spacing.y + 0.001f) / (cellSize.y + spacing.y)));
  242. }
  243. int cornerX = (int)startCorner % 2;
  244. int cornerY = (int)startCorner / 2;
  245. int cellsPerMainAxis, actualCellCountX, actualCellCountY;
  246. if (startAxis == Axis.Horizontal)
  247. {
  248. cellsPerMainAxis = cellCountX;
  249. actualCellCountX = Mathf.Clamp(cellCountX, 1, rectChildren.Count);
  250. actualCellCountY = Mathf.Clamp(cellCountY, 1, Mathf.CeilToInt(rectChildren.Count / (float)cellsPerMainAxis));
  251. }
  252. else
  253. {
  254. cellsPerMainAxis = cellCountY;
  255. actualCellCountY = Mathf.Clamp(cellCountY, 1, rectChildren.Count);
  256. actualCellCountX = Mathf.Clamp(cellCountX, 1, Mathf.CeilToInt(rectChildren.Count / (float)cellsPerMainAxis));
  257. }
  258. Vector2 requiredSpace = new Vector2(
  259. actualCellCountX * cellSize.x + (actualCellCountX - 1) * spacing.x,
  260. actualCellCountY * cellSize.y + (actualCellCountY - 1) * spacing.y
  261. );
  262. Vector2 startOffset = new Vector2(
  263. GetStartOffset(0, requiredSpace.x),
  264. GetStartOffset(1, requiredSpace.y)
  265. );
  266. for (int i = 0; i < rectChildren.Count; i++)
  267. {
  268. int positionX;
  269. int positionY;
  270. if (startAxis == Axis.Horizontal)
  271. {
  272. positionX = i % cellsPerMainAxis;
  273. positionY = i / cellsPerMainAxis;
  274. }
  275. else
  276. {
  277. positionX = i / cellsPerMainAxis;
  278. positionY = i % cellsPerMainAxis;
  279. }
  280. if (cornerX == 1)
  281. positionX = actualCellCountX - 1 - positionX;
  282. if (cornerY == 1)
  283. positionY = actualCellCountY - 1 - positionY;
  284. if (i < rectChildren.Count - 1)
  285. {
  286. float offsetX = GetStartOffsetSnap(rectChildren[i], 0, startOffset.x + (cellSize[0] + spacing[0]) * positionX, cellSize[0], ref needRefresh);
  287. SetChildAlongAxis(rectChildren[i], 0, offsetX, cellSize[0]);
  288. float offsetY = GetStartOffsetSnap(rectChildren[i], 1, startOffset.y + (cellSize[1] + spacing[1]) * positionY, cellSize[1], ref needRefresh);
  289. SetChildAlongAxis(rectChildren[i], 1, offsetY, cellSize[1]);
  290. }
  291. else
  292. {
  293. SetChildAlongAxis(rectChildren[i], 0, startOffset.x + (cellSize[0] + spacing[0]) * positionX, cellSize[0]);
  294. SetChildAlongAxis(rectChildren[i], 1, startOffset.y + (cellSize[1] + spacing[1]) * positionY, cellSize[1]);
  295. }
  296. if (needRefresh)
  297. {
  298. SetDirty();
  299. }
  300. }
  301. }
  302. protected float GetStartOffsetSnap(RectTransform rectTransform, int axis, float targetPos, float size, ref bool needRefresh)
  303. {
  304. if (Application.isPlaying)
  305. {
  306. if (m_SnapEnable)
  307. {
  308. Vector2 pivot = rectTransform.pivot;
  309. Vector2 anchoredPosition = rectTransform.anchoredPosition;
  310. float curPos = (axis != 0) ? -(anchoredPosition[axis] + size * (1 - pivot[axis])) : (anchoredPosition[axis] - size * (1 - pivot[axis]));
  311. if (Mathf.Abs(curPos - targetPos) > kSnap)
  312. {
  313. needRefresh = true;
  314. if (!m_SlideTargetPosDic.ContainsKey(rectTransform))
  315. {
  316. m_SlideTargetPosDic.Add(rectTransform, anchoredPosition);
  317. }
  318. Vector3 pos = m_SlideTargetPosDic[rectTransform];
  319. pos[axis] = (axis != 0) ? -targetPos - size * (1 - pivot[axis]) : targetPos + size * pivot[axis];
  320. m_SlideTargetPosDic[rectTransform] = pos;
  321. return Mathf.Lerp(curPos, targetPos, kLerp);
  322. }
  323. return targetPos;
  324. }
  325. }
  326. return targetPos;
  327. }
  328. }