Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Bug Root canvas invalid RectTransform.sizeDelta

Discussion in 'Editor & General Support' started by diwang, Aug 20, 2020.

  1. diwang

    diwang

    Joined:
    Sep 14, 2015
    Posts:
    30
    I have a root canvas (the most top object that have canvas component in hierarchy) , and i have canvas scaler with specification :
    • UI Scale Mode = Scale With Screen Size
    • Screen Match Mode = Expand
    Then i check the root canvas RectTransform size delta with this script

    Code (CSharp):
    1.  
    2. public int oldscreenwidth = 0;
    3. public int oldscreenheight = 0;
    4. public GameObject rootcanvas;
    5. void awake() {
    6. oldscreenwidth = Screen.width;
    7. oldscreenheight = Screen.height;
    8. rootcanvas = this.gameObject.GetComponentInParent<Canvas>().rootCanvas.gameObject;
    9. }
    10. void update() {
    11. if(Screen.width != oldscreenwidth || Screen.height != oldscreenheight ) {
    12. Debug.Log(" rootcanvas SizeDelta when screen resolution changed : " + rootcanvas.GetComponent<RectTransform>().sizeDelta);
    13. oldscreenwidth = Screen.width;
    14. oldscrenheight = Screen.height;
    15. } else {
    16. Debug.Log(" rootcanvas SizeDelta in normal condition : " + rootcanvas.GetComponent<RectTransform>().sizeDelta);
    17. }
    18. }
    19.  
    I noticed that when the screen resolution changed , at that exact frame , root canvas size delta will "blink" to wrong size which will be corrected in the next frame. This is annoying as i was wondering what was wrong with my UI script until i found this bug.
    Code (CSharp):
    1. LayoutRebuilder.ForceRebuildLayoutImmediate(rootcanvas.GetComponent<RectTransform>());
    I also have tried force rebuild before the Debug.Log but it seems like the problem cannot be fixed at that frame , the problem fixes itself the next frame after screen resolution changed.