I am attempting to create a 14 x 12 grid with a 2 pixel space surrounding each grid “tile”. The tile is just a primitive cube GameObject. This is a 2D project.
I’ve literally been attempting to do this all day, but I simply don’t have the skill to do it. I have been attempting to use screen.width and screen.height and dividing the height by 14 and width by 12, but I cannot figure out how to place the tiles (cubes), and I have made such a mess of everything I will have to restart.
Can anyone tell me the best way to do this? Thank you for any help you can give me.
Edit: By grid, I’m referring to something like a chess board
Cubes are world objects. I find it best to place world objects on unit boundaries and adjust the camera to make things fit. Since a cube is 1x1x1, using unit boundaries will place each cube next to its neighbors. To create a bit of space, simple decrease the size of the prefab just a bit…like (0.95, 0.95, 0.95). It is difficult to precise pixel space between the objects. It involves mapping between world space and screen space, so if you can live with a world space solution, things are easier. Put this code on an empty gambe object, initialize the ‘prefab’ variable by drag and drop:
#pragma strict
var prefab : GameObject;
var rows = 12;
var cols = 14;
function Start() {
var offset = Vector3(-cols / 2.0 + 0.5, -rows / 2.0 + 0.5, 0.0 );
for (var i = 0; i < rows; i++) {
for (var j = 0; j < cols; j++) {
Instantiate(prefab, Vector3(j, i, 0.0) + offset, Quaternion.identity);
}
}
}