Skip to content

Close tabs

Roman Fadeev edited this page Apr 26, 2021 · 1 revision

Motivation

Unreal Engine and web browsers provide tab management options like "Close other tabs" and "Close tabs to the right". Unity provides single option "Close Tab". So the idea of close tabs is to implement logic similar to Unreal Engine and web browsers.

It turns out there is no simple way to support that for built-in Unity editor windows like "Scene", "Game", "Inspector" etc. However it is possible to implement that for custom editor windows via UnityEditor.IHasCustomMenu interface. Thus close tabs provides both base class PumpEditor.EditorWindowWithCloseTabs to inherit from and PumpEditor.CloseTabsHelper helper class to populate custom menu if inheritance is not an option.

How to use

PumpEditor.EditorWindowWithCloseTabs

Inherit this class to get close tab menu options for your editor window. Note that you can override PumpEditor.EditorWindowWithCloseTabs.AddItemsToMenu virtual method to add your own menu items if needed.

PumpEditor.CloseTabsHelper

If inheritance is not an option, implement UnityEditor.IHasCustomMenu by your editor window. The in the AddItemsToMenu method implemenetation call PumpEditor.CloseTabsHelper methods to add close tabs menu items:

// ...

public class MyEditorWindow : EditorWindow, IHasCustomMenu
{
    // ...

    public void AddItemsToMenu(GenericMenu menu)
    {
        CloseTabsHelper.AddCloseOtherTabsItem(this, menu);
        CloseTabsHelper.AddCloseTabsToTheRightItem(this, menu);
    }
}

Examples

Either implementation option results in new menu items added to the tab right click menu. Note that "Close Tabs to the Right" is disabled as there are no tabs to the right from the selected tab.

close-tabs-example-0

For maximized window both close tabs options are disabled.

close-tabs-example-1

Clone this wiki locally