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
  3. Dismiss Notice

(Android) RectTransform height not being updated immediately after changing screen orientation.

Discussion in 'UGUI & TextMesh Pro' started by Fellshadow, Sep 15, 2015.

  1. Fellshadow

    Fellshadow

    Joined:
    Dec 2, 2012
    Posts:
    169
    I'm trying to set up a separate UI layout for landscape and portrait view, and have two panels (HorizontalLayout and VerticalLayout) that I disable or enable depending on the orientation. Afterwards, I instantiate an object and set it's height to a percentage of the screen height through code. I do this by getting the parent panel's (It always takes up the full height of the screen) rectTransform height and dividing it by a set amount.
    The problem I'm encountering is that if I do this immediately after enabling the appropriate panel, the RectTransform height has not been updated to the new orientation's height.

    So it ends up like this:
    -In portrait orientation
    -Switched to Landscape
    -Disable VerticalLayout gameobject
    -Enable HorizontalLayout gameobject
    -Child of HorizontalLayout gameobject tries to get HorizontalLayout's height
    -The height from HorizontalLayout is the same as VerticalLayout's height

    I've checked my references to make sure the child is referencing the correct object (it is), and I've tried delaying the function to get the height by one frame using a coroutine, but it still has this issue unless I delay it by *at least 5 frames*, then it gets the *correct height*.

    What is going on? Where am I messing up?

    Orientation Detection Code:
    Code (csharp):
    1.     void Update () {
    2.         bool landscapeCheck;
    3.  
    4.         if(Input.deviceOrientation == DeviceOrientation.LandscapeLeft || Input.deviceOrientation == DeviceOrientation.LandscapeRight) {
    5.             landscapeCheck = true;
    6.         }
    7.         else{
    8.             landscapeCheck = false;
    9.         }
    10.  
    11.         if(landscapeCheck != isLandscape) {
    12.             Debug.Log("Setting Orientation: " + Input.deviceOrientation);
    13.             if(landscapeCheck) {
    14.                 SetHorizontal();
    15.             }
    16.             else {
    17.                 SetVertical();
    18.             }
    19.  
    20.             isLandscape = landscapeCheck;
    21.         }
    22.     }
    Get height code:
    Code (csharp):
    1. barSize = transform.parent.GetComponent<RectTransform>().rect.height / barAmount;