Actual Rect for GUILayout.Scrollview?

Discussion in 'Extensions & OnGUI' started by Molix, Oct 20, 2009.

  1. Molix

    Molix

    Member

    Joined:
    Apr 24, 2009
    Messages:
    90
    Hello,

    I need the resulting Rect for a scrollview I have drawn with GUILayout.

    e.g.
    Code (csharp):
    1. scroller = GUILayout.BeginScrollView( scroller );
    2.      // dynamic run-time data drawn
    3.      // in various Verticals/Horizontals
    4.      // possibly with FlexibleSpace
    5.  
    6. Rect scrollArea = ?
    Or even:
    Code (csharp):
    1.   // various controls
    2.  
    3. Rect boxArea = ?
    I know you can use GUILayout.GetRect(...) to "reserve" the space for a single control like a Button--or more specifically for 1 piece of GUIContent--and then call the GUI equivalent with that Rect. But how do you find out how big your scrollview, or styled-BeginHorizontal() is? Even if I rewrite everything to GetRect() and GUI call, I still can't just union them all together because I'll need to clip it against the scroll view size, and I don't think you can get the size of the FlexibleSpace() either.

    In case it matters, I need the Rect so I can test dropping items with drag and drop against the screen-space mouse point coords. That works fine already everywhere else, but I at least need a Rect.

    Thanks in advance!
     
  2. AngryAnt

    AngryAnt

    Keyboard Operator Moderator

    Joined:
    Oct 25, 2005
    Messages:
    2,948
  3. Molix

    Molix

    Member

    Joined:
    Apr 24, 2009
    Messages:
    90
    I have never had any luck with that, and when I was writing this yesterday I noticed it is not in the docs, so I assumed it was broken and had been discretely removed.

    http://unity3d.com/support/documentation/ScriptReference/GUILayoutUtility.html

    I'll give it a try and let you know.

    Thanks!

    [Update: Yep, that seems to work perfectly. Now if I can just manage to not obsess over why it didn't work before, I can try to remember all the things I worked around when it didn't :) That will be so useful--thanks!]
     
  4. AngryAnt

    AngryAnt

    Keyboard Operator Moderator

    Joined:
    Oct 25, 2005
    Messages:
    2,948
    The fact that its missing from the docs is a bug and its being corrected for the next version of Unity.
     
  5. dmitryb

    dmitryb

    New Member

    Joined:
    Oct 21, 2009
    Messages:
    15
    And is there any function to get the INTERNAL area rect of scrollview?
    That is needed in my project to resize window dynamically when new text appears in it...
     
  6. dmitryb

    dmitryb

    New Member

    Joined:
    Oct 21, 2009
    Messages:
    15
    actually, it is - to use BeginRender and EndRender in the child windows functions.

    Code (csharp):
    1.  
    2.     protected Rect lastRect;
    3.  
    4.     public void BeginRender()
    5.     {
    6.         scrollPos = GUILayout.BeginScrollView(scrollPos);
    7.         GUILayout.BeginVertical();
    8.     }
    9.  
    10.     public void EndRender()
    11.     {
    12.         if (Event.current.type == EventType.repaint)
    13.         {
    14.             Rect r = GUILayoutUtility.GetLastRect();
    15.             lastRect.x = lastRect.y = 0;
    16.             lastRect.width = r.width;
    17.             lastRect.height = r.yMin;
    18.         }
    19.         GUILayout.EndVertical();
    20.         GUILayout.EndScrollView();
    21.     }
    22.