Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

What is RectTransform.sizeDelta?

Discussion in 'Scripting' started by Shayan, Jun 9, 2017.

  1. Shayan

    Shayan

    Joined:
    Jul 8, 2013
    Posts:
    53
    Hello!
    My question is in the header. I can't get what's the RectTransform.sizeDelta?
    I understood when anchors are same it shows a real size... But if it is not so I don't get that it shows...
    How does it calculate?
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    From the docs you linked:
    If the anchors are together, sizeDelta is the same as size. If the anchors are in each of the four corners of the parent, the sizeDelta is how much bigger or smaller the rectangle is compared to its parent.
     
  3. Shayan

    Shayan

    Joined:
    Jul 8, 2013
    Posts:
    53
    Yeah, I read it =) I mean what's it? How can I calculate this value for different anchors positions manually? Just I need it to write a custom UI fitter.
     
    IgorAherne likes this.
  4. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    You could try using RectTransfrom.rect.size instead.
     
  5. Shayan

    Shayan

    Joined:
    Jul 8, 2013
    Posts:
    53
    Thank you, I'll try!
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    If you're still looking for this, I'd just try to look at the values in a couple of tests and see if you can figure it out :)
     
    twobob likes this.
  7. IgorAherne

    IgorAherne

    Joined:
    May 15, 2013
    Posts:
    392
    right, so noone actually knows what the heck sizeDelta is...
     
    LHAppri and a436t4ataf like this.
  8. IgorAherne

    IgorAherne

    Joined:
    May 15, 2013
    Posts:
    392
    Last edited: Mar 25, 2019
  9. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    Pretty sure the OP knows the effect of modifying sizeDelta since hes asking how it calculates its value, which is dependent on the anchor values, and nobody should use such a an unreliable value as reference for anything.

    As for your code example, there is no sense in getting the rectTransform component from the Canvas since its values are frozen.
    You can also shorten the GetComponent line to this:
    Code (CSharp):
    1. RectTransform rt = object.GetComponent<RectTransform>();
    and
     
    Last edited: Jun 12, 2017
    Shayan likes this.
  10. suiri

    suiri

    Joined:
    May 28, 2017
    Posts:
    1
  11. Psyboyo

    Psyboyo

    Joined:
    May 16, 2017
    Posts:
    4
    sizeDelta: If you made a search, and end up here for an explanation of what sizeDelta means, like GetComponent<RectTransform>().sizeDelta.y, then clear your mind.

    Visualize a small PANEL, resting on top of a big CANVAS, it's Parent object.
    In the PANEL's Rect Transform component, there are 2 rectangles defined:

    (a) The rectangle defined by its Anchors. Those triangles. Normally related to the Parent Object location and dimensions, in this case the CANVAS.

    (b) The rectangle defined by its own size, the PANEL's own dimension.

    sizeDelta = (b) - (a)

    That's it. Because normally an interactive component like a Button, smaller in size compared to the object where it rests, like a Panel, and because of that, normally sizeDelta is a negative value. Button size - Panel size = a negative value, normally.
    You know the term Negative Space, used in general Design theory?
    Think of it, as the space NOT used by a Button resting on a Panel.

    Example:
    How to find the height of a Panel, that is a Child of a Canvas that is a Camera overlay, thus screen sized. The Anchors of the Panel are related to the Canvas dimensions. Script is on the Panel object:

    panelHeight = Screen.height + this.GetComponent<RectTransform>().sizeDelta.y;

    Remember, sizeDelta is normally negative so it reads more like this pseudo code:
    panelHeight = Screen.height - this.sizeDelta.y

    Hope this helps you, drove me crazy for a while. Cheers!

    References:

     
    GamerFawn, Shayan and a436t4ataf like this.
  12. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,910
    Ah! This explains why most of the google hits for "get the size of a RectTransform" say "sizeDelta" -- and it works, the first few times. Then breaks completely forever.

    (It "works" because if the (a) rect is zero, then b-a == b, equals the size)

    Unity did a spectacularly bad job of naming these API elements :D.
     
    elimar and Shayan like this.
  13. dizzy2003

    dizzy2003

    Joined:
    Nov 25, 2013
    Posts:
    103
    I finally got around to understanding the chaos that is sizeDelta.
    Heres the function you need.


    Code (CSharp):
    1. public static void SetRectSize(RectTransform rt,Vector2 sizeWant)
    2. {
    3.     //The size delta is relative to the current size of the rectTransforms anchors
    4.     //so if the anchor min and anchor max is the same, then no problem, sizeDelta gives the size you ask for
    5.     // But if (rt.anchorMax-rt.anchorMin) is not 0,0 then you wont get the answer you expect
    6.     // So you have to work out the size of the anchor (anchor max-min) by looking at the parents rect
    7.     //then take that away from the size you actually want
    8.  
    9.     var rtp = rt.parent.transform as RectTransform;
    10.     Vector2 sizep = rtp.rect.size;
    11.     sizep.Scale((rt.anchorMax-rt.anchorMin));
    12.     var sdelta =  sizeWant-sizep;
    13.     rt.sizeDelta = sdelta;
    14.  
    15. }