- Home /
Instantiating grid of objects starting at the bottom left of the viewport
I need to instantiate prefabs(Tiles) in rows and columns like a grid. I used a multidimensional array with 3 elements in two dimensions and, using nested for loops, I instantiated 1 prefab at each "coordinate" in world space like:
0,0 0,1 0,2
1,0 1,1 1,2
2,0 2,1 2,2
Of course this means my grid of objects starts at 0,0 in world space and moves up to the right, but I need them to start at the bottom left corner of the screen. I knew the solution had something to do with converting the index of each dimension of the array to view port space, so I did that and got them instantiating from the bottom left, but they're no longer adjacent, which I expected. Which probably means some maths need to be done but IDK.
If you can tell me what math to do that would be great, or if you have another solution...
I don't know if it's relevant, but each sprite used for the prefabs I'm instantiating is 256x256 pixels and they're using 256 pixels per unit so that one sprite is 1x1 unit(I won't be using any physics in this project).
Attached is a picture of what I'm getting, versus a Photoshop of what I want.
for (int i = 0; i < grid.GetLength(0); i++)
{
for (int j = 0; j < grid.GetLength(1); j++)
{
grid[i, j] = Instantiate(tile, Camera.main.ViewportToWorldPoint(new Vector3(i, j, 0)), Quaternion.identity);
}
}
Answer by Bunny83 · Jan 26, 2017 at 02:13 PM
Well, if you want to lay them out on the screen you don't want to use the Viewport space. Viewport coordinates goes from 0 to 1. So 0,0 is the bottom left corner and 1,1, is the top right corner of the screen. Those are also square-coordinates while the screen itself is usually a rectangle so even when calculating the right scale you have to deal with the aspect ratio as well.
If each tile is one world unit wide, just place them in worldspace. You can use ViewportToWorldPoint to determine the "first point". Something like that:
Vector3 start = Camera.main.ViewportToWorldPoint(new Vector3(0f, 0f, 0f)) + new Vector3(0.5f, 0.5f, 0f);
for (int i = 0; i < grid.GetLength(0); i++)
{
for (int j = 0; j < grid.GetLength(1); j++)
{
grid[i, j] = Instantiate(tile, start + new Vector3(i, j, 0f), Quaternion.identity);
}
}
I added "0.5" on the x and y axis since it looks like your prefabs origin is in the center. Since you said a tile is 1 unit wide moving it 0.5 will place it right in the lower left corner. Now you just add "whole units" to each axis (since integers are whole numbers it should work out just right). If for some reason your tiles are larger or smaller you just have to multiply "i" and "j" with your scaling factor. If you want a little space between the tiles you can do
+ new Vector3(i*(1f + 0.1f), j*(1f + 0.1f), 0f),
Here the "1f" would be the scale / worldsize of one tile and the "0.1f" would be the space between those tiles
That worked beautifully, and I appreciate that 0.5f offset too. I was going to add each tile as a child to an empty game object to fake an origin, but your way is cleaner.