- Home /
Editor window doesn't open
I'm trying to create a simple editor window but for some reason it won't show up.
Here's my code:
using UnityEngine;
using UnityEditor;
public class ExplorerWindow : EditorWindow
{
private string m_workdingDirectory = null;
[MenuItem("Window/Explorer Window")]
public static void ShowWindow() // Gets called
{
GetWindow<ExplorerWindow>().Show();
}
private void Awake() // Never gets called
{
Initialize();
}
private void OnGUI() // Never gets called
{
GUILayout.Label("Working directory: " + m_workdingDirectory);
}
private void Initialize()
{
m_workdingDirectory = Application.dataPath;
Debug.Log("Working directory: " + m_workdingDirectory);
}
}
The ShowWindow() function gets called when I open the window from the Window/Explorer Window menu, but the window doesn't appear on my screen, and none of the methods like Awake() or OnGUI() gets called.
Edit 0:
I tried creating a fresh new project and copy the code from the one that wasn't working into the new one, which worked. It still doesn't work in my actual project though.
Answer by ShroomWasTaken · Jan 16, 2019 at 10:28 AM
I solved the issue by changing the editor layout to one of the default ones, and then switching back to my custom one. Opening the window started working after doing this.
I also got some weird errors in the console after doing it:
Doesn't seem like I was doing anything wrong. Maybe the editor layouts in Unity aren't working properly for some reason.
That probably means that your window was still open somewhere. Note that you can dock an editor window as tab anywhere. $$anonymous$$aybe there was some hidden instance of your editor window somewhere. This can happen when you doing quick experiments and had some logic errors in your editor window.
The problem with GetWindow is that it only opens a new one when there isn't one already. It uses FindObjectOfType to find an existing instance. Also note that you don't have to call Show on the window when using GetWindow. It will show the window automatically.
You can also create an actual new instance of your window by using
CreateInstance<ExplorerWindow>().Show();
As i said EditorWindows are just ScriptableObjects. So you create them with CreateInstance and can destroy them with DestroyImmediate. Though keep in $$anonymous$$d that using CreateInstance will create a new window every time. So you can have two or more windows open at the same time.
I checked if it was tabbed somewhere and I couldn't find it. $$anonymous$$aybe it was positioned off-screen or something. The API for EditorWindow seems to call Show() after using GetWindow(), do you know why they would do that if GetWindow() already shows the window?
Answer by Bunny83 · Jan 15, 2019 at 03:35 PM
Are you sure that this class is in a file called ExplorerWindow.cs
and that this file is located in an "editor" folder?
The EditorWindows base class is just derived from ScriptableObject. Just like any ScriptableObject derived type, the filename and class name has to match. Also since this is an editor class it has to be placed in an editor folder.
Finally make sure you don't have any compiler error in your project. Unity can't compile and load your assemblies if there is an error in any script inside your project.
The file has the same name as the class and it's located directly inside an Editor folder.
Here's a picture of my file hierarchy:
And I do not have any errors in the project.