• 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
  • ‹
  • 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