Search Unity

Unexplained GUILayout: Mismatched issue, is it a Unity bug or a miss understanding?

Discussion in 'Editor & General Support' started by drastick, Nov 13, 2012.

  1. drastick

    drastick

    Joined:
    Aug 16, 2011
    Posts:
    12
    View attachment $BugTest.unitypackage

    I've attached a stripped down repro project that I'm trying to understand why it is an issue.

    In this case I'm getting "ArgumentException: GUILayout: Mismatched LayoutGroup.mouseDown" and it is tied to having a HorizontalLayout. In my real project code not matter where I move it around I get random errors related to Mismatched LayoutGroup.

    In my attached scenario, I have a game object that has a script attached that I want to load some data from the web. Here I'm just loading google.com for the sample. I then have a custom inspector that I want to use for debugging so I can force a load at any point I desire. So I keep a handle around so I know the point that I trigger it in the inspector and that I can change the inspector state once it has loaded.

    Somehow all these problems are seeming related to that code that that is awaiting the game object to change state on an object the inspector holds a reference to.

    TestInspector.cs
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System.Collections;
    5.  
    6. [CustomEditor(typeof(Test))]
    7. public class TestInspector : Editor
    8. {
    9.     RequestHandle _handle = null;
    10.     bool _loaded = false;
    11.    
    12.     //Example inspector, the idea is that we can trigger loading www data for the object
    13.     //in question via the editor a debug method.
    14.     public override void OnInspectorGUI()
    15.     {
    16.         Test test = target as Test;
    17.         if(test != null)
    18.         {
    19.             if (!_loaded  GUILayout.Button("Load"))
    20.             {
    21.                 _handle = test.Load();
    22.             }
    23.            
    24.             if(_handle != null  _handle.status != RequestHandle.Status.Pending)
    25.             {
    26.                 //Done...
    27.                 _loaded = true;
    28.             }          
    29.            
    30.             if(_loaded)
    31.             {
    32.                 //* Uncomment first two of this to swtich logic to working version
    33.                 EditorGUILayout.BeginHorizontal();
    34.                 {
    35.                     //Everything falls apart with GUI mismatch errors... Why?!
    36.                     //In this case it is ArgumentException: GUILayout: Mismatched LayoutGroup.mouseDown
    37.                     //but using this same logic in acutal code causes all sorts of random results.
    38.                     EditorGUILayout.LabelField("Loaded!");
    39.                 }
    40.                 EditorGUILayout.EndHorizontal();
    41.                
    42.                 /*/
    43.                 EditorGUILayout.LabelField("Loaded!");
    44.                 /**/
    45.             }
    46.         }
    47.     }
    48. }
    49.  
    Update from Test.cs
    Code (csharp):
    1.  
    2.     void Update ()
    3.     {
    4.         Request [] list = _wwwRequestList.ToArray();
    5.         foreach(Request request in list)
    6.         {
    7.             if(request.www != null)
    8.             {
    9.                 if(request.www.error != null)
    10.                 {
    11.                     if(Application.isEditor) Debug.LogWarning(request.www.error);
    12.                    
    13.                     request.handle.status = RequestHandle.Status.Failed;
    14.                     request.handle.error = request.www.error;
    15.                    
    16.                     _wwwRequestList.Remove(request);
    17.                     request.www.Dispose();
    18.                     request.www = null;
    19.                 }
    20.                 else if(request.www.isDone)
    21.                 {
    22.                     request.handle.status = RequestHandle.Status.Succeeded;
    23.                    
    24.                     _wwwRequestList.Remove(request);
    25.                     request.www.Dispose();
    26.                     request.www = null;
    27.                 }      
    28.             }
    29.         }
    30.     }
    31.  
    32.  
    If you remove the Horizontal layout from the equation everything works as expected, however in my case I really want to make my GUI layed out with one.

    Even more crazy, I'm finding that just setting _loaded will result in this behavior.
    Code (csharp):
    1.         if(_handle != null  _handle.status != RequestHandle.Status.Pending)
    2.             {
    3.                 _handle = null;
    4.                 _loaded = true;
    5.             }
    6.            
    7.             if(_handle == null)
    8.             {  
    9.                 //* Uncomment first slash of this to swtich logic to working version
    10.                 EditorGUILayout.BeginHorizontal();
    11.                 {
    12.                     //Everything falls apart with GUI mismatch errors... Why?!
    13.                     //In this case it is ArgumentException: GUILayout: Mismatched LayoutGroup.mouseDown
    14.                     //but using this same logic in acutal code causes all sorts of random results.
    15.                     EditorGUILayout.LabelField("Loaded!");
    16.                 }
    17.                 EditorGUILayout.EndHorizontal();
    18.                
    19.                 /*/
    20.                 EditorGUILayout.LabelField("Loaded!");
    21.                 /**/
    22.             }
    Now if I remove the _loaded = true; line it work.What is going on behind the scenes here that makes this logic so touchy?
    Code (csharp):
    1.  
    2.         if(_handle != null  _handle.status != RequestHandle.Status.Pending)
    3.             {
    4.                 _handle = null;
    5.             }
    6.            
    7.             if(_handle == null)
    8.             {  
    9.                 EditorGUILayout.BeginHorizontal();
    10.                 {
    11.                     //Works now that _loading = true; is gone! But not the logic I want...
    12.                     EditorGUILayout.LabelField("Loaded!");
    13.                 }
    14.                 EditorGUILayout.EndHorizontal();
    15.                
    16.             }
     
  2. UnLogick

    UnLogick

    Joined:
    Jun 11, 2011
    Posts:
    1,745
    This is a very common conceptual misunderstanding with immediate mode gui. It derives from the fact that OnGUI and OnInspectorGUI is called multiple times per frame. You can look at Event.current to see why OnGUI is called this time.

    http://docs.unity3d.com/Documentation/ScriptReference/EventType.html

    So each frame Unity starts with a Layout, does some Events(this is where your button code is triggered) and a Repaint. However the number and type of controls in the Repaint doesn't match the Layout and thats the problem.

    What you're doing is adding a new gui control in the middle of a GUI pass. In general you should use the EventType.Layout for adding and removing controls.

    Code (csharp):
    1. if (Event.current.type == EventType.Layout  _handle != null  _handle.status != RequestHandle.Status.Pending)
    2. {
    3.     //Done...
    4.     _loaded = true;
    5. }
    So if you change _loaded(which effectively changes the gui layout) only when processing the Layout event everything should work as intended.
     
    Last edited: Nov 13, 2012
  3. drastick

    drastick

    Joined:
    Aug 16, 2011
    Posts:
    12
    Very insightful! Thank you.
     
  4. Ashkan_gc

    Ashkan_gc

    Joined:
    Aug 12, 2009
    Posts:
    1,124
    Actually the problem is that the documentation doesn't state it clearly in manual and people need to look deep in class docs to learn about this. Note that i'm a unity user from 2.5 era and know it quite well. the time when i read manual in 2.5 era it wasn't stated there and it's easy to miss the detait that you should only change layout in layout event only after many months when you read class documentation however if you learn it as a concept in manual it will stay there.
     
  5. SuperSpasm

    SuperSpasm

    Joined:
    Aug 15, 2016
    Posts:
    8
    I realize this is an old thread, but thought I'd share my solution for random googlers since the above (sampling EventType) did not work for me.

    As it turns out, custom Property Drawers do not play well with EditorGUILayout.
    I was baffled for a long time by this since I had a custom property drawer with EGL that worked fine as a field in one script, but completely broke another. testing EventType, implementing GetPropertyHeight in various ways, and removing various parts of my logic did not help.

    In the end, it turns out the script that was working, was using a custom inspector, which for some unapparent reason makes property drawers with EGL work like a charm.

    To test this, you can do the following (verified on 2017.3):
    1. create a class (lets call it Foo) with a with a single public field (make sure its serializable)
    2. create a custom property drawer, expose that field via EditorGUILayout.PropertyField
    3. create a MonoBehaviour (FooHolder), which has a single "public Foo foo;"
    4. place FooHolder on GameObject- see it is broken.
    5. now, create a Custom Inspector for FooHolder- voila, it works. (No need to even implement OnInspectorGUI!)
    6. without the Custom inspector, FooHolder will display correctly if you:
    • switch to EditorGUI.PropertyField
    • implement GetPropertyHeight


    TL;DR- Custom Property Drawers do not work with EditorGUILayout, unless the MonoBehaviour showing them has a custom proerty drawer
     
    bac9-flcl, Rallix, kodo91 and 3 others like this.
  6. BradZoob

    BradZoob

    Joined:
    Feb 12, 2014
    Posts:
    66
    LOL it's 2019 and this is still true, my god Unity docs are insane, I mean i get that programmers hate documenting but daaaaaamn. I saw a full page doc for a method the other day, something like "GetBespokeProperty()" and the docs were literally "Gets the Bespoke Property", so clearly there's no passion for documenting over there, which is weird because some people _love_ bringing order to chaos, they should just find one of those to do it.
     
    jmunozar likes this.
  7. Jeff-Kesselman

    Jeff-Kesselman

    Joined:
    Apr 5, 2010
    Posts:
    99
    So this is a huge frustration because it means I cant act on a dropped object on a drag and drop event but have to cache it and wait for the next layout.

    Thats just ugly.
     
  8. Haytham_Kenway

    Haytham_Kenway

    Joined:
    Mar 22, 2019
    Posts:
    2
    How do I add a custom property drawer?