新Head
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
@@ -9,21 +8,64 @@ namespace Ichni.Editor
|
||||
{
|
||||
Inspector Inspector => EditorManager.instance.uiManager.inspector;
|
||||
public DynamicUIContainer parentContainer;
|
||||
public GridLayoutGroup gridLayoutGroup;
|
||||
public RectTransform rect;
|
||||
public List<DynamicUIElement> dynamicUIElements;
|
||||
|
||||
/// <summary> 当前行高 </summary>
|
||||
[HideInInspector]
|
||||
public float rowHeight = 100f;
|
||||
|
||||
/// <summary>
|
||||
/// 使用 GridLayoutGroup 实现 N 列自动换行布局。
|
||||
/// 当添加的元素数量超过 elementCountPerRow 时,会自动换行到下一行。
|
||||
/// </summary>
|
||||
public void Initialize(DynamicUIContainer parentContainer, int elementCountPerRow, float height = 100)
|
||||
{
|
||||
this.parentContainer = parentContainer;
|
||||
this.gridLayoutGroup.cellSize = new Vector2(600f / elementCountPerRow, height);
|
||||
// 支持对象池复用:避免每次 new List 产生 GC,直接清空
|
||||
if (this.dynamicUIElements == null)
|
||||
this.dynamicUIElements = new List<DynamicUIElement>();
|
||||
this.rowHeight = height;
|
||||
|
||||
int columns = Mathf.Max(1, elementCountPerRow);
|
||||
|
||||
// ── 启用 GridLayoutGroup ──
|
||||
var grid = GetComponent<GridLayoutGroup>();
|
||||
if (grid == null)
|
||||
grid = gameObject.AddComponent<GridLayoutGroup>();
|
||||
|
||||
grid.enabled = true;
|
||||
grid.constraint = GridLayoutGroup.Constraint.FixedColumnCount;
|
||||
grid.constraintCount = columns;
|
||||
grid.childAlignment = TextAnchor.UpperLeft;
|
||||
grid.startCorner = GridLayoutGroup.Corner.UpperLeft;
|
||||
grid.startAxis = GridLayoutGroup.Axis.Horizontal;
|
||||
grid.padding = new RectOffset(10, 10, 0, 0);
|
||||
|
||||
float spacing = 10f;
|
||||
float totalSpacing = (columns - 1) * spacing;
|
||||
float cellWidth = (LayoutPacker.TotalRowWidth - totalSpacing) / columns;
|
||||
grid.cellSize = new Vector2(cellWidth, height);
|
||||
grid.spacing = new Vector2(spacing, spacing);
|
||||
|
||||
// ── LayoutElement: preferredHeight = -1 让 GridLayoutGroup 的 ILayoutElement 接管高度 ──
|
||||
var layoutElement = GetComponent<LayoutElement>();
|
||||
if (layoutElement == null)
|
||||
layoutElement = gameObject.AddComponent<LayoutElement>();
|
||||
layoutElement.preferredHeight = -1;
|
||||
layoutElement.flexibleHeight = -1;
|
||||
|
||||
// ── ContentSizeFitter: 让子容器高度随网格行数动态增长 ──
|
||||
var fitter = GetComponent<ContentSizeFitter>();
|
||||
if (fitter == null)
|
||||
fitter = gameObject.AddComponent<ContentSizeFitter>();
|
||||
fitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize;
|
||||
fitter.horizontalFit = ContentSizeFitter.FitMode.Unconstrained;
|
||||
fitter.enabled = true;
|
||||
|
||||
if (dynamicUIElements == null)
|
||||
dynamicUIElements = new List<DynamicUIElement>();
|
||||
else
|
||||
this.dynamicUIElements.Clear();
|
||||
dynamicUIElements.Clear();
|
||||
}
|
||||
|
||||
|
||||
public DynamicUISubcontainer Mark(string mark, IHaveInspection inspection = null)
|
||||
{
|
||||
inspection ??= Inspector;
|
||||
@@ -31,4 +73,4 @@ namespace Ichni.Editor
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user