x


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.

more ▼

asked Jul 10 '12 at 04:33 AM

crushy gravatar image

crushy
26 2 5 8

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Move that piece of code to the window's update function and you should be fine.

more ▼

answered Jul 10 '12 at 06:31 PM

delstrega gravatar image

delstrega
840 5 8 12

Sorry for the late reply but won't this have a severe impact on performance? Not that it doesn't work...

Jul 12 '12 at 10:42 PM crushy

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 terminate the current GUI redraw cycle. This should also be used in the inspector to avoid warnings. See

this post

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.

Jul 12 '12 at 11:10 PM Bunny83
(comments are locked)
10|3000 characters needed characters left

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) 
more ▼

answered Jul 10 '12 at 07:32 AM

The Arc Games gravatar image

The Arc Games
392 1 4

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.

Jul 10 '12 at 06:20 PM crushy
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3697
x201
x171
x35

asked: Jul 10 '12 at 04:33 AM

Seen: 792 times

Last Updated: Jul 12 '12 at 11:14 PM