- Home /
How to create a 14 by 12 grid, depending on screen size?
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
Answer by robertbu · May 26, 2014 at 04:43 AM
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);
}
}
}
Hey, thanks for the help creating the grid. Are you sure there isn't a better way to create an even distance between each square? I don't need it to be measured in pixels, I just need a very slight break between each square.
Scaling down looks very bad, as you can see here:
http://gyazo.com/bd36d8525eadcaa0e8ea9bf4ce58b8aa
The distances are uneven, which is unacceptable.
The distances are uneven because of pixel roundoff. That is, the world size of your object might be 54.23 pixels. So as the distances are rounded to whole pixels, sometimes the lines will one pixel bigger than other times. There is no easy fix for this problem. You have some choices:
Ins$$anonymous$$d of real world object, you could use GUI.DrawTexture() or GUI.Button() (with a custom style). GUI uses pixels.
You could dynamically size your objects so that the world units of each tile maps to precise number of pixels. This is easier to do with an Orthographic camera.
You could use a pixel perfect texture to draw the grid and size the world objects to fit the texture.
None are simple.
You've already helped me so much and I don't want to bother you any more, but would you know how to adjust an orthographic camera to show the full grid horizontally (vertically, there can be space above and below)? Since I'm making this game for Android, I'm assu$$anonymous$$g I can't just set the orthographic camera in the editor and expect it to work for all different Android devices. The camera is 720 x 1080 if it matters
Thanks
Here is one link:
http://answers.unity3d.com/questions/671486/orthographic-camera-same-width-different-height.html
I wrote a better answer than this one in the last month, but a quick search did not turn it up.
Your answer

Follow this Question
Related Questions
A node in a childnode? 1 Answer
Mid point of a object 1 Answer
How to perform a task in a specified time 2 Answers
Create a factice/dummy 1 Answer
Unity not registering? 0 Answers