Get the true width and height of UI element regardless of its anchor points

Hi guys,

Any idea how to get the exact width and height of a UI element inside of Canvas?

The problem is that anchor positions greatly affect the results, so some times sizeDelta would work, and sometimes rect.width would work.

Is there a reliable way to fetch UI elements size? I am surprised there is no solid solution to that!

Thanks!

Anchor points shouldn’t effect the width… Even if you stretch it, the rect width should be whatever it is once stretched. The only issue I can think of is timing. If you change the width, it seems to need a frame before it returns the correct value. Example, I have a box that displays text and I adjust the box size, I have to wait a frame to get the new value.

Unless you’re doing something different that I just haven’t messed with when getting ui size. (Thus maybe you’re experiencing something I haven’t)

That is correct. However, the readings I receive from both sizeDelta and rect.width vary.

Is it because of this? (from the docs):
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.

You can use RectTransformUtility.PixelAdjustRect(rectTransform, canvas) to get a Rect that specifies the position and size of the RectTransform in pixels. Then if you need to, you can then refer to a Canvas Scaler to get the Pixels per unit to convert that Rect into Unity units.

5 Likes

Yes

I have tried this method. But for some reason, it returns Rect with values equal 0.

This function will work regardless of how you have your anchors set up, even if its driven by a LayoutGroup, even if the RectTransform is not a child of a canvas. it still gives accurate pixel measurements. Unless the Rect actually has a size of 0 (for example the default “Sliding Area” Gameobjects within the Scrollview have a height or width of 0 due to padding and parent size) I’ve never seen it return zero.

Can you show the code you are using? because the only way I can think of it returning 0 is if you’ve changed the Layout via code and then tried to read it before Unity had a chance to math out the Layout and rebuild it.

I have found the problem

The reason it was returning zeroes was that I ran the method in Awake() function. Putting it into Start() or Update() will do the trick!

3 Likes

:slight_smile: nice.

This will give you actual height and width of the Rect transform considering that your canvas size is 720x1280…
I’m doing this to get actual pixel size of rect transform …

float widthFactor =  Screen.width/720f;
        float heithFactor =  Screen.height/ 1280f;
        int width = Mathf.FloorToInt(panel.rect.width*widthFactor);
        int height = Mathf.FloorToInt(panel.rect.height * heithFactor);
2 Likes

It looks like RectTransformUtility.PixelAdjustRect won’t work if the UI has a Content Size Fitter component.

For example, if Content Size Fitter’s “Vertical Fit” is set to “Preferred Size”, the previous solution would return 0 as a height. Same with “Horizontal Fit” - width would be 0.

Does anyone have an idea how this could be fixed?

In order for calculations to return correctly, you may have to use Canvas.ForceUpdateCanvases() and ensure that all elements with a Content Size Fitter are activeInHierarchy at the time of calculation.