Search Unity

Question Beginner question on relative sizes of text elements on a canvas

Discussion in 'Getting Started' started by Mpx83, Apr 11, 2023.

  1. Mpx83

    Mpx83

    Joined:
    Apr 4, 2023
    Posts:
    21
    Hi,
    I'm sure that this is really simple but I cannot find a solution and I am a complete beginner.

    In the scene editor I created a very simple 2d scene with a canvas and inside it some text boxes.
    The textboxes are TextMeshPro objects.
    I would like to set one textbox to be 20% of the width of the canvas and 50% of the height of it, and to keep these proportions if I change the dimensions of the canvas.

    To do this, in the Anchors presets I select the "stretch" option and put the anchors to the corners of the parent canvas. Then I set the X scale to 0.2 and the Y scale to 0.5. This method seems to work but the problem is that also the text is scaled both in the X and Y directions and it looks weird.

    Is anyway to avoid the scaling of the text or is any other method to achieve what I need?

    Thanks for your help
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Yes, your approach is incorrect.

    Put the X and Y scale of your object back to 1. The correct way to do this is to explicitly set the anchors. For example, set your X min to 0 and X max to 0.2, which makes it 20% the width of the parent (and positioned on the left). For 50% the parent height, you'd set Y min/max to 0 and 0.5 (or 0.2 and 0.7, or whatever else you like). Set your Left, Top, Right, and Bottom all to 0, and Bob's your uncle.
     
  3. Mpx83

    Mpx83

    Joined:
    Apr 4, 2023
    Posts:
    21
    Thanks, It works!