Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Actual Rect for GUILayout.Scrollview?

Discussion in 'Immediate Mode GUI (IMGUI)' started by Molix, Oct 20, 2009.

  1. Molix

    Molix

    Joined:
    Apr 24, 2009
    Posts:
    92
    Hello,

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

    e.g.
    Code (csharp):
    1. scroller = GUILayout.BeginScrollView( scroller );
    2.   GUILayout.BeginVertical();
    3.      // dynamic run-time data drawn
    4.      // in various Verticals/Horizontals
    5.      // possibly with FlexibleSpace
    6.   GUILayout.EndVertical();
    7. GUILayout.EndScrollView();
    8.  
    9. Rect scrollArea = ?
    Or even:
    Code (csharp):
    1. GUILayout.BeginVertical( "Box" );
    2.   // various controls
    3. GUILayout.EndVertical();
    4.  
    5. 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

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    As I understand it, you need the size of the clipping rect of the scroll view - not the internal area, right? If this is the case, you can receive the rect like this:
    Code (csharp):
    1. scroller = GUILayout.BeginScrollView( scroller );
    2.   GUILayout.BeginVertical();
    3.      // dynamic run-time data drawn
    4.      // in various Verticals/Horizontals
    5.      // possibly with FlexibleSpace
    6.   GUILayout.EndVertical();
    7. GUILayout.EndScrollView();
    8.  
    9. Rect scrollArea = GUILayoutUtility.GetLastRect ();
     
    deadlyfingers likes this.
  3. Molix

    Molix

    Joined:
    Apr 24, 2009
    Posts:
    92
    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

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

    dmitryb

    Joined:
    Oct 21, 2009
    Posts:
    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

    Joined:
    Oct 21, 2009
    Posts:
    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.         GUILayout.Label(GUIContent.none, GUIStyle.none, GUILayout.MaxHeight(0), GUILayout.ExpandWidth(true));
    13.         if (Event.current.type == EventType.repaint)
    14.         {
    15.             Rect r = GUILayoutUtility.GetLastRect();
    16.             lastRect.x = lastRect.y = 0;
    17.             lastRect.width = r.width;
    18.             lastRect.height = r.yMin;
    19.         }
    20.         GUILayout.EndVertical();
    21.         GUILayout.EndScrollView();
    22.     }
    23.