Prevent Editor window from closing

Hi there guys,

I am working on an Editor Extension in which I need to create a couple of editor windows. Everything is fine and dandy, but eventually I run into 2 problems and I was wondering if any of you might know how I can solve these.

First, when I enter play mode, the windows close - is there any way to prevent this? I found some suggestions on the Internet, like using HideFlags.HideAndDontSave, but that doesn’t solve the problem.

Secondly, and this requires some explaining. In my Inspector GUI, I have some buttons that open my editor windows. Pressing the same button multiple times works well, but if I leave the editor windows opened and then close Unity, after I re-open Unity, pressing the buttons will create duplicate windows.

I am using a custom editor window class for each window. For example:

[Serializable]
class MyEditorWindow : EditorWindow
{
     // Stuff..
}

I keep references to these windows in a Monobehaviour and I was expecting that they would get serialized. But they don’t.

The windows are created only once when needed. In the parent Monobehaviour, I have a property for each window. When the property is called, the property checks if the window ref is null. If it is, it creates it and from there, any call to the property just returns the window reference. I am using the same approach for custom classes that derive from ScripatbleObjects and it works like a charm.

I would like to mention that I am using EditorWindow.CreateInstance to create the windows. I can not use EditorWindow.GetWindow() (which would solve this problem) because then I run into other problems. To cut a long story short :), I need to use CreateInstance.

Please let me know if you need more info.

Cheers,
Andrew

Ok. Solved both my problems. The windows were getting destroyed when switching to play mode because I was closing them in the ‘OnDestroy’ method of their parent Monobehaviour. In fact this was the rot of all my problems. Everything is well now :slight_smile: