• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Zogg · Jan 22, 2013 at 05:48 AM · exceptionscritping

EditorGUILayout.ColorField inside GUILayout.Window causes ExitGUIException

I'm coding an EditorWindow. My problem is, when I use a EditorGUILayout.ColorField inside a GUILayout.Window, an ExitGUIException occurs when I click onto the color field.

I was able to boil down the problem to the following script:

 using UnityEngine;
 using UnityEditor;
 
 public class TestEditor : EditorWindow 
 {
     Color mColor = Color.green;
     
     [MenuItem ("Test/TestEditor")]
     static void Init() 
     {
         EditorWindow.GetWindow( typeof( TestEditor ) );        
     }
     
     public void OnGUI () 
     {
         BeginWindows();
         GUILayout.Window ( 7, new Rect( 100, 100, 300, 50), DoWindow, "Window" );
          EndWindows();
     }
 
     void DoWindow( int id )
     {
         mColor = EditorGUILayout.ColorField("Click here -->", mColor);
     }
 }


When I open the test editor and click on the color field, the following error occurs:

ExitGUIException: Exception of type 'UnityEngine.ExitGUIException' was thrown. UnityEngine.GUIUtility.ExitGUI () (at C:/BuildAgent/work/812c4f5049264fad/Runtime/ExportGenerated/Editor/GUIUtility.cs:95) UnityEditor.EditorGUI.DoColorField (Rect position, Int32 id, Color value, Boolean showEyedropper, Boolean showAlpha) (at C:/BuildAgent/work/812c4f5049264fad/Editor/MonoGenerated/Editor/EditorGUI.cs:2222) UnityEditor.EditorGUI.ColorField (Rect position, UnityEngine.GUIContent label, Color value) (at C:/BuildAgent/work/812c4f5049264fad/Editor/MonoGenerated/Editor/EditorGUI.cs:2201) UnityEditor.EditorGUI.ColorField (Rect position, System.String label, Color value) (at C:/BuildAgent/work/812c4f5049264fad/Editor/MonoGenerated/Editor/EditorGUI.cs:2198) UnityEditor.EditorGUILayout.ColorField (System.String label, Color value, UnityEngine.GUILayoutOption[] options) (at C:/BuildAgent/work/812c4f5049264fad/Editor/MonoGenerated/Editor/EditorGUI.cs:3834) TestEditor.DoWindow (Int32 id) (at Assets/Editor/TestEditor.cs:23) UnityEngine.GUILayout+LayoutedWindow.DoWindow (Int32 windowID) (at C:/BuildAgent/work/812c4f5049264fad/Runtime/ExportGenerated/Editor/GUILayout.cs:317) UnityEngine.GUI.CallWindowDelegate (UnityEngine.WindowFunction func, Int32 id, UnityEngine.GUISkin _skin, Int32 forceRect, Single width, Single height, UnityEngine.GUIStyle style) (at C:/BuildAgent/work/812c4f5049264fad/Runtime/ExportGenerated/Editor/GUI.cs:1023) UnityEditor.EditorWindow:BeginWindows() TestEditor:OnGUI() (at Assets/Editor/TestEditor.cs:16) UnityEditor.DockArea:OnGUI()

The error does not occur if I write the EditorGUILayout.ColorField simply into OnGUI instead of a window. Also, other fields like EditorGUILayout.CurveField or EditorGUILayout.FloatField don't cause any problems.

What am I doing wrong?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image chabala · Mar 13, 2013 at 11:38 PM 0
Share

I am having this same problem. However the curve is giving me issues as well. Have you found the answer?

avatar image jlu7 · May 14, 2013 at 12:50 AM 0
Share

Same problem here as well!

6 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by sisus_co · Sep 16, 2019 at 07:08 AM

I just did a deep dive into investigating the ExitGUIException and thought I'd share my findings here.

Unity's GUIUtility has a method called ExitGUI that looks like this:

 public static void ExitGUI()
 {
     GUIUtility.guiIsExiting = true;
     throw new ExitGUIException();
 }


Unity's documentation on GUIUtility.ExitGUI states the following:

Use GUIUtility.ExitGUI in situations - - such as when a change in some value might change what controls are displayed next. Using this method can prevent errors such as ArgumentException: Getting control 0's position in a group with only 0 controls when doing Repaint.


So ExitGUIException is something that Unity intentionally throws in its internal code whenever it wants to exit out of GUI drawing code in the middle of it.
This usually happens when the user interacts with the GUI in some way, like clicks a button.

Unity will internally handle suppressing this exception silently without any errors appearing in the user's console, as long as you don't catch it yourself.

Here is how Unity's Inspector Window handles ExitGUIException:

 try
 {
     editor.OnInspectorGUI();
 }
 catch (Exception exception)
 {
     if (GUIUtility.ShouldRethrowException(exception))
     {
         throw;
     }
     Debug.LogException(exception);
 }


And here is what GUIUtility.ShouldRethrowException looks like:

 internal static bool ShouldRethrowException(Exception exception)
 {
     return GUIUtility.IsExitGUIException(exception);
 }
 
 internal static bool IsExitGUIException(Exception exception)
 {
     while (exception is TargetInvocationException && exception.InnerException != null)
     {
         exception = exception.InnerException;
     }
     return exception is ExitGUIException;
 }


You should do the same thing in your code. Whenever you have OnGUI logic inside a try-catch statement, you should check if the caught exception is an ExitGUIException, and if it is, rethrow it.

Unfortunately for some reason Unity has decided to make GUIUtility.ShouldRethrowException into an internal method, so you will have to replicate the method in your own code before you can use it.

You can do so by adding this code into your project inside a file called "ExitGUIUtility.cs":

 using System;
 using System.Reflection;
 using UnityEngine;
 
 public static class ExitGUIUtility
 {
     public static bool ShouldRethrowException(Exception exception)
     {
         return IsExitGUIException(exception);
     }
 
     public static bool IsExitGUIException(Exception exception)
     {
         while (exception is TargetInvocationException && exception.InnerException != null)
         {
             exception = exception.InnerException;
         }
         return exception is ExitGUIException;
     }
 }


You can then use it like this:

 try
 {
     mColor = EditorGUILayout.ColorField("Click here -->", mColor);
 }
 catch(Exception e)
 {
     if(ExitGUIUtility.ShouldRethrowException(e))
     {
         throw;
     }
     Debug.LogException(e);
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by buronix · Sep 27, 2016 at 10:39 AM

I needed to handle the exceptions, so my solution was:

 try
 {
         //Your Code
 }
 catch (Exception e)
 {
     if(e.GetType() != typeof(ExitGUIException))
         {
             //The exception is real, handle here
             Debug.LogException(e);
         }
     }
 }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

Answer by Glurth · Jul 12, 2016 at 07:03 PM

I unexpectedly ran into this error the other day, and found this post. But since the issue had NOTHING to do with try/catch blocks, I figured I'd share what it was, though probably a rare issue.

My problem seemed to have something to do with HOW my editor GUI function was invoked. In particular, I was using reflection to find a particular function that I wanted. The result was stored in a System.Reflection.MethodInfo variable.

If I called Invoke() on that variable, the exception was generated.

 method.Invoke(null, null);

However, if I converted the MethodInfo into a Delegate, and then called the delegate, the exception vanished.

 public delegate void PreferencesGUI();
 PreferencesGUI func = (PreferencesGUI)Delegate.CreateDelegate(typeof(PreferencesGUI), method);
 func();

Honestly, I'm not quite sure what the internal difference between these two invocation methods is, nor why it should generate an exception. Update: answered in bunny's comment below

@Bunny83 : thought you might find this interesting/good to know. Update: thank you!

[1]: http://stackoverflow.com/questions/4117228/reflection-methodinfo-invoke-catch-exceptions-from-inside-the-method

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jul 12, 2016 at 07:44 PM 2
Share

The answer to this is pretty simple ^^. You usually shouldn't receive a ExitGUIException in normal setups.

First of all lets look at where that exception comes from. It's actually raised on purpose by the GUIUtility.ExitGUI() method which literally just does this:

 public static void ExitGUI()
 {
     throw new ExitGUIException();
 }

So what's the point of this method / exception? The answer is simple. In some cases it's necessary to ter$$anonymous$$ate the current GUI execution to prevent either corruption or unpredictable results. This is usually necessary if you open a new window (actual standalone window, not a GUI.Window) from GUI code. It also might be necessary if the displayed content is changed during the execution and otherwise it might break the layout system. Since ColorField opens a seperate EditorWindow it uses ExitGUI when the field actually opens the window.

Usually Unity catches the exception itself silently. So in other words it's like OnGUI and OnInspectorGUI are implicitly wrapped in try-catch statements.

The reason why the exception isn't catched when using reflection is explained here on SO.

The reason in the original question seems to depend on the way how GUI.Windows are handled. The GUI.Window callbacks actually aren't called from the OnGUI method but later directly by Unity. It seems Unity somehow missed to catch the exception in this special case.

ExitGUI is usually ment to be only used internally by Unity since it's not documented. But since it's used implicitly inside documented methods you could rank this behaviour as bug.

avatar image
0

Answer by Seneral · Aug 05, 2015 at 08:08 PM

This is still bugging me. If you can, change the catch block so you just catch UnityException's:

 try
 {
 }
 catch (Exception e)
 {
     Debug.LogException(e);
 }
 
 // Instead of
 
 try
 {
 }
 catch (UnityException e)
 {
     Debug.LogException(e);
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
5

Answer by guavaman · Aug 29, 2013 at 12:58 AM

The problem: ExitGUIException thrown when clicking the color picker on a color field or the "Select" area on any object field.

Edit: Updated based on important information from @Bunny83's comments.

Unity throws this ExitGUIException EVERY time one of these controls is clicked. This exception is used by Unity and needs to be propagated. If you have wrapped the control in a try/catch statement (including upstream), it will catch the exception. If you don't have any try/catch statements, you'll never even see this exception thrown.

Any Try/Catch blocks around or upstream of a ColorField or ObjectField must propagate the ExitGUIException to allow Unity to receive it.

 try {
     // code
 } catch (UnityEngine.ExitGUIException) {
     throw;
 } catch (System.Exception e) {
     // Handle the exception
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Demigiant · Aug 17, 2016 at 10:15 AM 0
Share

This is super annoying and incredibly still unsolved. Try-catch is fundamental around big blocks of editor code imho, because that way you can have the fundamental parts still work in case an exception happens (so you can fix it via the visual editor itself and then work on the bug).

avatar image Bunny83 Demigiant · Aug 17, 2016 at 10:38 AM 1
Share

You can catch exceptions, but make sure the ExitGUIException will propergate through. The point of this exception is (as it's name suggests) to exit the current GUI code. This is required in some special cases. See my comment below Glurth answer.

In case of GUI,Window / GUILayout.Window i'm not sure if there's an actual workaround. You can try catching ExitGUIException right at the top of your window callback and swallow it. If it still throws an InvalidOperationException there's no workaround. The best thing you can do is let Unity catch the ExitGUIException and live with it. $$anonymous$$aybe they fix the issue some day ^^.

Though I'm sure that the priority of editor issues is lower than engine issues. Also the usage of GUI.Windows inside editor code is a very rare case. So depending on how complicated the fix might be i wouldn't bet it's fixed any time soon. ^^

If you think it's important that it get fixed, file a bug report yourself. $$anonymous$$ake sure you attach a sample project that reproduces the problem with some easy to follow instructions how to reproduce the problem. The more bug reports they get about a certain issue the more likely they will fix it :)

avatar image Demigiant Bunny83 · Aug 17, 2016 at 11:11 AM 0
Share

Thanks Bunny :) I found a workaround in the meantime, but it's still annoying. And by the way, it doesn't happen only with GUI.Windows, since I don't have any but I have the issue anyway. Still, not a biggie after all.

  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# Player moving as if he were on ice 0 Answers

looking for DOTS study materials 0 Answers

How to duplicate scene gizmo? 0 Answers

Setting up a key to cycle through editor modes 0 Answers

NullReferenceException help 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges