修复气泡布局存在的一些问题
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
@@ -24,89 +23,223 @@ namespace XericUI.BubbleLayout
|
||||
|
||||
/// <summary>
|
||||
/// 竖排布局约束插件 —— 将气泡元素沿 Y 轴排列,并通过弹簧力吸附到目标位置。
|
||||
/// 支持多布局框穿插排列和拉链模式(一左一右交替)。
|
||||
/// </summary>
|
||||
[AddComponentMenu("Xeric UI Vessel/Layout/Vertical Layout Constraint", 53)]
|
||||
public class VerticalLayoutConstraint : BubbleLayoutPluginBase, IBubbleConstraintPlugin
|
||||
public class VerticalLayoutConstraint : LinearLayoutConstraintBase
|
||||
{
|
||||
[Header("吸附力")]
|
||||
[Tooltip("弹簧吸附力倍率,值越大吸附越快")]
|
||||
[SerializeField] private float m_AttractionForce = 50f;
|
||||
|
||||
[Header("排列")]
|
||||
[Tooltip("排序模式")]
|
||||
[SerializeField] private LayoutSortMode m_SortMode = LayoutSortMode.HierarchyOrder;
|
||||
|
||||
[Tooltip("元素间距")]
|
||||
[SerializeField] private float m_Spacing = 10f;
|
||||
|
||||
[Tooltip("起始偏移(相对于布局中心上方)")]
|
||||
[Header("竖排特有")]
|
||||
[Tooltip("起始竖直偏移(相对布局框区域中心)")]
|
||||
[SerializeField] private float m_StartOffsetY = 0f;
|
||||
|
||||
[Header("拉链模式")]
|
||||
[Tooltip("启用拉链排列(偶数索引偏左,奇数索引偏右)")]
|
||||
[Tooltip("启用拉链排列(偶数索引偏左,奇数索引偏右,仅对默认单框布局有效)")]
|
||||
[SerializeField] private bool m_ZipperMode = false;
|
||||
|
||||
[Tooltip("拉链模式下左右间距")]
|
||||
[SerializeField] private float m_ZipperGap = 20f;
|
||||
|
||||
public void ProcessConstraint(List<BubbleItemData> items, Vector2 layoutCenter)
|
||||
#region LinearLayoutConstraintBase 实现
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override string PrimaryAxisName => "Y";
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override bool IsPrimaryHorizontal => false;
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override float GetPrimarySize(BubbleItemData item) => item.size.y;
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override float GetPrimarySizeFromRT(RectTransform rt) => rt.rect.height;
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override Vector2 MakeTargetPosition(float primary, float secondary)
|
||||
{
|
||||
return new Vector2(secondary, primary);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override float GetSecondaryAlignOffset(BubbleItemData item, LayoutBox box)
|
||||
{
|
||||
// 拉链偏移由外部交叉索引决定,这里返回 0
|
||||
return 0f;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 重写 ProcessConstraint 以支持拉链模式
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void ProcessConstraint(List<BubbleItemData> items, Vector2 layoutCenter)
|
||||
{
|
||||
if (items == null || items.Count == 0)
|
||||
return;
|
||||
|
||||
// 排序
|
||||
var sorted = new List<BubbleItemData>(items);
|
||||
SortItems(sorted);
|
||||
EnsureAtLeastOneBox();
|
||||
EnsureAssignmentsInitialized();
|
||||
|
||||
// 计算起始 Y
|
||||
float totalHeight = 0f;
|
||||
for (int i = 0; i < sorted.Count; i++)
|
||||
totalHeight += sorted[i].size.y + m_Spacing;
|
||||
totalHeight -= m_Spacing; // 去掉最后一个间距
|
||||
// 分组
|
||||
var boxGroups = new List<BubbleItemData>[m_LayoutBoxes.Count];
|
||||
for (int b = 0; b < m_LayoutBoxes.Count; b++)
|
||||
boxGroups[b] = new List<BubbleItemData>();
|
||||
|
||||
float startY = layoutCenter.y + m_StartOffsetY + totalHeight * 0.5f;
|
||||
|
||||
// 逐元素设置目标位置并施加弹簧力
|
||||
float currentY = startY;
|
||||
for (int i = 0; i < sorted.Count; i++)
|
||||
for (int i = 0; i < items.Count; i++)
|
||||
{
|
||||
var item = sorted[i];
|
||||
float targetX = layoutCenter.x;
|
||||
float targetY = currentY;
|
||||
|
||||
// 拉链模式:交替左右偏移
|
||||
if (m_ZipperMode)
|
||||
var item = items[i];
|
||||
int boxIdx = 0;
|
||||
if (item.rectTransform != null)
|
||||
{
|
||||
targetX = layoutCenter.x + (i % 2 == 0 ? -m_ZipperGap : m_ZipperGap) * 0.5f;
|
||||
string key = $"{item.rectTransform.name}_{item.rectTransform.GetInstanceID()}";
|
||||
if (m_BoxAssignments.TryGetValue(key, out int assigned))
|
||||
boxIdx = Mathf.Clamp(assigned, 0, m_LayoutBoxes.Count - 1);
|
||||
}
|
||||
|
||||
Vector2 targetPos = new Vector2(targetX, targetY);
|
||||
|
||||
// 弹簧力:F = (target - current) * forceMultiplier
|
||||
Vector2 springForce = (targetPos - item.position) * m_AttractionForce;
|
||||
item.force += springForce;
|
||||
|
||||
currentY -= item.size.y + m_Spacing;
|
||||
boxGroups[boxIdx].Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
private void SortItems(List<BubbleItemData> items)
|
||||
{
|
||||
switch (m_SortMode)
|
||||
// 排序各组
|
||||
for (int b = 0; b < boxGroups.Length; b++)
|
||||
SortItems(boxGroups[b]);
|
||||
|
||||
// 计算各组总主轴尺寸及起始位置
|
||||
float[] boxTotalPrimary = new float[boxGroups.Length];
|
||||
float[] boxAdvance = new float[boxGroups.Length];
|
||||
for (int b = 0; b < boxGroups.Length; b++)
|
||||
{
|
||||
case LayoutSortMode.HierarchyOrder:
|
||||
// 保持原始顺序(即 rectChildren 的顺序)
|
||||
break;
|
||||
case LayoutSortMode.PositionX:
|
||||
items.Sort((a, b) => a.position.x.CompareTo(b.position.x));
|
||||
break;
|
||||
case LayoutSortMode.PositionY:
|
||||
items.Sort((a, b) => b.position.y.CompareTo(a.position.y)); // Y 从大到小(上到下)
|
||||
break;
|
||||
case LayoutSortMode.Size:
|
||||
items.Sort((a, b) => (b.size.x * b.size.y).CompareTo(a.size.x * a.size.y));
|
||||
break;
|
||||
float total = 0f;
|
||||
for (int i = 0; i < boxGroups[b].Count; i++)
|
||||
total += boxGroups[b][i].size.y + m_Spacing;
|
||||
if (boxGroups[b].Count > 0) total -= m_Spacing;
|
||||
boxTotalPrimary[b] = total;
|
||||
boxAdvance[b] = m_LayoutBoxes[b].Region.center.y + m_StartOffsetY + total * 0.5f;
|
||||
}
|
||||
|
||||
// 穿插迭代
|
||||
int[] boxIndices = new int[boxGroups.Length];
|
||||
int globalIndex = 0;
|
||||
bool anyRemaining = true;
|
||||
var targetList = new List<(BubbleItemData item, int boxIndex, Vector2 targetPos)>();
|
||||
|
||||
while (anyRemaining)
|
||||
{
|
||||
anyRemaining = false;
|
||||
for (int b = 0; b < boxGroups.Length; b++)
|
||||
{
|
||||
if (boxIndices[b] >= boxGroups[b].Count)
|
||||
continue;
|
||||
|
||||
anyRemaining = true;
|
||||
var item = boxGroups[b][boxIndices[b]];
|
||||
var box = m_LayoutBoxes[b];
|
||||
|
||||
// 辅轴位置:拉链模式下的 X 偏移
|
||||
float secondaryX = box.Region.center.x;
|
||||
if (m_ZipperMode)
|
||||
{
|
||||
secondaryX += (globalIndex % 2 == 0 ? -m_ZipperGap : m_ZipperGap) * 0.5f;
|
||||
}
|
||||
|
||||
// 主轴位置推进:从上往下(Y 减小)
|
||||
float targetY = boxAdvance[b];
|
||||
Vector2 targetPos = new Vector2(secondaryX, targetY);
|
||||
targetPos = ClampToBox(targetPos, item, box);
|
||||
|
||||
targetList.Add((item, b, targetPos));
|
||||
|
||||
boxAdvance[b] -= item.size.y + m_Spacing;
|
||||
boxIndices[b]++;
|
||||
globalIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
// 施加力 / 瞬移
|
||||
for (int i = 0; i < targetList.Count; i++)
|
||||
{
|
||||
var (item, boxIndex, targetPos) = targetList[i];
|
||||
var box = m_LayoutBoxes[boxIndex];
|
||||
|
||||
if (IsOutOfBox(item.position, item, box))
|
||||
{
|
||||
item.position = targetPos;
|
||||
item.velocity = Vector2.zero;
|
||||
}
|
||||
else
|
||||
{
|
||||
Vector2 springForce = (targetPos - item.position) * m_AttractionForce;
|
||||
item.force += springForce;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Editor Gizmos
|
||||
|
||||
#if UNITY_EDITOR
|
||||
/// <inheritdoc/>
|
||||
protected override void DrawItemTargets(List<GizmoSlot> allSlots, Vector2 layoutCenter)
|
||||
{
|
||||
var boxSlots = new List<GizmoSlot>[m_LayoutBoxes.Count];
|
||||
for (int b = 0; b < m_LayoutBoxes.Count; b++)
|
||||
boxSlots[b] = new List<GizmoSlot>();
|
||||
|
||||
for (int i = 0; i < allSlots.Count; i++)
|
||||
{
|
||||
var slot = allSlots[i];
|
||||
int bIdx = Mathf.Clamp(slot.boxIndex, 0, m_LayoutBoxes.Count - 1);
|
||||
boxSlots[bIdx].Add(slot);
|
||||
}
|
||||
|
||||
int[] indices = new int[boxSlots.Length];
|
||||
float[] advances = new float[boxSlots.Length];
|
||||
for (int b = 0; b < boxSlots.Length; b++)
|
||||
{
|
||||
float total = 0f;
|
||||
for (int i = 0; i < boxSlots[b].Count; i++)
|
||||
total += boxSlots[b][i].size.y + m_Spacing;
|
||||
if (boxSlots[b].Count > 0) total -= m_Spacing;
|
||||
advances[b] = m_LayoutBoxes[b].Region.center.y + m_StartOffsetY + total * 0.5f;
|
||||
}
|
||||
|
||||
Color fillColor = new Color(0.1f, 0.7f, 1f, 0.15f);
|
||||
Color wireColor = new Color(0f, 0.6f, 1f, 0.7f);
|
||||
|
||||
int globalIndex = 0;
|
||||
bool anyRemaining = true;
|
||||
while (anyRemaining)
|
||||
{
|
||||
anyRemaining = false;
|
||||
for (int b = 0; b < boxSlots.Length; b++)
|
||||
{
|
||||
if (indices[b] >= boxSlots[b].Count)
|
||||
continue;
|
||||
|
||||
anyRemaining = true;
|
||||
var slot = boxSlots[b][indices[b]];
|
||||
var box = m_LayoutBoxes[b];
|
||||
|
||||
float targetX = box.Region.center.x;
|
||||
if (m_ZipperMode)
|
||||
targetX += (globalIndex % 2 == 0 ? -m_ZipperGap : m_ZipperGap) * 0.5f;
|
||||
|
||||
float targetY = advances[b];
|
||||
|
||||
Rect localRect = new Rect(
|
||||
targetX - slot.size.x * 0.5f,
|
||||
targetY - slot.size.y * 0.5f,
|
||||
slot.size.x,
|
||||
slot.size.y);
|
||||
|
||||
DrawLocalRectAsWireCube(localRect, wireColor, fillColor);
|
||||
|
||||
advances[b] -= slot.size.y + m_Spacing;
|
||||
indices[b]++;
|
||||
globalIndex++;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user