[Editor Extension] - Menu Helper

Sorry if the title is a bit deceiving and doesn’t entirely convey what I’m attempting to do. I wasn’t entirely sure what would be best to call it.

What am I attempting to do?

Alright, so I have a Class Type called Menu, which inherits from BaseMenu (Not Important).

If I want to effectively edit a specific menu, I have to make sure that it’s the only one enabled. This, is entirely a pain if I have multiple menus. So I thought, hey! This would be a great opportunity for an Editor Script!

What functionality does it need?

So essentially if a Menu’s GameObject or it’s children are selected, then I want it to be enabled.

Any GameObject that isn’t selected should be disabled.

If no GameObjects of the Menu type are selected, then it should try and Find a GameManager in the Scene and get it’s CurrentMenu (This is specified in the Inspector).

What have I tried so far?

I’ve tried a very inefficient method just for testing and trying to figure out the best way to go about it. But I feel as though a CustomEditor or at least in the way I’m using it isn’t the best option.

[CustomEditor(typeof(Menu))]
public class MenuEditor : Editor
{
    private Menu[] _menus;

    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();

        RefreshMenus();

        foreach (var menu in _menus)
        {
            menu.gameObject.SetActive(false);
        }

        var myMenu = (Menu) target;
        if (myMenu)
        {
            myMenu.gameObject.SetActive(true);
        }
    }

    private void RefreshMenus()
    {
        _menus = Resources.FindObjectsOfTypeAll<Menu>();
    }
}

I know the above code doesn’t include all of my functionality, and that’s because for what I want to do the CustomEditor isn’t a very good options. It only runs when a GameObject of type Menu is selected or it’s inspector is modified in some way.

Any help is appreciated, thanks!

You can create an EditorWindow for this and use OnSelectionChange, Selection.instanceIDs or Selection.activeGameObject for checking what is selected and see if those objects are the Menu Object or it’s a child of it.

If you don’t want to use an EditorWindow, you can try something like this:

though I think it’s inconvenient for this case.