Search Unity

Resolved Avoiding you getting insane because of "Invalid GUILayout state" errors

Discussion in 'Immediate Mode GUI (IMGUI)' started by Aka_ToolBuddy, Apr 26, 2023.

  1. Aka_ToolBuddy

    Aka_ToolBuddy

    Joined:
    Feb 25, 2014
    Posts:
    543
    Edit 1:
    The original post described a solution, let's call it SA.
    Since the original post, I found the real reason behind my issues: Not respecting the constraint where, in OnGUI, drawing calls (GUI/GUILayout) should be invoked in the same order for both Repaint and Layout events.
    Reworking my code to respect this contraint solved my issue. Let's call this solution SB.
    I keep SA in this post for reference, but please ignore it and use SB instead. Plus, now that I implemented SB, there seems to be issues created by conflicts between Unity using ExitGUIException and SA putting the End methods in a finally block.

    Original post:

    Hi,
    I finally found the reason behind these errors that would pop in one of my assets. I hope this post will save you time and sanity if you encounter the same errors.

    The error:
    The full error message is as follows:

    GUI Error: Invalid GUILayout state in <my_class> view. Verify that all layout Begin/End calls match
    UnityEngine.GUIUtility:processEvent (int,intptr,bool&)


    It happened at multiple occasions, but let's focus on one of them: when I open a material selector in my custom editor.

    Why it is driving insane:
    The error message explains that I am using a GUILayout.BeginX without following it with the corresponding GUILayout.EndX. Same for EditorGUILayout.

    My code had no such mismatches. I first checked visually all call sites. nothing wrong. I even used methods such as this one to avoid accidental mismatches that I might have missed:
    Code (CSharp):
    1. public static void Horizontal(Action action, GUIStyle style)
    2.         {
    3.             GUILayout.BeginHorizontal(style);
    4.             action();
    5.             GUILayout.EndHorizontal();
    6.         }
    The issue still happened.
    The only scenario I can think of that would cause the issue is:
    The EndX method is not called because of an exception thrown between the BeginX and EndX calls. But if such thing happened, the console would let me know that an exception was thrown, right? ... Right?


    The cause:
    Some Unity UI components throw ExitGUIException exceptions as part of their regular, non exceptional, code flow. For example, each time you open a material selector, that exception is thrown. This exception type is NOT shown in the console.
    A notable place where this exception is thrown is: GUIUtility.ExitGUI(), which implementation is literally just the exception being thrown.


    The fix:
    Put the End method in a finally block
    Code (CSharp):
    1. public static void Horizontal(Action action, GUIStyle style)
    2.         {
    3.             GUILayout.BeginHorizontal(style);
    4.             try
    5.             {
    6.                 action();
    7.             }
    8.             finally
    9.             {
    10.                 GUILayout.EndHorizontal();
    11.             }
    12.         }

    Have a nice day

    PS: Here is the tracker for the issue I opened at the time:
    https://issuetracker.unity3d.com/is...alid-guilayout-state-when-using-beginvertical
     
    Last edited: Mar 29, 2024
    Graham-B, hanaom, denizerturk and 2 others like this.
  2. AndreiMarian

    AndreiMarian

    Joined:
    Jun 9, 2015
    Posts:
    77
    "GUI Error: Invalid GUILayout state in BuildPlayerWindow view", aka the build window of Unity :)
     
    brunogattai1996 likes this.
  3. atr0phy

    atr0phy

    Joined:
    Nov 5, 2014
    Posts:
    43
    I'm getting insane because my 2022.3 editor is currently nigh unusable due to a similar issue, though the invalid state isn't in any of my classes, it's in the ConsoleWindow class itself. In a completely fresh project, whose only assets consist of a few scripts I've started writing, I'm getting spammed hundreds of null ref exceptions along with that invalid state error and a "you are pushing more gui clips than you are popping" error. Even more annoying, I can't even click the null ref exceptions to see the stack traces because the console freezes up entirely when attempting to interact with it in any way.

    None of my scripts have anything to do with the editor in any way. So this is purely a Unity bug. And, unfortunately, this is the closest thread I could find related to this issue.
     
    lcyyyyyyyy and Arty_Brest like this.
  4. Aka_ToolBuddy

    Aka_ToolBuddy

    Joined:
    Feb 25, 2014
    Posts:
    543
    I too noticed what seems like these errors coming from Unity's editor/official packages :(
     
    brunogattai1996 and Arty_Brest like this.
  5. Noitceffa

    Noitceffa

    Joined:
    Sep 13, 2019
    Posts:
    4
    This type of error appears in my Unity 2022.3 project also. There's also several other errors that appear when using different tools from asset store. Guess one should wait at least two years before starting to use latest Unity LTS versions, since external assets aren't compatible anymore and really makes using latest version worth less than utter S***. Guess the best solution is to nerf project to an older version.
     
    brunogattai1996 likes this.
  6. fortranmapleperl78

    fortranmapleperl78

    Joined:
    Aug 6, 2023
    Posts:
    4
    getting this now in Plastic Window View - how to view/resolve? thx
     
  7. Aka_ToolBuddy

    Aka_ToolBuddy

    Joined:
    Feb 25, 2014
    Posts:
    543
    I don't have a solution. My post was about how to avoid the issue for editors (windows) you code yourself.
     
  8. lcyyyyyyyy

    lcyyyyyyyy

    Joined:
    Nov 24, 2022
    Posts:
    4
    I also had the same problem. I followed the official website unity learn, but I couldn't proceed.
    https://learn.unity.com/tutorial/te...uv=2022.1&projectId=6448869fedbc2a0569dfdaeb#
    this is driving me crazy
     
  9. tobario

    tobario

    Joined:
    Jun 12, 2022
    Posts:
    2
    I could resolve it just by reinstalling one of my Assets. In this case Master Audio. The Editor scripts of that Asset suddenly began to throw this error. Unity 2022.3.16f1.