- Home /
Close()ing an EditorWindow
While trying to make a (rather complex) editor for a component, I decided that, instead of trying to cram everything into a custom inspector, to just make a simple one that creates a custom EditorWindow, like this:
function OnInspectorGUI () {
if (GUILayout.Button("Open Dialogue Editor")) {
var window:DialogueEditor = EditorWindow.GetWindow(DialogueEditor);
window.SetTarget(target);
window.Init();
}
}
And use this window to edit the target. However, if the target is deleted and the window is still open, it should close itself. For that purpose I wrote the following at the start of the window's OnGUI():
if (!target) {
this.Close();
return;
}
However, if I test it out by simply not setting the target, I get the following errors:
NullReferenceException: CalculateNextFromHintList accessing GUI state from destroyed monobehaviour
UnityEngine.IDList.GetNext (Int32 hint, FocusType focusType, Rect position) (at C:/BuildAgent/work/14194e8ce88cdf47/Runtime/ExportGenerated/Editor/GUIUtility.cs:66)
UnityEngine.IDList.GetNext (Int32 hint, FocusType focusType) (at C:/BuildAgent/work/14194e8ce88cdf47/Runtime/ExportGenerated/Editor/GUIUtility.cs:59)
UnityEngine.GUIUtility.GetControlID (Int32 hint, FocusType focus) (at C:/BuildAgent/work/14194e8ce88cdf47/Runtime/ExportGenerated/Editor/GUIUtility.cs:254)
UnityEngine.GUI.Box (Rect position, UnityEngine.GUIContent content, UnityEngine.GUIStyle style) (at C:/BuildAgent/work/14194e8ce88cdf47/Runtime/ExportGenerated/Editor/GUI.cs:215)
UnityEditor.DockArea.OnGUI () (at C:/BuildAgent/work/14194e8ce88cdf47/Editor/Mono/GUI/DockArea.cs:664)
And the slightly more intuitive
GUI Window tries to begin rendering while something else has not finished rendering! Either you have a recursive OnGUI rendering, or previous OnGUI did not clean up properly.
Am I doing something wrong? This seems to be all fine according to the Reference and the examples they've given.
Answer by delstrega · Jul 10, 2012 at 06:31 PM
Move that piece of code to the window's update function and you should be fine.
Sorry for the late reply but won't this have a severe impact on performance? Not that it doesn't work...
No, the Update is only called when something changes. The Editor doesn't redraw itself 60 times per sec ;) only when it's necessary.
An alternative could be to call GUIUtility.ExitGUI(); Which should ter$$anonymous$$ate the current GUI redraw cycle. This should also be used in the inspector to avoid warnings. See
But generally it's better to execute it from outside of OnGUI, or atleast in the repaint step. The same rule applies to changes to the GUI elements (adding / removing elements). Never change such a condition in the layout step. The layout and the repaint step have to be exactly the same.
Answer by The-Arc-Games · Jul 10, 2012 at 07:32 AM
What happens is that when target is not set, the first OnGUI call is used to close the window, which was generated in the same iteration.
You basically need to change your window condition into something like:
if (GUILayout.Button("Open Dialogue Editor") && target != null)
Even if I use that code or make sure it never closes the window on the first OnGUI call, I still get the same errors.
Your answer
![](https://koobas.hobune.stream/wayback/20220613073944im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Converting from EditorWindow to runnable window 1 Answer
Close (X) button 1 Answer
Unity editor creates a lot of default objects. Why? 0 Answers
GUI Window problem 2 Answers
GUI Window on GameObject location? 1 Answer