If the window is docked it will create a new instance of this window rather than bringing it to focus…this does not happen if the window is un-docked…any ideas why?
Still an issue.
In our case we open an internal unitypackage all the time and open the required EditorWindow by menu. To automate this, I tried this:
[InitializeOnLoad]
public class Startup
{
static Startup()
{
MyWindow window = EditorWindow.GetWindow<MyWindow>();
window.Show();
}
}
But on the next script reload the Editor doesn’t know about the already existing window and opens another one. Looks like an initialization issue of the editor.
Next thing i realized: The window even opens just by calling GetWindow.
So the documentation is absolute wong in this case.
I solved this with a custom singleton:
[InitializeOnLoad]
public class Startup
{
static Startup()
{
MyWindow.ShowWindow();
}
}
public class MyWindow : EditorWindow
{
// Singleton to avoid multiple instances of window.
private static MyWindow instance = null;
// Set instance on reloading the window, else it gets lost after script reload (due to PlayMode changes, ...).
public MyWindow()
{
instance = this;
}
[MenuItem("Window/MyWindow")]
public static void ShowWindow()
{
if (instance == null)
{
// "Get existing open window or if none, make a new one:" says documentation.
// But if called after script reloads a second instance will be opened! => Custom singleton required.
MyWindow window = EditorWindow.GetWindow<MyWindow>();
window.titleContent = new GUIContent("My Title");
instance = window;
window.Show();
}
else
instance.Focus();
}
}