Creating a basic window from a menu item? (2021.1.9f1)

I’m starting out with a simple UI addition to the editor, and for some reason, the window does not appear. No errors in Visual Studio, and no errors in the Unity Editor Console when I select the menu item.

I just followed the instructions here: Unity - Scripting API: EditorWindow.GetWindow

Here’s the code:

using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

public class MenuCreateWorldTilemap : EditorWindow
{

    //Add a menu entry that will bring up the World Tilemap creation window
    [MenuItem("FiftyTifty/World/Create World Tilemap")]
    static void menuCreateWorldTilemap()
    {

        EditorWindow windowCreateWorldTilemap = (MenuCreateWorldTilemap) EditorWindow.GetWindow(typeof(MenuCreateWorldTilemap), true, "Create World Tilemap", true);
        windowCreateWorldTilemap.titleContent = new GUIContent("Create World Tilemap");

    }

    [MenuItem("FiftyTifty/World/Create World Tilemap", false)]
    private static bool menuCreateWorldTilemap_Check()
    {
        return GameObject.Find("tilemapWorld");
    }

}

This should just work, no? I’ve passed the proper arguments, the menu entry is not greyed out, and it is selectable. The window just won’t appear for some reason. Any idea why?

1 Like

Well, you created two MenuItems with the same name since you set the validate flag to false. Therefore both MenuItems have their validate flag set to false. This makes no sense. Your second method looks like it should be a validate method, so set the parameter to true. If you don’t want the validate method, remove it.

1 Like

I was following the official Unity tutorial, which states you are to create a separate validator with the same MenuItem path: https://learn.unity.com/tutorial/editor-scripting#

I misremembered and thought the false argument was the wanted returned bool value.

Thanks, it works now. Coding at 9am with no sleep is interesting.

1 Like