1
\$\begingroup\$

As an old Flash dev I'm slightly confused about how you position game objects on the screen in Unity since game objects have no width or height (only a scale).

Let's say I have a container gameobject that has several child game objects with sprite renderers representing tiles on them. If I set the container to x0, y0, the whole tile layout will extend from the screen center upwards and to the right.

What are the commonly used ways to position the game object so that it would be centered on the screen? I could calculate the width/height of my container but that would still not be very useful because the x/y coordinates work in a different scale than pixel (right?). E.g. my test screen is 640x1024 and to center my container (while running in the editor) I'd have to set x/y to -4.5. How does that make any sense?

Of course Unity's UI system would be a solution but I don't want to create the playfield with the UI system.

\$\endgroup\$
  • 1
    \$\begingroup\$ Why not just move the child game objects so that they're centered relative to the parent's origin? Then positioning the parent container at (0, 0) (or whatever your camera's x & y position might be) will center it on the screen. \$\endgroup\$ – DMGregory Jan 2 '16 at 17:57
1
\$\begingroup\$

It is not that complex as you think. May be you are confusing because you have a tiled map. So the basic solution is to keep object at center (0,0) and if it has children then keep children's position (local position, position w.r.t its parent) should also be at center (0,0).

Now in case of tiles you have to make sure that the whole tiled map should be centered to its parent. For example if you have tile's dimension 5x5 and in 1x1 tile size then Top-Left tile should be at -2,2, Top-Right tile should be at 2,2, Bottom-Left tile should be at -2,-2, Bottom-Right tile should be at 2,-2

Similarly in case of 2x2 tile size then the Top-Left tile should be at -4,4 and so on...

So if your tiles positioned like this and your parent object is at center (0,0) then I believe that no power on this earth can stop to be centered on the screen.

\$\endgroup\$
1
\$\begingroup\$

I think it's a good enough way to position the items as children of a GameObject that is centered in front of your camera. Since you have an actual camera to mess with you can easily change it to fit your needs. And also test that the different resolutions look good in the game window.

If you are using an orthographic camera you can set the size depending on your screen size and the size of your items.

Camera.main.orthograpthicSize = gridWidth * (Screen.height / Screen.width) * 0.5f;
\$\endgroup\$
1
\$\begingroup\$

Aside from the other answers here, I've got two more:

Option 1:

  • Move the camera. After constructing your grid you can relocate the camera so that its position is the same as the position of the center-most tiles. Of course, this might be a problem if a user's screensize is smaller than your board...

Option 2:

  • Use the New UI system, all of which operates under a RectTransform hierarchy which does have a width and height and is inherently based in Screen Space. The pivot and anchor values allow you to even set things up to be flexible and arrange based on how wide and tall the screen is. Admittedly you asked for Non-UI solutions, but for a 2D game, this might be fine for someone else (I'm doing it!).
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.