Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question RectTransform height of two objects are same but different on screen

Discussion in 'UGUI & TextMesh Pro' started by djalobre, Jan 21, 2023.

  1. djalobre

    djalobre

    Joined:
    Apr 18, 2022
    Posts:
    2
    I have one probably stupid question, but cant find anything useful in unity documentation.

    I have a 2d object that is a panel, and button within it, as I wanted to have 5 buttons with same height that would fill that panel, my idea was to get height of panel and then get 20% of it.

    But what I am not getting is how this height thing even works, I have panel of 500 height, and button of 500 height that are not same size?? Can't find anything useful on this topic so bumping here for tips.

    I am talking about rect transform height.



    pictures for the reference




     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,582
    This is UGUI not 2D. I'll move your post to the UI forum.
     
    djalobre likes this.
  3. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    604
    One you fundamental part of how unity works that you need understand is that all the positioning, rotation and scaling is based on object hierarchy. This is somewhat described in the "Parenting" section of documentation for transform https://docs.unity3d.com/Manual/class-Transform.html . While RectTransforms aren't exactly the same as regular Transform they share a lot of stuff and most of it applies equally to both.

    For example if you have object hierachy like this:

    Code (CSharp):
    1. * box 1 (scale= 0.3)
    2.     * box2 (scale = 0.25)
    3.          * box4 (scale=2, height=100)
    4.     * box3 (scale = 0.5)
    5.           * box5 (scale=2, height=100)
    Even though both box4 and box5 have the same local height and scale, their actual height on screen will be different. The height of box4 will be 0.3*0.25*2*100=15, but box5 will be 0.3*0.5*2*100=30.

    The difference you are observing is most likely due to one of the ancestors (parents, or parent parent ....) have different scale.

    As a general advice strongly recommend you to never set the scale to anything else than 1,1,1 for GUI objects (the ones with RectTransform which are inside canvas). That way it will be a lot more easier to deal with dimensions and they will be directly comparable between multiple objects.

    Even if you didn't intentially change the scale you might have done it accidentally so keep an eye for it. There two tools for changing size : Scale tool and Rect tool. Both will seemingly cause the size of object change, but for RectTransforms you want to use the second. Scale tool will change the scale but Rect tool changes width/height.

    You can also get weird scale if you created a GUI object by dragging a prefab into scene without parenting to canvas and afterwards reparented the new object somewhere under the canvas.
     
    djalobre likes this.
  4. djalobre

    djalobre

    Joined:
    Apr 18, 2022
    Posts:
    2
    Thanks a lot man, helped a ton!