2
\$\begingroup\$

E.g I have a bunch of objects which are generated in specific coordinates. Let's say in a range of - 5 to + 5 on X axis. I came app with these numbers because of camera size I have in Unity editor.

enter image description here

What if I run my game on few random devices with different aspect ratios and resolutions? Will unity adjust my game automatically so everything fit perfectly or should I do it manually somehow? And how if I should?

\$\endgroup\$
3
\$\begingroup\$

This depends on how you've authored the objects.

  • 3D objects and 2D sprites positioned in your scene's worldspace will not move, scale or change their worldspace transformations no matter what happens to the display screen/window.

    • BUT: These objects are viewed through a Camera, and the camera's view is mapped to the screen. That mapping will change as the screen/window size changes.

      By default, Unity will keep the camera's vertical FoV or orthographic height the same, meaning that you'll see the same portion of your game world vertically. So if your objects were 60 pixels tall on a screen 1080 pixels in height, they'll be 40 pixels tall when displayed on a 720-pixel-high screen. The objects didn't move or shrink in your scene, they're just being displayed on fewer pixels.

      The width of the camera's view is, by default, allowed to grow or shrink to match the aspect ratio of the screen. So a player on a wider screen will see more of your scene to the left and right. If you haven't placed any objects there, they'll see blank space, because Unity doesn't know what you might want to put there instead - you'll have to tell it.

  • UI objects inside a Canvas element that's set to Screenspace - Camera or Screenspace - Overlay will be repositioned / sized according to their anchors when the dimensions of the containing canvas's viewport change.

    This lets you, say, make a UI button that's always in the bottom-right corner, no matter how tall or wide the window/screen is.

    The canvas and UI components expose a number of controls for you to decide exactly how you want this scaling to happen - whether you want to preserve sizes proportional to the width or height, or preserve the native pixel size of the elements, etc.

If you like the scaling behaviour described above, you're done. If not, then you'll need to define what "proper" scaling behaviour is for your game. Some examples:

  • Do you want to match the width of the screen instead of the height? Put a script on the camera that adjusts the height or FoV properties to match the aspect ratio of the screen/window.

  • Do you want to spread your objects wider / cluster them more narrowly depending on the width? Update your spawning script to scale its spread by the screen/window's aspect ratio.

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

It will adjust everything automatically for different resolutions if the aspect ratio is the same. If the aspect ratio is different, everything will still be scaled automatically, but you might see a little bit more or less of the game at the edges of the screen.

To see this for yourself, you can try resizing the game view window in the Unity editor and setting different aspect ratios in the game view control bar to see how your game behaves.

If you want to enforce a specific aspect ratio, this post illustrates the problem and also has potential solutions, such as enforcing a specific aspect ratio in the project settings, or adding a script to resize the camera viewport to fit that aspect ratio with a pillarbox or letterbox effect.

\$\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.