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

BeginGroup and RotateAroundPivot doesnt work properly

Discussion in 'Immediate Mode GUI (IMGUI)' started by pointer, May 10, 2011.

  1. pointer

    pointer

    Joined:
    Oct 9, 2010
    Posts:
    7
    Hello,

    I have the following code snippet:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class BeginGroupTest : MonoBehaviour
    6. {
    7.                 Texture2D rotateTexture;
    8.                 Texture2D backgroundTexture;
    9.  
    10.                 void Start ()
    11.                 {
    12.                                // ignore this
    13.                                rotateTexture = JUtils.CreateTexture(20,100, Color.blue);
    14.                                backgroundTexture = JUtils.CreateTexture(200, 200, Color.red);
    15.                 }
    16.  
    17.                 void OnGUI () {
    18.  
    19.                                GUI.BeginGroup(new Rect(100, 0, 200, 200));
    20.                                Matrix4x4 restore = GUI.matrix;
    21.                                GUI.Box(new Rect(0, 0, 200, 200), backgroundTexture);
    22.                                GUIUtility.RotateAroundPivot(90, new Vector2(50, 50));
    23.                                GUI.DrawTexture(new Rect(50, 50, 20, 100), rotateTexture);
    24.                                GUI.matrix = restore;
    25.                                GUI.EndGroup();
    26.                 }
    27. }
    28.  
    Well i have a grouped area with a texture which im rotating and i thought it would be clipped off, if the rotated texture is
    beyond the grouped area, but it doesnt. Rotate works great, but the clipping fails. Am i wrong or is this a bug?
    Any idea?
    I dont wanna use Rendertexture or GuiTexture as improvements. Thanks :)
     
  2. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,605
    I know this is a very old post but this problem is still happening. It seems BeginGroup clips the GUI before RotateAroundPivot is applied. So when any element is rotated, it will stick out of the group's clipping area. The rotated element will also start clipping off along its top flat edge as it nears the top of the containing group even though the edge is angled and still within the group. It basically makes rotating GUI elements unusable in any clipped area including scroll views.

    Still broken in 4.3. For crying out loud Unity, people have been reporting this issue since 2009 and was claimed to be fixed in 2010. Sigh...
     

    Attached Files:

    Last edited: Dec 2, 2013
  3. RamzaB

    RamzaB

    Joined:
    Nov 4, 2012
    Posts:
    42
    I still have this issue right now on Unity 4.2

    If it isn't going to be fixed, is there anyway we could achieve rotation without using GUIUtility.RotateAroundPivot() ?
     
  4. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,605
    Not that I know of ... at least not for editor windows. If you're doing in-game GUI, obviously there are a lot of other solutions.

    In my case, the workaround was to draw the box pre-rotated to a temporary Texture2D every frame, then draw that to the scroll view without any GUI rotation. So what I have is a large rect that can contain the rotated box that gets clipped as expected. However, this method is quite slow and I was forced to draw the rect quite small and scale up to fill the area because rotating even a relatively big (400x400) rect this way was REALLY slow. So I ended up with a rather pixelated rotated box. It's also not great for garbage collection being that you create and toss Texture2D's every frame. (I believe there also is a small memory leak in Unity related to this process, but its small enough to never really affect my tool.)

    Just another case of Unity prioritizing new features to attract newcomers over making existing ones work properly. This is one of MANY issues that have lingered for years upon years. Their lack of interest in making properly working tools in favor of bullet points is progressively becoming a bigger and bigger problem as time passes. Unity is starting to leave a sour taste in my mouth after all these years.
     
    Last edited: Mar 31, 2014
  5. RamzaB

    RamzaB

    Joined:
    Nov 4, 2012
    Posts:
    42
    Hm, I am doing an outside-game tool, that's too bad.
    Your idea sounds nice, but I think I'll avoid it if it's slow, because I have a lot of rotated rects at the same time.

    Maybe there will be a newer version of the editor which will come in 5.0 and the Unity team is focusing on that ?

    But thinking about it, this issue CAN'T be that hard to fix, can it ?
     
  6. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,605
    Unless they've specifically said there will be a new version of the editor GUI system, which I haven't heard a thing about, don't count on it. There will be a new in-game GUI system roughly based on NGUI coming, but I haven't heard anything about editor GUI changes. They never make major changes like that without a lot of fanfare. And no, it can't be that hard to fix, but they'll likely never get around to it regardless. Just like a bunch of other forgotten bugs and incomplete features that they never get around to addressing many years after acknowledging the problems. I've been waiting 5 years for a joystick bug fix that would take all of 5 minutes to do... grr... and that's a FAR more important system than editor GUI yet it gets zero attention. I swear, as a tool developer, I'd have a zero-star rating in the asset store if I ignored bugs like Unity does.
     
    Last edited: Apr 11, 2014
  7. hedgehog90

    hedgehog90

    Joined:
    May 18, 2016
    Posts:
    27
    Any updates on this?
    I'm simply trying to draw a rotated label (ala LayerMatrixGUI in the Physics Inspector) within a ScrollView, and I'm encountering this clipping problem.
     
  8. wechat_os_Qy06wDCHIkyinNVZF11ZU9JYw

    wechat_os_Qy06wDCHIkyinNVZF11ZU9JYw

    Joined:
    Dec 8, 2019
    Posts:
    2
    So many years has passed, the problem still exists
     
  9. PeGiannOS

    PeGiannOS

    Joined:
    Nov 30, 2016
    Posts:
    2
    I am facing the same problem in Unity LTS 2021.3.6f1. I am using GUIUtility.RotateAroundPivot to rotate a simple LabelField inside a custom Editor Window. The label is inside a rectangular area (A) inside the window. The area outside that is filled with a color (B). What is strange is that B area clips the label when i pan it over it, whilst label appears on top of the editor window title bar when i pan it there. Is there a way to redraw the title bar without repainting and thus calling OnGUI() again? Or an update about GUIUtility.RotateAroundPivot? Can we use a new BeginGroup somehow after calling it to restore clipping?
     
  10. PeGiannOS

    PeGiannOS

    Joined:
    Nov 30, 2016
    Posts:
    2
    UPDATE!!!!!!

    Found a work around:

    First add this line on top of your OnGUI(). Don't worry about any errors, the next code should fix it. It doesn't throw anything to me.
    Code (CSharp):
    1. GUI.EndClip()
    Code (CSharp):
    1.  
    2.         DrawTitleBar();
    3.    
    4.         Rect clippingRect = new Rect(0, 0, Screen.width, Screen.height);
    5.         Vector2 scrollOffset = new Vector2(0, 0);
    6.         Vector2 renderOffset = new Vector2(0, 21f);
    7.         GUI.BeginClip(clippingRect, scrollOffset, renderOffset, false);
    Where DrawTitleBar redraws the title bar like this (grab the icons from https://github.com/halak/unity-editor-icons)

    Code (CSharp):
    1. void DrawTitleBar()
    2.     {
    3.         Rect titleBarRect = new Rect(0, 0, currentWindow.position.width, 21f);
    4.  
    5.      
    6.         //GUI.EndGroup();
    7.         GUI.BeginGroup(titleBarRect);
    8.  
    9.         //Draw Title Bar Background
    10.         EditorGUI.DrawRect(titleBarRect, softBlack);
    11.  
    12.         EditorGUILayout.BeginHorizontal();
    13.         //Draw Title name
    14.         var titleSize = GUI.skin.label.CalcSize(new GUIContent("Title Name"));
    15.         Rect titleBarNameRect = new Rect(0, 0, titleSize.x + 16, 21f);
    16.         Rect titleBarNameTitleRect = new Rect(5, 0, titleSize.x + 16, 21f);
    17.         EditorGUI.DrawRect(titleBarNameRect, softGray);
    18.         GUIStyle myTextAreaStyle = new GUIStyle(EditorStyles.label);
    19.         Handles.color = softRed;
    20.         Handles.DrawLine(new Vector2(0, 1), new Vector3(titleSize.x + 15, 1));
    21.         //myTextAreaStyle.alignment = note.alignment;
    22.         myTextAreaStyle.normal.textColor = softWhite;
    23.         GUILayout.BeginArea(titleBarNameTitleRect);
    24.      
    25.         EditorGUILayout.LabelField("Title Name", myTextAreaStyle);
    26.         GUILayout.EndArea();
    27.  
    28.         Rect middleRect = new Rect(titleBarNameRect.width + 1, 0, currentWindow.position.width - titleBarNameRect.width - 52, 21f);
    29.         EditorGUI.DrawRect(middleRect, softBlack);
    30.  
    31.         GUIStyle titleBarButtonStyle = new GUIStyle(EditorStyles.iconButton);
    32.         titleBarButtonStyle.alignment = TextAnchor.UpperCenter;
    33.  
    34.         Rect minimizeRect = new Rect(currentWindow.position.width + 1 - 52, 0, 16, 21f);
    35.         Rect maximizeRect = new Rect(currentWindow.position.width + 1 - 35, 0, 16, 21f);
    36.         Rect closeRect = new Rect(currentWindow.position.width + 1 - 18, 0, 16, 21f);
    37.         GUI.Button(minimizeRect, minimizeIcon.texture, titleBarButtonStyle);
    38.  
    39.         // currentWindow ? restoreIcon.texture : maximizeIcon.texture;
    40.      
    41.         if (currentWindow.position.width == Screen.currentResolution.width) maxIcon = restoreIcon.texture;
    42.         else maxIcon = maximizeIcon.texture;
    43.  
    44.         GUI.Button(maximizeRect, maxIcon, titleBarButtonStyle);
    45.      
    46.      
    47.         GUI.Button(closeRect, closeIcon.texture, titleBarButtonStyle);
    48.  
    49.         EditorGUILayout.EndHorizontal();
    50.  
    51.         GUI.EndGroup();
    52.     }
    Attention! This is important part. After doing all this, you will notice that the titlebar is ok, but everything underneath needs to be moved by 21f pixels (titlebar's height) down. So after the first GUI.EndClip() and before the DrawTitlebar() call put this two lines:

    Code (CSharp):
    1.  
    2. //Right after GUI.EndClip()
    3. GUI.BeginGroup(new Rect(0,21f,Screen.width,Screen.height));
    4.  
    5. //Your drawing part underneath titlebar
    6.  
    7. //Right before DrawTItlebar()
    8. GUI.EndGroup();
    Hope this helps!
     
    Last edited: Aug 13, 2022
  11. warpfx

    warpfx

    Joined:
    Feb 10, 2020
    Posts:
    14