private void CollectElements() { m_elementList.Clear(); m_staticElementList.Clear(); m_dynamicElementList.Clear(); for (int i = 0; i < transform.childCount; i++) { Transform child = rectTransform.GetChild(i); if (!child.gameObject.activeInHierarchy) { continue; } UILayoutGroupElement element = child.GetComponent <UILayoutGroupElement>(); if (element == null) { continue; } m_elementList.Add(element); UILayoutGroupElementStatic staticElement = element as UILayoutGroupElementStatic; if (staticElement != null) { m_staticElementList.Add(staticElement); } UILayoutGroupElementDynamic dynamicElement = element as UILayoutGroupElementDynamic; if (dynamicElement != null) { m_dynamicElementList.Add(dynamicElement); } } }
private void SetCellsAlongAxis(int axis) { if (axis == 0) { for (int i = 0; i < elementList.Count; i++) { UILayoutGroupElement element = elementList[i]; RectTransform rect = element.transform as RectTransform; m_tracker.Add(this, rect, DrivenTransformProperties.Anchors | DrivenTransformProperties.AnchoredPosition | DrivenTransformProperties.SizeDelta); rect.anchorMin = Vector2.up; rect.anchorMax = Vector2.up; rect.sizeDelta = cellSize; } return; } float width = rectTransform.rect.size.x; float height = rectTransform.rect.size.y; int virtualItemCount = m_maxIndex; int cellCountX = 1; int cellCountY = 1; if (m_constraint == Constraint.FixedColumnCount) { cellCountX = m_constraintCount; cellCountY = Mathf.CeilToInt(virtualItemCount / (float)cellCountX - 0.001f); } else if (m_constraint == Constraint.FixedRowCount) { cellCountY = m_constraintCount; cellCountX = Mathf.CeilToInt(virtualItemCount / (float)cellCountY - 0.001f); } 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, virtualItemCount); actualCellCountY = Mathf.Clamp(cellCountY, 1, Mathf.CeilToInt(virtualItemCount / (float)cellsPerMainAxis)); } else { cellsPerMainAxis = cellCountY; actualCellCountY = Mathf.Clamp(cellCountY, 1, virtualItemCount); actualCellCountX = Mathf.Clamp(cellCountX, 1, Mathf.CeilToInt(virtualItemCount / (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 < elementList.Count; i++) { UILayoutGroupElement element = elementList[i]; int index = element.index; int positionX; int positionY; if (startAxis == Axis.Horizontal) { positionX = index % cellsPerMainAxis; positionY = index / cellsPerMainAxis; } else { positionX = index / cellsPerMainAxis; positionY = index % cellsPerMainAxis; } if (cornerX == 1) { positionX = actualCellCountX - 1 - positionX; } if (cornerY == 1) { positionY = actualCellCountY - 1 - positionY; } SetChildAlongAxis(elementList[i].transform as RectTransform, 0, startOffset.x + (cellSize[0] + spacing[0]) * positionX, cellSize[0]); SetChildAlongAxis(elementList[i].transform as RectTransform, 1, startOffset.y + (cellSize[1] + spacing[1]) * positionY, cellSize[1]); } }