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.