Refactored CalculateLayoutInputVertical; Released memory used by temporary array for reclaiming by the GC; Added comments to help understand the script better
--HG-- branch : develop_5.3release
parent
1d251301eb
commit
6888cb500d
|
@ -100,6 +100,7 @@ namespace UnityEngine.UI.Extensions
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Temporarily stores data generated during the execution CalculateLayoutInputVertical for use in SetLayoutVertical
|
||||||
private float[] preferredRowHeights;
|
private float[] preferredRowHeights;
|
||||||
|
|
||||||
public override void CalculateLayoutInputHorizontal()
|
public override void CalculateLayoutInputHorizontal()
|
||||||
|
@ -141,37 +142,42 @@ namespace UnityEngine.UI.Extensions
|
||||||
|
|
||||||
if (flexibleRowHeight)
|
if (flexibleRowHeight)
|
||||||
{
|
{
|
||||||
// Find the max value for minimum and preferred heights in each row
|
// If flexibleRowHeight is enabled, find the max value for minimum and preferred heights in each row
|
||||||
for (int i = 0; i < rowCount; i++)
|
|
||||||
|
float maxMinimumHeightInRow = 0;
|
||||||
|
float maxPreferredHeightInRow = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < rectChildren.Count; i++)
|
||||||
{
|
{
|
||||||
float maxMinimumHeightInRow = 0;
|
int currentRowIndex = i / columnCount;
|
||||||
float maxPreferredHeightInRow = 0;
|
int currentColumnIndex = i % columnCount;
|
||||||
|
|
||||||
for (int j = 0; j < columnCount; j++)
|
// If it's the first cell in the row, reset heights for the row
|
||||||
|
if (currentColumnIndex == 0)
|
||||||
{
|
{
|
||||||
int childIndex = (i * columnCount) + j;
|
maxMinimumHeightInRow = minimumRowHeight;
|
||||||
|
maxPreferredHeightInRow = minimumRowHeight;
|
||||||
if (childIndex >= rectChildren.Count)
|
|
||||||
break;
|
|
||||||
|
|
||||||
maxPreferredHeightInRow = Mathf.Max(LayoutUtility.GetPreferredHeight(rectChildren[childIndex]), maxPreferredHeightInRow);
|
|
||||||
maxMinimumHeightInRow = Mathf.Max(LayoutUtility.GetMinHeight(rectChildren[childIndex]), maxMinimumHeightInRow);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
maxMinimumHeightInRow = Mathf.Max(minimumRowHeight, maxMinimumHeightInRow);
|
maxPreferredHeightInRow = Mathf.Max(LayoutUtility.GetPreferredHeight(rectChildren[i]), maxPreferredHeightInRow);
|
||||||
totalMinHeight += maxMinimumHeightInRow;
|
maxMinimumHeightInRow = Mathf.Max(LayoutUtility.GetMinHeight(rectChildren[i]), maxMinimumHeightInRow);
|
||||||
|
|
||||||
maxPreferredHeightInRow = Mathf.Max(minimumRowHeight, maxPreferredHeightInRow);
|
// If it's the last cell in the row, or if it's the last cell and the row is incomplete, set calculated heights
|
||||||
preferredRowHeights[i] = maxPreferredHeightInRow;
|
if (currentColumnIndex == columnCount - 1 || (i == rectChildren.Count - 1 && currentRowIndex == rowCount - 1))
|
||||||
totalPreferredHeight += maxPreferredHeightInRow;
|
{
|
||||||
|
totalMinHeight += maxMinimumHeightInRow;
|
||||||
|
totalPreferredHeight += maxPreferredHeightInRow;
|
||||||
|
|
||||||
|
// Add calculated row height to a commonly accessible array for reuse in SetLayoutVertical()
|
||||||
|
preferredRowHeights[currentRowIndex] = maxPreferredHeightInRow;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
// If flexibleRowHeight is disabled, then use the minimumRowHeight to calculate vertical layout information
|
||||||
for (int i = 0; i < rowCount; i++)
|
for (int i = 0; i < rowCount; i++)
|
||||||
{
|
|
||||||
preferredRowHeights[i] = minimumRowHeight;
|
preferredRowHeights[i] = minimumRowHeight;
|
||||||
}
|
|
||||||
|
|
||||||
totalMinHeight += rowCount * minimumRowHeight;
|
totalMinHeight += rowCount * minimumRowHeight;
|
||||||
totalPreferredHeight = totalMinHeight;
|
totalPreferredHeight = totalMinHeight;
|
||||||
|
@ -183,6 +189,7 @@ namespace UnityEngine.UI.Extensions
|
||||||
|
|
||||||
public override void SetLayoutHorizontal()
|
public override void SetLayoutHorizontal()
|
||||||
{
|
{
|
||||||
|
// If no column width is defined, then assign a reasonable default
|
||||||
if (columnWidths.Length == 0)
|
if (columnWidths.Length == 0)
|
||||||
columnWidths = new float[1] { 0f };
|
columnWidths = new float[1] { 0f };
|
||||||
|
|
||||||
|
@ -191,6 +198,8 @@ namespace UnityEngine.UI.Extensions
|
||||||
|
|
||||||
float startOffset = 0;
|
float startOffset = 0;
|
||||||
float requiredSizeWithoutPadding = 0;
|
float requiredSizeWithoutPadding = 0;
|
||||||
|
|
||||||
|
// We calculate the actual cell count for cases where the number of children is lesser than the number of columns
|
||||||
int actualCellCount = Mathf.Min(rectChildren.Count, columnWidths.Length);
|
int actualCellCount = Mathf.Min(rectChildren.Count, columnWidths.Length);
|
||||||
|
|
||||||
for (int i = 0; i < actualCellCount; i++)
|
for (int i = 0; i < actualCellCount; i++)
|
||||||
|
@ -210,9 +219,9 @@ namespace UnityEngine.UI.Extensions
|
||||||
|
|
||||||
for (int i = 0; i < rectChildren.Count; i++)
|
for (int i = 0; i < rectChildren.Count; i++)
|
||||||
{
|
{
|
||||||
int currentRowIndex = i / columnCount;
|
|
||||||
int currentColumnIndex = i % columnCount;
|
int currentColumnIndex = i % columnCount;
|
||||||
|
|
||||||
|
// If it's the first cell in the row, reset positionX
|
||||||
if (currentColumnIndex == 0)
|
if (currentColumnIndex == 0)
|
||||||
positionX = startOffset;
|
positionX = startOffset;
|
||||||
|
|
||||||
|
@ -258,11 +267,13 @@ namespace UnityEngine.UI.Extensions
|
||||||
int currentRowIndex = i / columnCount;
|
int currentRowIndex = i / columnCount;
|
||||||
int currentColumnIndex = i % columnCount;
|
int currentColumnIndex = i % columnCount;
|
||||||
|
|
||||||
|
// If it's the first cell in the row and start corner is one of the bottom corners, then modify positionY appropriately
|
||||||
if (currentColumnIndex == 0 && cornerY == 1)
|
if (currentColumnIndex == 0 && cornerY == 1)
|
||||||
positionY -= preferredRowHeights[currentRowIndex];
|
positionY -= preferredRowHeights[currentRowIndex];
|
||||||
|
|
||||||
SetChildAlongAxis(rectChildren[i], 1, positionY, preferredRowHeights[currentRowIndex]);
|
SetChildAlongAxis(rectChildren[i], 1, positionY, preferredRowHeights[currentRowIndex]);
|
||||||
|
|
||||||
|
// If it's the first last cell in the row, then modify positionY appropriately
|
||||||
if (currentColumnIndex == columnCount - 1)
|
if (currentColumnIndex == columnCount - 1)
|
||||||
{
|
{
|
||||||
if (cornerY == 1)
|
if (cornerY == 1)
|
||||||
|
@ -271,6 +282,9 @@ namespace UnityEngine.UI.Extensions
|
||||||
positionY += preferredRowHeights[currentRowIndex] + rowSpacing;
|
positionY += preferredRowHeights[currentRowIndex] + rowSpacing;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set preferredRowHeights to null to free memory
|
||||||
|
preferredRowHeights = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue