Problem creating EditorWindow from Inspector

I’ve created a simple example of what I’m trying to achieve in my custom tool. I have a button in my inspector that when pressed, should open a custom EditorWindow. It works, except that I see the following error in the log when I press the button:

ArgumentException: You can only call GUI functions from inside OnGUI.

I’ve seen several posts about this but no solution that seems to work in 2.6. Is there one? Here’s the code for relevent classes (C#);

Editor\TestEditor.cs

using UnityEngine;
using UnityEditor;

// custom inspector window for Tests
[CustomEditor(typeof(TestClass))]
public class TestEditor : Editor 
{
	private string Test;
	SerializedObject serializedObject;
	int testIndex = 0;
	string[] testList;
	
	public override void OnInspectorGUI () 
    {
		testList = GetList();
		
		Rect r = EditorGUILayout.BeginHorizontal ();
		testIndex = EditorGUILayout.Popup("  Test Item", testIndex, testList, EditorStyles.popup);
		if (GUILayout.Button("Edit...", EditorStyles.miniButtonLeft))
		{
			TestEditorWindow.Init();
		}
		if (GUILayout.Button("Create...", EditorStyles.miniButtonRight))
		{
//			WizardCreateTest.ShowWindow();
		}
		//GUILayout.Label ("");
		EditorGUILayout.EndHorizontal();
    }
	
	protected void OnEnable() 
    {
		testIndex = 0;
        serializedObject = new SerializedObject(target);
        serializedObject.Update();
    }	
	
	protected void OnDisable() 
    {

    }
	
	private string[] GetList()
	{
		string[] testList = new string[4];
		testList[0] = "Test0";
		testList[1] = "Test1";
		testList[2] = "Test2";
		testList[3] = "Test3";
		return testList;
	}
}

Editor\TestEditorWindow.cs

using UnityEngine;
using UnityEditor;

public class TestEditorWindow : EditorWindow 
{
	string myString = "Hello World";
	bool groupEnabled;
	bool myBool = true;
	float myFloat = 1.23f;

	// Add menu named "My Window" to the Window menu
	[MenuItem ("Window/My Window")]
	public static void Init () 
	{
		// Get existing open window or if none, make a new one:
		TestEditorWindow window = (TestEditorWindow)EditorWindow.GetWindow (typeof (TestEditorWindow));
		window.Show ();
	}

	void OnGUI () 
	{
		GUILayout.Label ("Base Settings", EditorStyles.boldLabel);
		myString = EditorGUILayout.TextField ("Text Field", myString);

		groupEnabled = EditorGUILayout.BeginToggleGroup ("Optional Settings", groupEnabled);
		myBool = EditorGUILayout.Toggle ("Toggle", myBool);
		myFloat = EditorGUILayout.Slider ("Slider", myFloat, -3, 3);
		EditorGUILayout.EndToggleGroup ();
	}
}

As you can see, I’d also like to create a WizardWindow here as well, which is giving me similar results. Any help is truly appreciated.
Thanks,
Dan.


Hi, welcome to the forum!

You can only use EditorGUI/EditorGUILayout functions in the OnInspectorGUI function and similarly only GUI/GUILayout functions in OnGUI. You shouldn’t attempt to mix the two.

Thanks for the info, but what is the solution? How can I open an EditorWindow or Wizard from the Inspector? I really appreciate the help on this.

BTW, in case it wasn’t obvious, the offending piece of code is:

 TestEditorWindow.Init();

If I comment that out, I get no errors.
Thanks,
Dan.

total ignorant question but why do you use a distinct windows at all, why not “tabs” and switch between sub editors in your editor?
wouldn’t that be more convenient anyway than popup - popaway messing? :slight_smile:

Based on the amount of screen real estate the tool window will take up as well as what it’s being used for, having a seperate window just makes the most sense. The example here is obviously not the tool itself, but just an example of the problem. I didn’t want to post a bunch of stuff unrealeted to the problem I’m having with the tool, so I put this very simplified example together based on the Unity documentation.
Thanks,
Dan.

I asked the same question about 10 months ago but got no responses. Today I thought I’d try again, but still no luck.

You can use GUILayout in the editor, and in some cases you must (e.g. buttons, text fields that update properly). Perhaps this is what is causing us confusion – it works elsewhere but not here?

Hi,

you can add GUIUtility.ExitGUI(); right after Init. Then exception will not be shown.
It looks like a bug.

Yep, that did the trick. Thanks so much.