93 lines
2.5 KiB
C#
93 lines
2.5 KiB
C#
|
|
using UnityEngine;
|
|||
|
|
using UnityEngine.UI;
|
|||
|
|
|
|||
|
|
[ExecuteAlways]
|
|||
|
|
public class CustomWrapGrid : LayoutGroup
|
|||
|
|
{
|
|||
|
|
public float spacingX = 0f;
|
|||
|
|
public float spacingY = 0f;
|
|||
|
|
|
|||
|
|
private float _containerWidth;
|
|||
|
|
private Vector2 _currentPos;
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>ܸ߶<DCB8>
|
|||
|
|
private float _totalHeight;
|
|||
|
|
protected override void OnEnable()
|
|||
|
|
{
|
|||
|
|
base.OnEnable();
|
|||
|
|
CalculateLayout();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public override void SetLayoutHorizontal()
|
|||
|
|
{
|
|||
|
|
CalculateLayout();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public override void SetLayoutVertical()
|
|||
|
|
{
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA>ʵ<EFBFBD>֣<EFBFBD><D6A3><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><DFBC><EFBFBD>SetLayoutHorizontal<61><6C><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public override void CalculateLayoutInputVertical()
|
|||
|
|
{
|
|||
|
|
CalculateLayout();
|
|||
|
|
float minHeight = _totalHeight;
|
|||
|
|
SetLayoutInputForAxis(minHeight, minHeight, 0, 1); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void CalculateLayout()
|
|||
|
|
{
|
|||
|
|
_totalHeight = 0f; // <20><><EFBFBD><EFBFBD><EFBFBD>ܸ߶<DCB8>
|
|||
|
|
if (rectChildren.Count == 0) return;
|
|||
|
|
|
|||
|
|
_containerWidth = rectTransform.rect.width - padding.horizontal;
|
|||
|
|
_currentPos = new Vector2(padding.left, -padding.top);
|
|||
|
|
float rowHeight = 0f;
|
|||
|
|
foreach (RectTransform child in rectChildren)
|
|||
|
|
{
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>صĶ<D8B5><C4B6><EFBFBD>
|
|||
|
|
if (!child.gameObject.activeSelf) continue;
|
|||
|
|
|
|||
|
|
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ߴ<EFBFBD>
|
|||
|
|
float childWidth = LayoutUtility.GetPreferredWidth(child);
|
|||
|
|
float childHeight = LayoutUtility.GetPreferredHeight(child);
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
|
|||
|
|
if (_currentPos.x + childWidth > _containerWidth - padding.right)
|
|||
|
|
{
|
|||
|
|
StartNewRow(ref _currentPos, ref rowHeight);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
|
|||
|
|
SetChildAlongAxis(child, 0, _currentPos.x, childWidth);
|
|||
|
|
SetChildAlongAxis(child, 1, _currentPos.y, childHeight);
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD>µ<EFBFBD>ǰ<EFBFBD><C7B0>״̬
|
|||
|
|
_currentPos.x += childWidth + spacingX;
|
|||
|
|
rowHeight = Mathf.Max(rowHeight, childHeight);
|
|||
|
|
}
|
|||
|
|
// <20><><EFBFBD>ռ<EFBFBD><D5BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>и߶<D0B8>
|
|||
|
|
_totalHeight = Mathf.Abs(_currentPos.y) + rowHeight - padding.top;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void StartNewRow(ref Vector2 pos, ref float rowHeight)
|
|||
|
|
{
|
|||
|
|
pos.x = padding.left;
|
|||
|
|
pos.y += rowHeight + spacingY;
|
|||
|
|
rowHeight = 0f;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>仯ʱ<E4BBAF>Զ<EFBFBD>ˢ<EFBFBD><CBA2>
|
|||
|
|
protected override void OnRectTransformDimensionsChange()
|
|||
|
|
{
|
|||
|
|
base.OnRectTransformDimensionsChange();
|
|||
|
|
CalculateLayout();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#if UNITY_EDITOR
|
|||
|
|
protected override void OnValidate()
|
|||
|
|
{
|
|||
|
|
base.OnValidate();
|
|||
|
|
CalculateLayout();
|
|||
|
|
}
|
|||
|
|
#endif
|
|||
|
|
}
|