- Home /
Custom editor window stopped showing up
Greetings.
Yesterday I created a custom window for the editor that featured literally one button. Here's the code (obviously edited for brevity):
public class CubeSpawner : EditorWindow
{
[MenuItem ("Window/CubeSpawner")]
static void Create() {
// ... does some stuff...
}
public static void ShowWindow () {
EditorWindow.GetWindow<CubeSpawner>("CubeSpawner");
}
void OnGUI () {
if(GUILayout.Button("Generate")) {
Create();
}
}
}
It was working, was dockable, and it showed the intended button, up until less than an hour ago, when I took a break, saved my project and closed Unity. I didn't even restart my PC but, when I reopened Unity, the editor gave me some vague warning regarding layouts, and now this custom window only shows up in the Window tab, but when I click it, instead of a window with the declared button showing up, it just executes the functionality implemented in the Create() function. No dockable tab shows up, and it's not available under the "Add Tab" dropdown menu.
Has anyone else experienced this?
Currently using 2019.1, if that helps.
Answer by Bunny83 · Jun 13, 2019 at 07:52 AM
You attached the MenuItem attribute to your Create method, not to your ShowWindow method. It should be
// [ ... ]
{
[MenuItem ("Window/CubeSpawner")]
public static void ShowWindow () {
EditorWindow.GetWindow<CubeSpawner>("CubeSpawner");
}
static void Create() {
// ... does some stuff...
}
// [ ... ]
Attributes always belong to the thing that follows the attribute. This is true for all kinds of attributes. Attributes can be attached to all sorts of things. For example the In / Out attribute can be attached to method parameters like this
public bool SomeMethod([In, Out] object obj)
{
In case of methods you can think about it this way:
[MenuItem ("Window/CubeSpawner")] public static void ShowWindow () {
EditorWindow.GetWindow<CubeSpawner>("CubeSpawner");
}
It makes sense, but why was it working, then? I made literally no changes to the code between it working and it not working. And it's not about the closing of the editor either, because I had closed it before and when I booted it up again, it was still working fine.
That's not possible. You probably didn't have the "Create" method in the beginning which you later put in between the attribute and the ShowWindow method. You probably opened the window before that change. As long as the window is open you can do any changes to the editor window class.
The $$anonymous$$enuItem attribute tells the Unity editor to create that menu item and associate the click event with the method it is attached to. That's all. Unity certainly won't call any other unrelated method Unity doesn't care about what you actually do inside that method.
Your answer
Follow this Question
Related Questions
Custom Editor - Is there any way to detect whether the user is in Prefab editing mode? 1 Answer
How to update a EditorGUILayout Textfield value in realtime? 0 Answers
Drag a non standard unity asset on a custom editor slot 1 Answer
Create a custom editor window with repeating subsection (mockup included) 0 Answers
Positioning a button at the bottom of an editor window 1 Answer