Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question RectTransform.GetWorldCorners() results are not consistent between PC and Mobile

Discussion in 'UGUI & TextMesh Pro' started by Mhmd-Subhi, Sep 14, 2021.

  1. Mhmd-Subhi

    Mhmd-Subhi

    Joined:
    Mar 13, 2015
    Posts:
    28
    Hello everyone,

    I am trying to make a UI that is above all of other UI, for both Overlay or World space UIs. So, I get GetWorldCorners(), then, if the source is a World UI, I correct this by using camera WorldToScreenPoint() transformation matrix. Then I scale the RectTransforms in the Upper UI according to these corners.

    In Mobile, this have given me what I wanted, in PC, if any resolution other than 1980 x 1080, which is the reference resolution of Canvases in the game, the results aren't consistent.

    The same happens when using Aspect ratio for the "Game" tap in Unity, but if I use a set resolution, instead of a ratio, the results are exactly what I want, same as in mobile.


    Correct Scaling


    Wrong Scaling


    Correct Scaling


    Wrong Scaling

    Finally, this is the code I use for scalling:

    Code (CSharp):
    1. private void LateUpdate ()
    2.     {
    3.         if (refRectTransform == null)
    4.             return;
    5.  
    6.         //var cornerPoints = new Vector3[4];
    7.         refRectTransform.GetWorldCorners(cornerPoints);
    8.  
    9.         minCorner = (refUsesWorldSpace)? (Vector3)RectTransformUtility.WorldToScreenPoint(levelCamera, cornerPoints[0]) : cornerPoints[0];
    10.         maxCorner = (refUsesWorldSpace)? (Vector3)RectTransformUtility.WorldToScreenPoint(levelCamera, cornerPoints[2]) : cornerPoints[2];
    11.  
    12.         newPos = (minCorner + maxCorner) / 2 + (Vector3)positionOffset;
    13.         newPos.z = 0;
    14.  
    15.         rectTransform.position = newPos;
    16.         rectTransform.sizeDelta = new Vector2((maxCorner.x - minCorner.x), (maxCorner.y - minCorner.y)) + addSize;
    17.     }
    Thank you for your time
     
  2. RichardGoesToNightSchool

    RichardGoesToNightSchool

    Joined:
    Dec 9, 2020
    Posts:
    27
    I've been getting differing results for GetWorldCorners depending on if I call it in Update or LateUpdate. I think it might have something to do with the layout component.