RectTransform.rect behaving differently 2020.1 -> 2020.3 LTS

I’m trying to upgrade from 2020.1.4f1 to 2020.3.4f1 (LTS) and I’m having an issue where some of my dynamically created UI elements are four times larger than they were in the older build. The UI created in the editor is all correctly sized, but I create some UI dynamically from a prefab and size them based on their parent transform’s rect width and height. The result of the parent’s width and height is four times larger, so these UI elements end up rendering four times too large.

RectTransform panelRectTransform = gridPanel.GetComponent<RectTransform>();
gridPanelWidth = panelRectTransform.rect.width;
gridPanelHeight = panelRectTransform.rect.height;
Debug.Log($"gridPanelWidth: {gridPanelWidth}");
Debug.Log($"gridPanelHeight: {gridPanelHeight}");

2020.1:
gridPanelWidth: 1323.049
gridPanelHeight: 827.7333

2020.3:
gridPanelWidth: 5292.196
gridPanelHeight: 3310.933

Any ideas what could be causing this? How do I adjust for this? I doubt just dividing by 4 is the right answer. Preferably, I’d have a way to adjust the asset so it doesn’t happen, so that I don’t have to find everywhere that is using code like this and fix it.

1 Like

Could simply be a timing issue. How is gridPanel setup? Is it already in the scene or something you create?

Yes, it’s created in the editor.

Ok, so it’s already in the scene? Depending on when you get the width and height and if you’re making changes to it, I usually do a yield in a coroutine till end of frame to get the size after the yield. But I’d have to know more about your setup to know if that is the issue. Right now I’m just guessing based on past experiences.

I used a coroutine to move the calculation to the end of frame, and that actually fixed it!

Thanks!

Ah, nice! In that case, it’s just a timing issue where it hasn’t gotten the size set yet, so by waiting till the end of the frame, it’s finished updating the size and you’re able to grab it at the end of the frame.

Yeah, it seem so. I guess something in the timing changed between 2020.1 and 2020.3?

Maybe it was just the script order that changed when you upgraded your project.