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?
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.
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.
You could try using RectTransfrom.rect.size instead.
Thank you, I’ll try!
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
right, so noone actually knows what the heck sizeDelta is…
Well, this will be useful:
What sizeDelta is (Explanation with pictures)
Modify the width and height of RectTransform
lzt120 How Can I modify the width and height of RectTransform ?
jashan Try this
csharp* *RectTransform rt = UICanvas.GetComponent (typeof (RectTransform)) as RectTransform; rt.sizeDelta = new Vector2 (100, 100);* *
where UICanvas is Canvas from scene.
or this link if you need to match the dimension of some other rec transform.
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:
RectTransform rt = object.GetComponent<RectTransform>();
and
hm
sizeDelta: If you made a search, and end up here for an explanation of what sizeDelta means, like GetComponent().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().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:
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 .
I finally got around to understanding the chaos that is sizeDelta.
Heres the function you need.
public static void SetRectSize(RectTransform rt,Vector2 sizeWant)
{
//The size delta is relative to the current size of the rectTransforms anchors
//so if the anchor min and anchor max is the same, then no problem, sizeDelta gives the size you ask for
// But if (rt.anchorMax-rt.anchorMin) is not 0,0 then you wont get the answer you expect
// So you have to work out the size of the anchor (anchor max-min) by looking at the parents rect
//then take that away from the size you actually want
var rtp = rt.parent.transform as RectTransform;
Vector2 sizep = rtp.rect.size;
sizep.Scale((rt.anchorMax-rt.anchorMin));
var sdelta = sizeWant-sizep;
rt.sizeDelta = sdelta;
}