diff --git a/Runtime/Helper/GridLayoutCalculator.cs b/Runtime/Helper/GridLayoutCalculator.cs index c86f21d..30b8cd2 100644 --- a/Runtime/Helper/GridLayoutCalculator.cs +++ b/Runtime/Helper/GridLayoutCalculator.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; @@ -99,10 +99,18 @@ namespace XericUI.Helper if (cornerY == 1) gridY = actualCellCountY - 1 - gridY; - // 计算最终位置(固定方向:向右下) + // 计算最终位置(根据startCorner决定方向) + // 在Unity UI坐标系中:左下角是(0,0),向右X增加,向上Y增加 + // UpperLeft (0): 起点左上,向右(+)填充,向下(-)排列 + // UpperRight (1): 起点右上,向左(-)填充,向下(-)排列 + // LowerLeft (2): 起点左下,向右(+)填充,向上(+)排列 + // LowerRight (3): 起点右下,向左(-)填充,向上(+)排列 + float xDir = cornerX == 0 ? 1f : -1f; // 左边开始向右,右边开始向左 + float yDir = cornerY == 0 ? -1f : 1f; // 上边开始向下,下边开始向上 + Vector2 position = new Vector2( - startOffset.x + (cellSize.x + spacing.x) * gridX, - startOffset.y + (cellSize.y + spacing.y) * gridY + startOffset.x + (cellSize.x + spacing.x) * gridX * xDir, + startOffset.y + (cellSize.y + spacing.y) * gridY * yDir ); layoutItems.Add(new LayoutItem diff --git a/Runtime/OverrideLayout/XericScrollRect.cs b/Runtime/OverrideLayout/XericScrollRect.cs index 3403376..cce428b 100644 --- a/Runtime/OverrideLayout/XericScrollRect.cs +++ b/Runtime/OverrideLayout/XericScrollRect.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using UnityEngine; @@ -153,7 +153,11 @@ namespace XericUI.OverrideLayout } public Vector2 WorldAnchoredPosition(RectTransform parentContent) - => parentContent.TransformPoint(itemRectTransform.anchoredPosition); + { + // 获取对象的世界坐标位置 + var worldPos = itemRectTransform.TransformPoint(Vector3.zero); + return worldPos; + } } #region 生命周期 @@ -212,8 +216,9 @@ namespace XericUI.OverrideLayout else { Gizmos.color = Color.red; - MacroGizmosDraw.DrawGizmosRectAtPoint(data.layout.position, data.layout.size); - MacroGizmosDraw.Label(data.layout.position, $"[{data.index}] 已回收"); + var worldPos = (Vector2)content.TransformPoint(data.layout.position); + MacroGizmosDraw.DrawGizmosRectLine(new Vector2(worldPos.x, worldPos.y - data.layout.size.y) , new Vector2(worldPos.x + data.layout.size.x, worldPos.y)); + MacroGizmosDraw.Label(worldPos, $"[{data.index}] 已回收"); } } } @@ -360,7 +365,9 @@ namespace XericUI.OverrideLayout } else { - if (!CheckIsVisibal(childData.layout.position)) + // 将本地坐标转换为世界坐标进行比较 + var worldPos = content.TransformPoint(childData.layout.position); + if (CheckIsVisibal(worldPos)) childData.UnderVisible(); } } @@ -536,10 +543,14 @@ namespace XericUI.OverrideLayout private List GetChildrenLayout(int childrenCount) { // 根据约束类型设置合适的 constraintCount + // FixedColumnCount: 计算容器能容纳多少列 = 容器宽度 / (单元格宽度 + 间距) + // FixedRowCount: 计算容器能容纳多少行 = 容器高度 / (单元格高度 + 间距) int constraintCount = m_constraint switch { - GridLayoutGroup.Constraint.FixedColumnCount => Mathf.CeilToInt(m_cellSize.x / ContextSize.x), - GridLayoutGroup.Constraint.FixedRowCount => Mathf.CeilToInt(m_cellSize.y / ContextSize.y), + GridLayoutGroup.Constraint.FixedColumnCount => + Mathf.CeilToInt((ContextSize.x - m_padding.horizontal) / (m_cellSize.x + m_spacing.x)), + GridLayoutGroup.Constraint.FixedRowCount => + Mathf.CeilToInt((ContextSize.y - m_padding.vertical) / (m_cellSize.y + m_spacing.y)), _ => 0, };