Search Unity

UI Builder. Problems with scale images

Discussion in 'UI Toolkit' started by DREBOTgamestudio, Jul 7, 2020.

  1. DREBOTgamestudio

    DREBOTgamestudio

    Joined:
    Jan 30, 2018
    Posts:
    66
    Hello. I have 2 igames: testBackground and testImage.
    TextBackground.png TestImage.png
    I place them on the layout and align them with each other. If I change the size they will stretch. This is normal. But we need a constant size relative to the screen.
    upload_2020-7-7_16-1-21.png

    i find scale mode. That should have solved my problem. But for me it does not work as it should.
    If i pick scale-and-crop it is cut TestImage.
    upload_2020-7-7_16-10-37.png

    If i pick scale-to-fit it is change size.
    upload_2020-7-7_16-11-3.png

    Resizing does not help return the correct scale.
    I spent a few hours but never found the perfect solution. I feel like I'm missing something.
    How do I achieve scale while maintaining proportions?
     
  2. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
    I'm not sure I get what you want to achieve.

    First off, if you want to keep the center image always in the center, you should not be using Position > Position: Absolute. You should instead use the Align options you already are using on your TestImage (just note that they apply to the children of an element, not to the element itself).

    You already are using % for the width/height which should maintain the proportion of the inner red image with the proportions of its parent green image. This is if you do want it to stretch.

    However, if you want the red image to maintain its own original proportions, regardless of the height/width of its parent green image, then all you can do is set to a fixed width/height. There is no USS property combination to achieve an aspect-ratio lock but still having it grow/shrink.
     
    DREBOTgamestudio likes this.
  3. DREBOTgamestudio

    DREBOTgamestudio

    Joined:
    Jan 30, 2018
    Posts:
    66
    Thank you for the answer. I will change my question. I have 3 perfect squares. How can I change the size of the squares depending on the screen?

    upload_2020-7-8_11-18-16.png

    If I choose scale-to-fit, these are no longer squares. :(:(:(

    upload_2020-7-8_11-19-31.png

    If i set to a fixed width/height and choose stretch-to-fill, the squares will not change size.
    Or do you want to say that I cannot get a square if I change the screen size?
     
  4. uMathieu

    uMathieu

    Unity Technologies

    Joined:
    Jun 6, 2017
    Posts:
    398
    There is a way to get an aspect-ratio, but it's a little couter-intuitive. The padding-top property, when specified as percentage is relative to the parent element's width. Thus, you can set a height of 0, and use a percentage padding-top and width to get a element that will have a constant ratio.

    Now controlling the height through padding will be fine if your element only has a background image and no children, since the content will be pushed downward. If you want to add children, you need to add one in between, that will use absolute positioning to stretch with the aspect-ratio-box, then add the children to this element instead.

    Here is a demo:
    Code (CSharp):
    1. <ui:UXML xmlns:ui="UnityEngine.UIElements">
    2.     <ui:VisualElement name="container" class="stretch-to-parent-size center-content" style="background-color: rgba(0, 255, 0, 255);">
    3.         <ui:VisualElement name="aspect-ratio-element" class="aspect-ratio-element" style="background-color: rgba(255, 0, 0, 255);">
    4.             <ui:VisualElement name="aspect-ratio-content" class="stretch-to-parent-size" />
    5.         </ui:VisualElement>
    6.     </ui:VisualElement>
    7. </ui:UXML>
    8.  
    Code (CSharp):
    1. .stretch-to-parent-size {
    2.     position: absolute;
    3.     left: 0;
    4.     top: 0;
    5.     right: 0;
    6.     bottom: 0;
    7. }
    8.  
    9. .center-content {
    10.     align-items: center;
    11.     justify-content: center;
    12. }
    13.  
    14. .aspect-ratio-element {
    15.     width: 50%;
    16.     height: 0;
    17.     padding-top: 28.125%;   /* We want a 16:9 box, so padding needs to be: 9/16 * width in percentage */
    18. }
    19.  
     

    Attached Files:

  5. DREBOTgamestudio

    DREBOTgamestudio

    Joined:
    Jan 30, 2018
    Posts:
    66
    Thanks, this partly works. Are you sure of the numbers you gave me (padding-top: 28.125%)? For these numbers, it looks like this.
    1111111.jpg

    Screen resolution 1920 x 1080
    upload_2020-7-8_21-52-40.png

    With other numbers(18%), it turns out similar to the original result.
    22222222.jpg

    But for the original size, you need to change the parameters a little more. But these are all approximate and inaccurate settings.

    33333333333.jpg

    Finally. It is very inconvenient to resize an object. The result is not accurate. Are there any other more usable options?
    But. Your method really works. Thanks again.
     
  6. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
    Resizing in the canvas currently only really supports pixel resizing, not %. I think for %, it will change the percent number as if it was px, hence the inaccuracies. But even if it had % support, your use case is a bit special and requires use of the Inspector.
     
  7. marcospgp

    marcospgp

    Joined:
    Jun 11, 2018
    Posts:
    194
    With height set to auto, shouldn't it automatically grow in order to keep the image's aspect ratio based on the width?

    So if I set width to 100%, the height should increase automatically. Right now, this happens only if the width is less than the image's height. After that, the height stops automatically growing, which is a bit unexpected compared to CSS.

    Also, the trick above didn't work for me because the padding seems to not be interpreted as part of the element's size - it just eats into the parent element.
     
    Last edited: Feb 17, 2022
    SparkesRS and thalesdeluca like this.
  8. vejab

    vejab

    Joined:
    Dec 21, 2021
    Posts:
    119
    I also have a question about scaling images : whenever an image is scaled, it is centered. Is there a way to change this?