Clean up: Removed duplicate calculations and unnecessary intermediate variables.

--HG--
branch : develop_5.3
release
Rahul Raman 2016-12-27 20:46:57 +00:00
parent ff95c38ee2
commit da480b1f8b
1 changed files with 23 additions and 41 deletions

View File

@ -86,6 +86,8 @@ namespace UnityEngine.UI.Extensions
} }
} }
private float[] maxPreferredHeightInRows;
public override void CalculateLayoutInputHorizontal() public override void CalculateLayoutInputHorizontal()
{ {
base.CalculateLayoutInputHorizontal(); base.CalculateLayoutInputHorizontal();
@ -108,6 +110,8 @@ namespace UnityEngine.UI.Extensions
{ {
int rowCount = Mathf.CeilToInt(rectChildren.Count / (float)columnWidths.Length); int rowCount = Mathf.CeilToInt(rectChildren.Count / (float)columnWidths.Length);
maxPreferredHeightInRows = new float[rowCount];
float totalMinHeight = padding.vertical; float totalMinHeight = padding.vertical;
float totalPreferredHeight = padding.vertical; float totalPreferredHeight = padding.vertical;
@ -139,10 +143,10 @@ namespace UnityEngine.UI.Extensions
totalMinHeight += maxMinimumHeightInRow; totalMinHeight += maxMinimumHeightInRow;
maxPreferredHeightInRow = Mathf.Max(minimumRowHeight, maxPreferredHeightInRow); maxPreferredHeightInRow = Mathf.Max(minimumRowHeight, maxPreferredHeightInRow);
maxPreferredHeightInRows[i] = maxPreferredHeightInRow;
totalPreferredHeight += maxPreferredHeightInRow; totalPreferredHeight += maxPreferredHeightInRow;
} }
totalPreferredHeight = Mathf.Max(totalMinHeight, totalPreferredHeight); totalPreferredHeight = Mathf.Max(totalMinHeight, totalPreferredHeight);
SetLayoutInputForAxis(totalMinHeight, totalPreferredHeight, 1, 1); SetLayoutInputForAxis(totalMinHeight, totalPreferredHeight, 1, 1);
} }
@ -177,50 +181,32 @@ namespace UnityEngine.UI.Extensions
int cornerY = (int)startCorner / 2; int cornerY = (int)startCorner / 2;
Vector2 startOffset = new Vector2(); Vector2 startOffset = new Vector2();
Vector2 requiredSizeWithoutPadding = new Vector2();
float requiredWidthWithoutPadding = 0; for (int i = 0; i < columnCount; i++)
for (int i = 0; i < columnWidths.Length; i++)
{ {
requiredWidthWithoutPadding += columnWidths[i]; requiredSizeWithoutPadding.x += columnWidths[i];
requiredWidthWithoutPadding += cellSpacing; requiredSizeWithoutPadding.x += cellSpacing;
} }
requiredWidthWithoutPadding -= cellSpacing; requiredSizeWithoutPadding.x -= cellSpacing;
startOffset.x = GetStartOffset(0, requiredWidthWithoutPadding); startOffset.x = GetStartOffset(0, requiredSizeWithoutPadding.x);
float requiredHeightWithoutPadding = 0;
float[] maxPreferredHeightInRows = new float[rowCount];
for (int i = 0; i < rowCount; i++) for (int i = 0; i < rowCount; i++)
{ {
float maxPreferredHeightInRow = 0; requiredSizeWithoutPadding.y += maxPreferredHeightInRows[i];
requiredSizeWithoutPadding.y += rowSpacing;
for (int j = 0; j < columnCount; j++)
{
int childIndex = (i * columnCount) + j;
if (childIndex >= rectChildren.Count)
break;
maxPreferredHeightInRow = Mathf.Max(LayoutUtility.GetPreferredHeight(rectChildren[childIndex]), maxPreferredHeightInRow);
} }
maxPreferredHeightInRow = Mathf.Max(minimumRowHeight, maxPreferredHeightInRow); requiredSizeWithoutPadding.y -= rowSpacing;
maxPreferredHeightInRows[i] = maxPreferredHeightInRow;
requiredHeightWithoutPadding += maxPreferredHeightInRow;
requiredHeightWithoutPadding += rowSpacing;
}
requiredHeightWithoutPadding -= rowSpacing; startOffset.y = GetStartOffset(1, requiredSizeWithoutPadding.y);
startOffset.y = GetStartOffset(1, requiredHeightWithoutPadding);
if (cornerX == 1) if (cornerX == 1)
startOffset.x += requiredWidthWithoutPadding; startOffset.x += requiredSizeWithoutPadding.x;
if (cornerY == 1) if (cornerY == 1)
startOffset.y += requiredHeightWithoutPadding; startOffset.y += requiredSizeWithoutPadding.y;
float positionY = startOffset.y; float positionY = startOffset.y;
@ -228,8 +214,6 @@ namespace UnityEngine.UI.Extensions
{ {
float positionX = startOffset.x; float positionX = startOffset.x;
float sizeYOfRect = maxPreferredHeightInRows[i] + rowSpacing;
if (cornerY == 1) if (cornerY == 1)
positionY -= maxPreferredHeightInRows[i]; positionY -= maxPreferredHeightInRows[i];
@ -240,8 +224,6 @@ namespace UnityEngine.UI.Extensions
if (childIndex >= rectChildren.Count) if (childIndex >= rectChildren.Count)
break; break;
float sizeXOfRect = columnWidths[j] + cellSpacing;
if (cornerX == 1) if (cornerX == 1)
positionX -= columnWidths[j]; positionX -= columnWidths[j];
@ -251,13 +233,13 @@ namespace UnityEngine.UI.Extensions
if (cornerX == 1) if (cornerX == 1)
positionX -= cellSpacing; positionX -= cellSpacing;
else else
positionX += sizeXOfRect; positionX += columnWidths[j] + cellSpacing;
} }
if (cornerY == 1) if (cornerY == 1)
positionY -= rowSpacing; positionY -= rowSpacing;
else else
positionY += sizeYOfRect; positionY += maxPreferredHeightInRows[i] + rowSpacing;
} }
} }
} }