private void AnchotObjectToTable(GameObject obj, int row) { obj.transform.localScale = Vector3.one; UIWidget widget = obj.GetComponent <UIWidget>(); widget.leftAnchor.target = table; widget.rightAnchor.target = table; widget.bottomAnchor.target = table; widget.topAnchor.target = table; widget.leftAnchor.relative = 0; widget.leftAnchor.absolute = 0; widget.rightAnchor.relative = 1; widget.rightAnchor.absolute = 0; widget.bottomAnchor.relative = 1; widget.bottomAnchor.absolute = (row + 1) * -height; widget.topAnchor.relative = 1; widget.topAnchor.absolute = row * -height; widget.Update(); }
/// <summary> /// Positions the grid items, taking their own size into consideration. /// </summary> /// protected void RepositionVariableSizeNew(List <Transform> children) { float xOffset = 0; float yOffset = 0; int cols = columns > 0 ? children.Count / columns + 1 : 1; int rows = columns > 0 ? columns : children.Count; Bounds[,] bounds = new Bounds[cols, rows]; Bounds[] boundsRows = new Bounds[rows]; Bounds[] boundsCols = new Bounds[cols]; int x = 0; int y = 0; 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; } } x = 0; y = 0; for (int i = 0, imax = children.Count; i < imax; ++i) { Transform t = children[i]; RectOffset margins = this.GetMargins(t, (i == 0), (i == (imax - 1))); 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 += b.min.x - br.min.x + padding.x + margins.left; if (direction == Direction.Down) { pos.y = -yOffset - b.extents.y - b.center.y; pos.y += (b.max.y - b.min.y - bc.max.y + bc.min.y) * 0.5f - padding.y - margins.top; } else { pos.y = yOffset + (b.extents.y - b.center.y); pos.y -= (b.max.y - b.min.y - bc.max.y + bc.min.y) * 0.5f - padding.y - margins.top; } xOffset += br.max.x - br.min.x + padding.x * 2f + (margins.left + margins.right); t.localPosition = pos; if (++x >= columns && columns > 0) { x = 0; ++y; xOffset = 0f; yOffset += bc.size.y + padding.y * 2f + (margins.top + margins.bottom); } // In case we're at the last column // we need to increase the y offset by a row for the sizing process if (i == (imax - 1) && x > 0 && x < columns) { yOffset += bc.size.y + padding.y * 2f + (margins.top + margins.bottom); } } // Update the container's height if (this.resizeContainer) { UIWidget container = this.GetComponent <UIWidget>(); if (container != null) { container.height = Mathf.RoundToInt(yOffset); container.Update(); } } }