public void GenerateCells() { ClearCells(); cells = new Cell[Layout.VisibleCellCount()]; int rows = Layout.Rows; int cols = Layout.Cols; Vector3 offset = new Vector3(-(cols - 1) * 0.5f * cellSize, (rows - 1) * 0.5f * cellSize, 0.0f); for (int i = 0; i < cols; i++) { for (int j = 0; j < rows; j++) { if (Layout.GetState(i, j) != CellState.Empty) { Cell newCell = Instantiate(cellTemplate, transform); newCell.transform.localPosition = new Vector3(i * cellSize, -j * cellSize, 0.0f) + offset; newCell.state = Layout.GetState(i, j); newCell.SetImage(Layout.GetSprite(i, j)); newCell.x = i; newCell.y = j; cells[GridUtils.BtoLIndex(i, j, Cols)] = newCell; } } } RectTransform rect = GetComponent <RectTransform>(); rect.sizeDelta = new Vector2(cols * cellSize, rows * cellSize); }