public List<Transform> GetChildList () { Transform myTrans = transform; List<Transform> list = new List<Transform>(); for (int i = 0; i < myTrans.childCount; ++i) { Transform t = myTrans.GetChild(i); if (!hideInactive || (t && t.gameObject.activeSelf)) { if (!UIDragDropItem.IsDragged(t.gameObject)) list.Add(t); } } // Sort the list using the desired sorting logic if (sorting != Sorting.None && arrangement != Arrangement.CellSnap) { if (sorting == Sorting.Alphabetic) list.Sort(SortByName); else if (sorting == Sorting.Horizontal) list.Sort(SortHorizontal); else if (sorting == Sorting.Vertical) list.Sort(SortVertical); else if (onCustomSort != null) list.Sort(onCustomSort); else Sort(list); } return list; } static public int SortByName (Transform a, Transform b) { return string.Compare(a.name, b.name); } static public int SortHorizontal (Transform a, Transform b) { return a.localPosition.x.CompareTo(b.localPosition.x); } static public int SortVertical (Transform a, Transform b) { return b.localPosition.y.CompareTo(a.localPosition.y); }
int x = 0; int y = 0; int maxX = 0; int maxY = 0; // Re-add the children in the same order we have them in and position them accordingly for (int i = 0, imax = list.Count; i < imax; ++i) { Transform t = list[i]; Vector3 pos = t.localPosition; float depth = pos.z; if (arrangement == Arrangement.CellSnap) { if (cellWidth > 0) pos.x = Mathf.Round(pos.x / cellWidth) * cellWidth; if (cellHeight > 0) pos.y = Mathf.Round(pos.y / cellHeight) * cellHeight; } else pos = (arrangement == Arrangement.Horizontal) ? new Vector3(cellWidth * x, -cellHeight * y, depth) : //横向排序,x轴递增cellWidth new Vector3(cellWidth * y, -cellHeight * x, depth); //纵向排序,y轴递减cellHeight if (animateSmoothly && Application.isPlaying && (pivot != UIWidget.Pivot.TopLeft || Vector3.SqrMagnitude(t.localPosition - pos) >= 0.0001f)) { var sp = SpringPosition.Begin(t.gameObject, pos, 15f); sp.updateScrollView = true; sp.ignoreTimeScale = true; } else t.localPosition = pos; maxX = Mathf.Max(maxX, x);//最大列数 maxY = Mathf.Max(maxY, y);//最大行数 if (++x >= maxPerLine && maxPerLine > 0) //换行 { x = 0; ++y; } }
if (pivot != UIWidget.Pivot.TopLeft) { var po = NGUIMath.GetPivotOffset(pivot); float fx, fy; if (arrangement == Arrangement.Horizontal) { fx = Mathf.Lerp(0f, maxX * cellWidth, po.x); fy = Mathf.Lerp(-maxY * cellHeight, 0f, po.y); } else { fx = Mathf.Lerp(0f, maxY * cellWidth, po.x); fy = Mathf.Lerp(-maxX * cellHeight, 0f, po.y); } foreach (var t in list) { Vector3 pos = t.localPosition; pos.x -= fx; pos.y -= fy; t.localPosition = pos; } }
for (int i = 0, imax = children.Count; i < imax; ++i) { Transform t = children[i]; Bounds b = NGUIMath.CalculateRelativeWidgetBounds(t, !hideInactive); Vector3 scale = t.localScale; b.min = Vector3.Scale(b.min, scale); b.max = Vector3.Scale(b.max, scale); bounds[y, x] = b; boundsRows[x].Encapsulate(b); boundsCols[y].Encapsulate(b); if (++x >= columns && columns > 0) { x = 0; ++y; } }
Vector2 po = NGUIMath.GetPivotOffset(cellAlignment); for (int i = 0, imax = children.Count; i < imax; ++i) { Transform t = children[i]; Bounds b = bounds[y, x]; Bounds br = boundsRows[x]; Bounds bc = boundsCols[y]; Vector3 pos = t.localPosition; pos.x = xOffset + b.extents.x - b.center.x; pos.x -= Mathf.Lerp(0f, b.max.x - b.min.x - br.max.x + br.min.x, po.x) - padding.x; //pos.x = xOffset + b.extents.x;//x现在在左边界 //float maxDis = (br.max.x - br.min.x) - (b.max.x - b.min.x); //loat offsetX = Mathf.Lerp(0f, maxDis, po.x) ;//计算最终要偏移的距离 //pos.x += offsetX + padding.x; if (direction == Direction.Down) { pos.y = -yOffset - b.extents.y - b.center.y; pos.y += Mathf.Lerp(b.max.y - b.min.y - bc.max.y + bc.min.y, 0f, po.y) - padding.y; } else { pos.y = yOffset + b.extents.y - b.center.y; pos.y -= Mathf.Lerp(0f, b.max.y - b.min.y - bc.max.y + bc.min.y, po.y) - padding.y; } xOffset += br.size.x + padding.x * 2f; t.localPosition = pos; if (++x >= columns && columns > 0) { x = 0; ++y; xOffset = 0f; yOffset += bc.size.y + padding.y * 2f; } }
原文:https://www.cnblogs.com/wang-jin-fu/p/13509107.html