Search Unity

GUI.BeginGroup() Without Clipping

Discussion in 'Immediate Mode GUI (IMGUI)' started by orionburcham, Jul 12, 2014.

  1. orionburcham

    orionburcham

    Joined:
    Jan 31, 2010
    Posts:
    488
    Hi. I'm creating an EditorWindow in which I'm drawing a lot of GUI boxes. I'd like to offset all my boxes by a specific x and y amount. For example, a box that normally starts at (0, 0) would now start at (0 + xOffset, 0 + yOffset).

    Seems like this simplest way to do this would be to modify GUI.matrix to automatically give everything this offset. This works, but it automatically clips anything with a negative value (which is something I'm doing often).

    GUI.BeginGroup() and GUILayout.BeginArea() also do this sort of clipping, which I need to avoid. I'm concerned that manually adding an offset to each Box's rect before drawing it will have a noticeable performance impact, and my tests are backing that up.

    Is there any way to disable clipping when working with GUI.matrix, GUI.BeginGroup(), or GUILayout.BeginArea()? This would be very helpful. Thanks for any tips!

    Orion
     
  2. Deni35

    Deni35

    Joined:
    Jul 10, 2012
    Posts:
    43
    You can try this:
    Code (csharp):
    1.  
    2.         private static void BeginClip(Rect rect, Matrix4x4 matrix) {
    3.             rect.min = matrix.inverse.MultiplyVector( rect.min );
    4.             rect.max = matrix.inverse.MultiplyVector( rect.max );
    5.  
    6.             GUI.matrix = matrix;
    7.             BeginWindowClip();
    8.             GUI.BeginClip( rect );
    9.         }
    10.  
    11.         private static void EndClip() {
    12.             GUI.EndClip();
    13.             EndWindowClip();
    14.             GUI.matrix = Matrix4x4.identity;
    15.         }
    16.  
    17.  
    18.         private static void BeginWindowClip() {
    19.             Rect rect = Rect.MinMaxRect( 1, 18, Screen.width, Screen.height );
    20.             rect.min = GUI.matrix.inverse.MultiplyPoint( rect.min );
    21.             rect.max = GUI.matrix.inverse.MultiplyPoint( rect.max );
    22.  
    23.             // End the group Unity begins automatically for an EditorWindow to clip out the window tab. This allows us to draw outside of the size of the EditorWindow.
    24.             GUI.EndClip(); // scroll area
    25.             GUI.EndClip(); // base group
    26.             GUI.BeginClip( rect );
    27.         }
    28.  
    29.         private static void EndWindowClip() {
    30.             GUI.EndClip();
    31.             GUI.BeginClip( new Rect( 2, 18, Screen.width, Screen.height ) ); // base group
    32.             GUI.BeginClip( new Rect( 0, 0, Screen.width, Screen.height ) ); // scroll area
    33.         }
    34.  
    But in inspector it has problem with scrolling.
    In EditorWindow it must work but you have to remove 'scroll area'.
     
    deadpixel7 likes this.