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)
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.
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.
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);
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.
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.