unity中使用嵌套for循环 生成 奇数偶数列居中展示物体
public class test_hexagonal : MonoBehaviour
{
public int width = 10;
public int height = 5;
public Grid grid;
public Transform parent;
public List<List<Grid>> grids;
// Start is called before the first frame update
void Start()
{
Init();
}
private void Init()
{
grids = new List<List<Grid>>();
int oneWidth = 6;
for (int j = 0; j < height; j++)
{
var list = new List<Grid>();
for (int i = 0; i < width; i++)
{
Grid grid = Instantiate(this.grid);
grid.Init();
grid.SetParent(parent);
float posX = i * oneWidth - (width - 1) / 2.0f * oneWidth;
float posY = j * oneWidth - (height - 1) / 2.0f * oneWidth;
grid.SetPosition(posX, posY);
list.Add(grid);
}
grids.Add(list);
}
}
// Update is called once per frame
void Update()
{
}
}
}