Skip to content

Commit 36d0c7c

Browse files
committed
Sort object by hierarchy
1 parent 18cf354 commit 36d0c7c

File tree

1 file changed

+60
-13
lines changed

1 file changed

+60
-13
lines changed

Editor/AlignTools.cs

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,7 @@ private static void DistributionUI(CalcValueTwo calcValue, ApplyValue applyValue
165165
v = calcValue(corners, 0 == i, ref minV, ref maxV)
166166
});
167167
};
168-
vlist.Sort((a, b) =>
169-
{
170-
if (a.v < b.v) return -1;
171-
else if (a.v > b.v) return 1;
172-
return 0;
173-
});
168+
vlist.Sort(SortByHierarchy);
174169

175170
float gap = (maxV - minV) / (list.Count - 1);
176171
for (var i = 1; i < vlist.Count - 1; i++)
@@ -207,16 +202,21 @@ private static void DistributionGapUI(CalcSize calcSize, ApplyValue applyValue)
207202
size = size,
208203
});
209204
};
210-
vlist.Sort((a, b) =>
205+
Debug.Log("============= befor sort");
206+
foreach (var v in vlist)
207+
{
208+
Debug.Log(v.rt.name);
209+
}
210+
vlist.Sort(SortByHierarchy);
211+
Debug.Log("------------ end sort");
212+
foreach (var v in vlist)
211213
{
212-
if (a.v < b.v) return -1;
213-
else if (a.v > b.v) return 1;
214-
return 0;
215-
});
214+
Debug.Log(v.rt.name);
215+
}
216216

217217
float gap = (maxV - minV - sumSize) / (list.Count - 1);
218-
float curV = minV + vlist[0].size + gap;
219-
for (var i = 1; i < vlist.Count - 1; i++)
218+
float curV = minV;
219+
for (var i = 0; i < vlist.Count; i++)
220220
{
221221
var rt = vlist[i].rt;
222222
var pos = applyValue(rt, curV + vlist[i].size / 2);
@@ -454,6 +454,53 @@ private static Vector3 ApplyValueSizeV(RectTransform rt, float v)
454454
}
455455
#endregion
456456

457+
458+
private static int SortByPosition(Value a, Value b)
459+
{
460+
if (Mathf.Approximately(a.v, b.v)) return 0;
461+
if (a.v < b.v) return -1;
462+
else if (a.v > b.v) return 1;
463+
return 0;
464+
}
465+
466+
private static Dictionary<Transform, int> s_Sets = new Dictionary<Transform, int>();
467+
private static int SortByHierarchy(Value a, Value b)
468+
{
469+
// 是否兄弟节点
470+
if (a.rt.parent == b.rt.parent)
471+
return b.rt.GetSiblingIndex() - a.rt.GetSiblingIndex();
472+
473+
// 是否非跟节点
474+
var rootA = a.rt.root;
475+
var rootB = b.rt.root;
476+
if (rootA != rootB)
477+
return rootB.GetSiblingIndex() - rootA.GetSiblingIndex();
478+
479+
s_Sets.Clear();
480+
int siblingIndx = -1;
481+
Transform transA = a.rt;
482+
do
483+
{
484+
s_Sets.Add(transA, siblingIndx);
485+
siblingIndx = transA.GetSiblingIndex();
486+
transA = transA.parent;
487+
} while (transA != null);
488+
489+
490+
Transform transB = b.rt;
491+
while (transB != null)
492+
{
493+
if (s_Sets.TryGetValue(transB.parent, out siblingIndx))
494+
{
495+
s_Sets.Clear();
496+
return transB.GetSiblingIndex() - siblingIndx;
497+
}
498+
transB = transB.parent;
499+
}
500+
s_Sets.Clear();
501+
return 0;
502+
}
503+
457504
}
458505
}
459506

0 commit comments

Comments
 (0)