Search Unity

How could I get the real rect of a Window which is created with GUILayout.Window

Discussion in 'Immediate Mode GUI (IMGUI)' started by enmeii, Jul 23, 2015.

  1. enmeii

    enmeii

    Joined:
    May 18, 2009
    Posts:
    7
    I am trying to create a lot of windows and sort them automatically.
    But I found the size of any window is wrong.

    Code (csharp):
    1. Rect newR = GUILayout.Window(i, new Rect(triggerGroupManager.triggerGroupList[i].posInEditor.x, triggerGroupManager.triggerGroupList[i].posInEditor.y, 0, 0), DrawTriggerGroup, "" + i);
    I use GUILayout system to create contents in windows. So the size of window should be fit the contents, which is smart. But I found the width or height of variable newR is 0 as the beginning.

    Plz help me, I am wondering to get the real size of a window.
     
  2. SonicBloomEric

    SonicBloomEric

    Joined:
    Sep 11, 2014
    Posts:
    1,090
    So, the Rect you are passing to the GUILayout.Window function is defined as follows:
    Code (CSharp):
    1. // Width and Height are 0!
    2. Rect windowRect = new Rect(triggerGroupManager.triggerGroupList[i].posInEditor.x, triggerGroupManager.triggerGroupList[i].posInEditor.y, 0, 0);
    3.  
    4. Rect newR = GUILayout.Window(i, windowRect, DrawTriggerGroup, "" + i);
    You're passing 0 for width and height. Are you actually seeing something on-screen when the code runs?
     
  3. enmeii

    enmeii

    Joined:
    May 18, 2009
    Posts:
    7
    Thanks for replying.
    As the Unity guide says : The layouting system will attempt to fit the window inside it - if that cannot be done, it will adjust the rectangle to fit.
    I am using GUILayout functions to draw my GUI, so that everything in this window works fine and smart. But the Rect value is same with I defined before. windowRect equals "new Rect(triggerGroupManager.triggerGroupList.posInEditor.x, triggerGroupManager.triggerGroupList.posInEditor.y, 0, 0)"
    Actually, I want to get the real rect size, so that I can sort all windows one by one.
     
  4. SonicBloomEric

    SonicBloomEric

    Joined:
    Sep 11, 2014
    Posts:
    1,090
    First off, I don't think you answered my question:
    Are you actually seeing something on-screen when the code runs?
    I'm asking this because in theory a sub-window of size (0,0) will not be visible. Nowhere does the documentation say that "specifying a size of 0 will cause the window to autofit", which I think is what you're assuming/trying.

    Yes, it does indeed say that. However, I interpret that to imply that the layouting system will shrink the content if the specified size is too large for the specified area. This is why I asked if

    I would suggest trying the following:
    Code (CSharp):
    1. // Width and Height are set to the entire size of the containing window!
    2. Rect windowRect = new Rect(triggerGroupManager.triggerGroupList[i].posInEditor.x, triggerGroupManager.triggerGroupList[i].posInEditor.y, position.width, position.height);
    3. Rect newR = GUILayout.Window(i, windowRect, DrawTriggerGroup, "" + i);
    Note that you could also do something like position.width/numHorizontalSubWindows (and similar for height).

    But, again, when you use a size of (0,0),
    Are you actually seeing something on-screen when the code runs?
     
  5. enmeii

    enmeii

    Joined:
    May 18, 2009
    Posts:
    7
    Yes. The window shows well no-matter the size is (0,0). But It is that drives me crazy. Because I can't get the real size of any window if the content of window is a little more.
    I tried "position.width/numHorizontalSubWindows" way. But if the content is larger than size, windows are covered by others.
     
  6. SonicBloomEric

    SonicBloomEric

    Joined:
    Sep 11, 2014
    Posts:
    1,090
    What does the position variable output from within your GUI.WindowFunction function that you pass to GUILayout.Window?

    Alternatively, perhaps this will help?
     
  7. enmeii

    enmeii

    Joined:
    May 18, 2009
    Posts:
    7
    I have to say I get the real size now with your code right now!!!!
    WHY? It's wrong yesterday!!!
    Anyway. Thanks a lot. @SonicBloomEric.
     
  8. SonicBloomEric

    SonicBloomEric

    Joined:
    Sep 11, 2014
    Posts:
    1,090
    Glad to hear that it worked! I suspect that internally, the following code exists somewhere:
    Code (CSharp):
    1. //NOTE: this is pseudocode.
    2. Rect actualRect = GetActualRect(userRect);
    3.  
    4. return Mathf.MinRect(actualRect, userRect);
    So if you were to pass in (0,0), the MinRect would always be the one you passed in...

    Which sounds like a bug, honestly - either in implementation or in documentation. Could you file a bug with Unity on this one? Do so by selecting Help→Report a Bug... from within Unity. Then they could either fix it to work with your expectations or adjust the documentation!