proper 2d scaling for mobilephones

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By RoniPerson

I’m coming from unity and I try to port one of my old prototypes to godot. It is a mobile game. When developing for mobile it is important to respect multiple aspect ratios.
In unity my scaling worked the way that the width of the shown area was constant. The height is set based on this. It is basicly the scaling behavior you get when adding an ortagonal 3d camera and set the scaling mode of the camera to keep width.
But my game is 2d and therefore I have to use the 2d mode. When using it you can set the scaling mode in the project settings to keep_width as well. But there is a difference to the 3d scalling behavior. In 3d the height of the visible area can get less than it started. But in 3d the height of the visible area can only get bigger and when the window is to width there are black bars added instead of decreasing the height.

Is there a way to achieve the scaling behaviour of the 3d camera with 2d.

Edit:
I hope it is understandable what i mean. If not feel free to ask. I would add images but I dont know how. The image options only allows for urls.

:bust_in_silhouette: Reply From: Calinou

Use the 2d stretch mode and expand stretch aspect, and make sure to configure anchors correctly in your UI nodes to support multiple aspect ratios. See Multiple resolutions in the documentation.

Is there a way to achieve the scaling behaviour of the 3d camera with 2d.

You can still adjust the camera zoom to compensate for different screen resolutions manually. Basically, you can multiply the camera zoom value by the scale factor.

:bust_in_silhouette: Reply From: RoniPerson

Here is a litle script to solve this.

extends Camera2D

var base_width

func _ready():
    base_width = ProjectSettings.get_setting("display/window/size/width")
    get_tree().get_root().connect("size_changed", self, "calc_size")
    calc_size()

func calc_size():
    var s = base_width / get_viewport_rect().size.x

    zoom.x = s
    zoom.y = s