Unity size system

Hello,

I’m pretty new to Unity and I would like to know how can I fit something perfectly on a screen? I’ve googled a lot and have found different methods on the Camera class and the Screen.width and Screen.height properties, but the units differ quite a bit.

What I want to do is have a simple tile grid which can be displayed on any device correctly which means it should change based on screen size or the camera. That is the part that is bothering me.

I know how to mathematically get the correct calculations for distributing stuff, but Unity doesn’t use sizes on game objects, but it has a scale. Also, there is a big difference in units. By getting the screen size, I get the size in pixels. Those don’t mean a lot for placing my objects on the screen. If I get the coordinates from the camera viewport, I know how to position items into a grid, but I don’t know how to make objects (sprites in my case) get enlarged to fit into the desired grid size because I can’t set the object’s width and height. Instead, they have the scale which I don’t know how to manipulate to fit my needs.

I am guessing there is a simple solution to my problem, but I just got stuck and can’t seem to find an answer.

I would greatly appreciate any help!

Sounds like you’re using GameObjects in the world and not a Canvas, right?

What type of projection are you using? Orthogonal will make things easier, since with a Perspective projection objects closer to the camera will take more space.

For Orthogonal, the “size” value determines how many vertical world units fit into the camera. The horizontal size is based on the size value and the current aspect ratio. If you know the size of an object you can scale them to fit in that area.

For Perspective, you first need to find or define a distance from the camera, since the area that you need to fill will be smaller if you place things closer. You can get the min and max values of each side of the screen with ScreenToWorldPoint .

With that you can adapt the grid to fit any screen by adding or removing rows or columns, and keep a kind of consistent aspect ratio of things.

If you just want to have the same portion of the world drawn in any screen and stretching is not a problem, you can just set a fixed aspect ratio in code and keep the Viewport to cover the whole screen. Unity will do the stretching/shrinking for you.