First of all, sry for my English mistakes, I'm not a native English speaker. I'm trying to make an UI which is composed of a canvas within different gameObject, and I would like that my canvas scales to the dimension of the screen but keeps its original resolution (16x9 portrait). If it is displayed on a tablet resolution (4x3) then an image is displayed in the space that is not covered by the canvas. But actually all I've got is a canvas which scales to every resolution, and it changes the aspect of its child (for example a square becomes a rectangle).
Thank you for showing interest in my query!
CodePudding user response:
UI's are heavy beasts. Canvas in Unity have a component attached to themm called Canvas Scaler
which is set by default on Constant Pixel Size
. You may try to set this property on Scale With Screen Size
and then specify the base resolution you want to work with (usually 1920x1080 is a console standart). This is your first step
Then, to avoid strange Image
scaling, you may check the property Preserve Aspect
, this way the ration of the Sprite
into your Image
will remain the same indepently of the ratio of the Image
Last, you may play a bit with anchors but this is another story, you should let those at plain center at the beginning and come back to it when you will feel ready
Hope that helped ;)
CodePudding user response:
Your canvas should have a component called Canvas Scaler
. Here it should say Constant Pixel Size
, change this to Scale With Screen Size
and it should lock the Canvas to be the same width / height as the screen. If you want to lock an image to a specific width/height ratio, go to the Image
component on the image and check the Preserve Aspect
checkbox. This way if you have a 100x100 image, the images width will always be the same as the height. If you have a 200x100 image, the images width will always be twice the height, etc etc etc.