How to calculate sizeDelta in RectTransform?

Шах picture Шах · Jun 10, 2017 · Viewed 30k times · Source

I write a custom content fitter that is required for my custom layout. So, I need to control RectTransform.sizeDelta property when anchors aren't same but I can't get that shows this value.
I don't need Unity3D API reference, I read it and got a nothing cuz it says only:

The size of this RectTransform relative to the distances between the anchors. 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.

Can anyone explain in normal language what does it mean? And how can I calculate it manually when anchors aren't same?

Answer

Galandil picture Galandil · Jun 10, 2017

The definition is somewhat confusing, indeed.

sizeDelta, basically, returns the difference between the actual rectangle of the UI element and the rectangle defined by the anchors.

For example, given a rectangle of 300x200:

Anchors in the same place as the corners of the rectangle: sizeDelta is (0,0) enter image description here

Left or right anchors at half width of the rectangle: sizeDelta is (150,0) enter image description here enter image description here

All four anchors in a point: sizeDelta is (300,200) (i.e.: same size as the rectangle) enter image description here enter image description here

As you can see, it doesn't matter at all where the center of the rectangle defined by the anchors is, the only thing that matters is the difference between the width and height of the element rectangle and the anchors rectangle.

In pseudo-code, it's like this:

sizeDelta.x = UIElementRectangle.width - AnchorsRectangle.width;
sizeDelta.y = UIElementRectangle.height - AnchorsRectangle.height;

So, if the UI Rectangle has a dimension bigger than the anchors' one, sizeDelta is positive, if it's smaller, sizeDelta is negative.