0

我需要生成这样一个字段:

照片

但我不知道该怎么做。我发生什么事了:

我的结果

我的代码:

    [ContextMenu("Generate grid")]
    public void GenerateGrid()
    {
        for(int x = 0; x < _gridSize.x; x++)
        {
            for (int z = 0; z < _gridSize.z; z++)
            {
                var meshSize = _cell.GetComponent<MeshRenderer>().bounds.size;
                var position = new Vector3(x * (meshSize.x + _offset), 0, z * (meshSize.z + _offset));

                var cell = Instantiate(_cell, position, Quaternion.Euler(_rotationOffset), _parent.transform);

                cell.GridActions = GridActions;
                cell.Position = new Vector2(x, z);
                cell.name = $"Cell: x:{x}, z:{z}";

                GridActions.AllCell.Add(cell);
            }
        }
    }
4

1 回答 1

0

Simply for every odd z value, move the cell up/down by half a cell size, and move them inward toward the previous cell half a cell size. I didnt test it, but here is the code that might do that, not sure tho, again I didnt test this.

[ContextMenu("Generate grid")]
public void GenerateGrid()
{
    for(int x = 0; x < _gridSize.x; x++)
    {
        for (int z = 0; z < _gridSize.z; z++)
        {
            int xResize = 0;
            int zResize = 0;
            if (z % 2 == 1) {
                xResize = meshSize.x / 2;
                zResize = meshSize.z / 2;
            }
            var meshSize = _cell.GetComponent<MeshRenderer>().bounds.size;
            var position = new Vector3(x * (meshSize.x + _offset - xResize), 0, z * (meshSize.z + _offset - zResize));

            var cell = Instantiate(_cell, position, Quaternion.Euler(_rotationOffset), _parent.transform);

            cell.GridActions = GridActions;
            cell.Position = new Vector2(x, z);
            cell.name = $"Cell: x:{x}, z:{z}";

            GridActions.AllCell.Add(cell);
        }
    }
}
于 2021-08-21T16:44:18.927 回答