# Unreal Editor: adding a custom menu

**Adding a new option in the menus in unreal engine**

In the module add for example to add to tools:&#x20;

#### Extending Menus: Step-by-Step Guide

**1. Setting Up**

Start by preparing your module to modify the Unreal Editor's menus during its initialization phase. This ensures that your customizations are loaded correctly when the editor starts.

```
#include "ToolMenus.h"

void MyModule::StartupModule()
{
    UToolMenus::RegisterStartupCallback(FSimpleMulticastDelegate::FDelegate::CreateRaw(this, &MyModule::RegisterMenus));
}

void MyModule::ShutdownModule()
{
    UToolMenus::UnRegisterStartupCallback(this);
    UToolMenus::UnregisterOwner(this);
}

void MyModule::RegisterMenus()
{
    FToolMenuOwnerScoped OwnerScoped(this);
    // Menu extension logic goes here...
}
```

```
// Registers custom menu items within the Unreal Engine Editor
void MyModule::RegisterMenus()
{
    // Scoped tool menu registration, ensures this instance of the menu registration is managed correctly
    // when the module is loaded and unloaded, preventing issues like duplicate entries.
    FToolMenuOwnerScoped OwnerScoped(this);

    // Retrieves and extends the "Tools" menu within the Level Editor's main menu.
    // This function finds the existing "Tools" menu and prepares it for modification.
    UToolMenu* Menu = UToolMenus::Get()->ExtendMenu("LevelEditor.MainMenu.Tools");

    // Locates an existing section named "PluginTools" or creates a new section if it does not exist.
    // Sections help organize commands into logical groups.
    FToolMenuSection& Section = Menu->FindOrAddSection("PluginTools");

    // Adds a new menu entry to the "PluginTools" section. This is where you actually add your command.
    // This step involves several parameters to configure the appearance and behavior of the menu entry:
    Section.AddMenuEntryWithCommandList(
        // Command from MyCommands - identifies what action this menu entry will trigger when clicked.
        MyCommands::Get().MyCommand,

        // Command list that this entry is a part of - handles execution context and ensures the command
        // is executed as part of this list, enabling things like undo/redo stacks to work correctly.
        MyCommandList,

        // Localized text for the label of the menu entry. 'LOCTEXT' is a macro used to handle
        // localization/text internationalization in Unreal Engine.
        // "MyCommandLabel" - namespace, "Execute My Command" - default text.
        LOCTEXT("MyCommandLabel", "Execute My Command"),

        // Localized text for the tooltip of the menu entry. Tooltips provide additional information
        // when the user hovers the mouse over the menu item.
        // "MyCommandToolTip" - namespace, "Executes My Command." - default text.
        LOCTEXT("MyCommandToolTip", "Executes My Command."),

        // Icon to be displayed next to the menu item. Icons visually represent the action and enhance UX.
        // 'MyStyle::GetStyleSetName()' returns the name of the style set where icons and other UI assets are defined.
        // 'MyCommandIcon' is the identifier for the specific icon within the style set.
        FSlateIcon(MyStyle::GetStyleSetName(), "MyCommandIcon")
    );
}
```

#### Extra Info for this section

#### Overview of UToolMenus

`UToolMenus` is a subsystem in Unreal Engine designed to manage UI menus and toolbars throughout the Editor. It allows you to modify existing menus or add new items dynamically, supporting a wide range of customization.

#### Extending Menus in Unreal Editor

Regarding extending menus in Unreal Editor, the `UToolMenus` class allows plugins to insert their custom menu items into various parts of the editor's UI. Here's a list of some common menu locations you can extend:

* **LevelEditor.MainMenu**: The main menu bar at the top of the Level Editor.
  * **File**: Related to file operations (new, open, save).
  * **Edit**: Related to editing operations (undo, redo, cut, copy, paste).
  * **View**: Related to the viewport configurations and other view options.
  * **Window**: Related to the various windows and panels that can be opened.
  * **Tools**: Typically used for tools related to development and utilities.
  * **Help**: Provides help links, documentation, and other resources.
* **LevelEditor.Toolbar**: The main toolbar in the Level Editor.
  * **Settings**: Where settings and preferences can be accessed.
  * **Viewport**: Specific tools related to the viewport.
* **AssetEditor**: Menus specific to asset types like Material Editor, Animation Editor, etc.

#### Common Menu Locations and How to Extend Them

**LevelEditor.MainMenu**

This is the main menu bar at the top of the Level Editor. Here's how to add a new tool under the "Tools" menu:

```cpp
cppCopy codevoid MyModule::RegisterMenus()
{
    FToolMenuOwnerScoped OwnerScoped(this);
    UToolMenu* Menu = UToolMenus::Get()->ExtendMenu("LevelEditor.MainMenu.Tools");
    FToolMenuSection& Section = Menu->FindOrAddSection("PluginTools");
    Section.AddMenuEntryWithCommandList(
        MyCommands::Get().MyCommand,
        MyCommandList,
        LOCTEXT("MyCommandLabel", "Execute My Command"),
        LOCTEXT("MyCommandToolTip", "Executes My Command."),
        FSlateIcon(MyStyle::GetStyleSetName(), "MyCommandIcon")
    );
}
```

**LevelEditor.Toolbar**

For adding items to the main toolbar:

```cpp
cppCopy codevoid MyModule::RegisterMenus()
{
    FToolMenuOwnerScoped OwnerScoped(this);
    UToolMenu* ToolbarMenu = UToolMenus::Get()->ExtendMenu("LevelEditor.LevelEditorToolBar");
    FToolMenuSection& Section = ToolbarMenu->AddSection("MyPluginSection", LOCTEXT("MyPluginSection", "My Plugin"));
    Section.AddEntry(FToolMenuEntry::InitToolBarButton(
        MyCommands::Get().MyToolbarCommand,
        LOCTEXT("MyToolbarCommandLabel", "My Toolbar Button"),
        FSlateIcon(MyStyle::GetStyleSetName(), "MyToolbarIcon")
    ));
}
```

**AssetEditor Menus**

For asset-specific editors like Material Editor or Animation Editor:

```cpp
cppCopy codevoid MyModule::RegisterMenus()
{
    FToolMenuOwnerScoped OwnerScoped(this);
    UToolMenu* Menu = UToolMenus::Get()->ExtendMenu("AssetEditor.AnimationEditor.Menu");
    FToolMenuSection& Section = Menu->FindOrAddSection("MyAssetTools");
    Section.AddMenuEntryWithCommandList(
        MyCommands::Get().MyAssetCommand,
        MyCommandList,
        LOCTEXT("MyAssetCommandLabel", "Process Animation"),
        LOCTEXT("MyAssetCommandToolTip", "Processes selected animation."),
        FSlateIcon(MyStyle::GetStyleSetName(), "MyAssetCommandIcon")
    );
}
```

#### Tips for Effective Menu Integration

* **Localization**: Use `LOCTEXT` for labels and tooltips to support localization.
* **Iconography**: Ensure that you provide clear, understandable icons for your menu items. This improves the UX and makes your tools easily recognizable.
* **Testing**: Always test your menu additions across different configurations and ensure that they do not interfere with existing functionalities.
* **Documentation**: Provide clear documentation for your plugin's users, explaining what each menu item does, especially if the functionality isn't immediately obvious.
