Search Unity

Question RectTransform.rect behaving differently 2020.1 -> 2020.3 LTS

Discussion in 'Scripting' started by dclamage, Apr 21, 2021.

  1. dclamage

    dclamage

    Joined:
    Sep 29, 2020
    Posts:
    6
    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.

    Code (CSharp):
    1. RectTransform panelRectTransform = gridPanel.GetComponent<RectTransform>();
    2. gridPanelWidth = panelRectTransform.rect.width;
    3. gridPanelHeight = panelRectTransform.rect.height;
    4. Debug.Log($"gridPanelWidth: {gridPanelWidth}");
    5. 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.
     
    PutridEx likes this.
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Could simply be a timing issue. How is gridPanel setup? Is it already in the scene or something you create?
     
  3. dclamage

    dclamage

    Joined:
    Sep 29, 2020
    Posts:
    6
    Yes, it's created in the editor.
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    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.
     
  5. dclamage

    dclamage

    Joined:
    Sep 29, 2020
    Posts:
    6
    I used a coroutine to move the calculation to the end of frame, and that actually fixed it!

    Thanks!
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    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.
     
  7. dclamage

    dclamage

    Joined:
    Sep 29, 2020
    Posts:
    6
    Yeah, it seem so. I guess something in the timing changed between 2020.1 and 2020.3?
     
  8. vitaliano_fanatee

    vitaliano_fanatee

    Joined:
    Oct 9, 2020
    Posts:
    37
    Maybe it was just the script order that changed when you upgraded your project.