0
\$\begingroup\$

What is my problem?
I have started a new 2d game project in Unity and would like to allow for resizing of the window by the player. When resizing, I don't want black bars at any of the 4 sides (I think that's called letterboxing?) in windowed mode. Instead I would like that height or width of the window (it depends what the user input is) is adjusted to the other metric, so that one aspect ratio is always kept when resizing. If possible I'd like to use built-in functionality of Unity and not external code packages.

What solutions have I considered so far and why they don't work (for me):

  • The first animated image of the readme of this github project explains what I would like to accomplish:
    solution_for_windows
    As stated in the readme however, this code only works for windows.

  • In this stackoverflow post and this gamedev stackexchange post the advised solution is to constantly use Screen.SetResolution() in the Update() method. This solution however seems very resource intensive, so if there's a way to only update this when the screen is actually resized, please tell me.

  • In a lot of gamedev stackexchange and stackoverflow posts the recommended solution is often to use a canvas or something like that. As my problem is not the UI itself but the whole game, I think this solution wouldn't suit my needs.

Other info you might need to help me:
Here is a screenshot of my current camera configuration (URP):
camera configuration

Here are my resolution and presentation settings:
enter image description here

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

In this stackoverflow post and this gamedev stackexchange post the advised solution is to constantly use Screen.SetResolution() in the Update() method. This solution however seems very resource intensive, so if there's a way to only update this when the screen is actually resized, please tell me.

This is actually a very good solution in my opinion but what you should do is to declare two variables: LastWidth and LastHeight

And whenever one of them wasn't equal to the current width/height, just use Screen.SetResolution and then set LastWidth/LastHeight to the current width/height.

Of course, it's not efficient as you have to do two checks every frame, but Unity doesn't have anything for resizing (wish there was an OnWindowResize() or something) so

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .