8using System.Collections.Generic;
15 [AddComponentMenu(
"Layout/Extensions/Flow Layout Group")]
27 private float _layoutHeight;
28 private float _layoutWidth;
36 base.CalculateLayoutInputHorizontal ();
38 SetLayoutInputForAxis (minWidth, -1, -1, 0);
60 base.CalculateLayoutInputHorizontal ();
62 SetLayoutInputForAxis (minHeight, -1, -1, 1);
70 return childAlignment == TextAnchor.LowerCenter || childAlignment == TextAnchor.MiddleCenter ||
71 childAlignment == TextAnchor.UpperCenter;
79 return childAlignment == TextAnchor.LowerRight || childAlignment == TextAnchor.MiddleRight ||
80 childAlignment == TextAnchor.UpperRight;
88 return childAlignment == TextAnchor.MiddleLeft || childAlignment == TextAnchor.MiddleRight ||
89 childAlignment == TextAnchor.MiddleCenter;
97 return childAlignment == TextAnchor.LowerLeft || childAlignment == TextAnchor.LowerRight ||
98 childAlignment == TextAnchor.LowerCenter;
105 private readonly IList<RectTransform> _itemList =
new List<RectTransform>();
116 var groupHeight = rectTransform.rect.height;
117 var groupWidth = rectTransform.rect.width;
119 float spacingBetweenBars = 0;
120 float spacingBetweenElements = 0;
122 float counterOffset = 0;
124 float workingSize = 0;
126 groupSize = groupHeight;
127 workingSize = groupWidth - padding.left - padding.right;
129 offset = (float)padding.bottom;
130 counterOffset = (
float)padding.top;
132 offset = (float)padding.top;
133 counterOffset = (
float)padding.bottom;
138 groupSize = groupWidth;
139 workingSize = groupHeight - padding.top - padding.bottom;
141 offset = (float)padding.right;
142 counterOffset = (
float)padding.left;
144 offset = (float)padding.left;
145 counterOffset = (
float)padding.right;
151 var currentBarSize = 0f;
152 var currentBarSpace = 0f;
154 for (var i = 0; i < rectChildren.Count; i++) {
157 var child = rectChildren [index];
159 float childOtherSize = 0;
165 child = rectChildren [index];
166 childSize = LayoutUtility.GetPreferredSize (child, 0);
167 childSize = Mathf.Min (childSize, workingSize);
168 childOtherSize = LayoutUtility.GetPreferredSize (child, 1);
169 childOtherSize = Mathf.Min (childOtherSize, workingSize);
174 child = rectChildren [index];
175 childSize = LayoutUtility.GetPreferredSize (child, 1);
176 childSize = Mathf.Min (childSize, workingSize);
177 childOtherSize = LayoutUtility.GetPreferredSize (child, 0);
178 childOtherSize = Mathf.Min (childOtherSize, workingSize);
183 if (currentBarSize + childSize > workingSize) {
185 currentBarSize -= spacingBetweenElements;
190 float newOffset = CalculateRowVerticalOffset (groupSize, offset, currentBarSpace);
191 LayoutRow (_itemList, currentBarSize, currentBarSpace, workingSize, padding.left, newOffset, axis);
193 float newOffset = CalculateColHorizontalOffset (groupSize, offset, currentBarSpace);
194 LayoutCol (_itemList, currentBarSpace, currentBarSize, workingSize, newOffset, padding.top, axis);
202 offset += currentBarSpace;
203 offset += spacingBetweenBars;
210 currentBarSize += childSize;
211 _itemList.Add (child);
214 if (childOtherSize > currentBarSpace) {
215 currentBarSpace = childOtherSize;
219 if (i < rectChildren.Count - 1){
220 currentBarSize += spacingBetweenElements;
228 float newOffset = CalculateRowVerticalOffset (groupHeight, offset, currentBarSpace);
229 currentBarSize -= spacingBetweenElements;
230 LayoutRow (_itemList, currentBarSize, currentBarSpace, workingSize - (
ChildForceExpandWidth ? 0 : spacingBetweenElements), padding.left, newOffset, axis);
232 float newOffset = CalculateColHorizontalOffset(groupWidth, offset, currentBarSpace);
233 currentBarSize -= spacingBetweenElements;
234 LayoutCol(_itemList, currentBarSpace, currentBarSize, workingSize - (
ChildForceExpandHeight ? 0 : spacingBetweenElements), newOffset, padding.top, axis);
241 offset += currentBarSpace;
242 offset += counterOffset;
245 SetLayoutInputForAxis(offset, offset, -1, axis);
250 private float CalculateRowVerticalOffset(
float groupHeight,
float yOffset,
float currentRowHeight)
253 return groupHeight - yOffset - currentRowHeight;
255 return groupHeight * 0.5f - _layoutHeight * 0.5f + yOffset;
261 private float CalculateColHorizontalOffset(
float groupWidth,
float xOffset,
float currentColWidth)
264 return groupWidth - xOffset - currentColWidth;
266 return groupWidth * 0.5f - _layoutWidth * 0.5f + xOffset;
272 protected void LayoutRow(IList<RectTransform> contents,
float rowWidth,
float rowHeight,
float maxWidth,
float xOffset,
float yOffset,
int axis)
277 xPos += (maxWidth - rowWidth) * 0.5f;
279 xPos += (maxWidth - rowWidth);
283 var extraSpacing = 0f;
286 extraWidth = (maxWidth - rowWidth)/_itemList.Count;
289 extraSpacing = (maxWidth - rowWidth)/(_itemList.Count - 1);
290 if (_itemList.Count > 1) {
292 xPos -= extraSpacing * 0.5f * (_itemList.Count - 1);
294 xPos -= extraSpacing * (_itemList.Count - 1);
299 for (var j = 0; j < _itemList.Count; j++) {
303 var rowChild = _itemList[index];
305 var rowChildWidth = LayoutUtility.GetPreferredSize(rowChild, 0) + extraWidth;
306 var rowChildHeight = LayoutUtility.GetPreferredSize(rowChild, 1);
309 rowChildHeight = rowHeight;
311 rowChildWidth = Mathf.Min(rowChildWidth, maxWidth);
316 yPos += (rowHeight - rowChildHeight) * 0.5f;
318 yPos += (rowHeight - rowChildHeight);
322 xPos += extraSpacing;
326 SetChildAlongAxis (rowChild, 0, xPos, rowChildWidth);
328 SetChildAlongAxis (rowChild, 1, yPos, rowChildHeight);
332 if (j < _itemList.Count - 1) {
338 protected void LayoutCol(IList<RectTransform> contents,
float colWidth,
float colHeight,
float maxHeight,
float xOffset,
float yOffset,
int axis)
343 yPos += (maxHeight - colHeight) * 0.5f;
345 yPos += (maxHeight - colHeight);
348 var extraHeight = 0f;
349 var extraSpacing = 0f;
352 extraHeight = (maxHeight - colHeight)/_itemList.Count;
355 extraSpacing = (maxHeight - colHeight)/(_itemList.Count - 1);
356 if (_itemList.Count > 1) {
358 yPos -= extraSpacing * 0.5f * (_itemList.Count - 1);
360 yPos -= extraSpacing * (_itemList.Count - 1);
365 for (var j = 0; j < _itemList.Count; j++) {
369 var rowChild = _itemList[index];
371 var rowChildWidth = LayoutUtility.GetPreferredSize(rowChild, 0) ;
372 var rowChildHeight = LayoutUtility.GetPreferredSize(rowChild, 1) + extraHeight;
375 rowChildWidth = colWidth;
378 rowChildHeight = Mathf.Min(rowChildHeight, maxHeight);
383 xPos += (colWidth - rowChildWidth) * 0.5f;
385 xPos += (colWidth - rowChildWidth);
390 yPos += extraSpacing;
394 SetChildAlongAxis (rowChild, 0, xPos, rowChildWidth);
396 SetChildAlongAxis (rowChild, 1, yPos, rowChildHeight);
400 if (j < _itemList.Count - 1) {
409 for (var i = 0; i < rectChildren.Count; i++) {
410 var w = LayoutUtility.GetMinWidth(rectChildren[i]);
412 max = Mathf.Max(w, max);
420 for (var i = 0; i < rectChildren.Count; i++) {
421 var w = LayoutUtility.GetMinHeight(rectChildren[i]);
423 max = Mathf.Max(w, max);
Layout Group controller that arranges children in bars, fitting as many on a line until total size ex...
bool ChildForceExpandWidth
float SetLayout(int axis, bool layoutInput)
Main layout method
void LayoutRow(IList< RectTransform > contents, float rowWidth, float rowHeight, float maxWidth, float xOffset, float yOffset, int axis)
bool ChildForceExpandHeight
float GetGreatestMinimumChildWidth()
void LayoutCol(IList< RectTransform > contents, float colWidth, float colHeight, float maxHeight, float xOffset, float yOffset, int axis)
float GetGreatestMinimumChildHeigth()
override void CalculateLayoutInputHorizontal()
override void SetLayoutHorizontal()
bool ExpandHorizontalSpacing
override void CalculateLayoutInputVertical()
override void SetLayoutVertical()
Credit Erdener Gonenc - @PixelEnvision.