UE4 Asset Editor Menu and Toolbar

UE4 Asset Editor Menu and Toolbar

1 Menu


1.1 Default Menu


Call InitAssetEditor of FAssetEditorToolkit in editor class and set parameter bCreateDefaultStandaloneMenu true.


virtual void InitAssetEditor(const EToolkitMode::Type Mode, const TSharedPtr<IToolkitHost>& InitToolkitHost, const FName AppIdentifier, const TSharedRef<FTabManager::FLayout>& StandaloneDefaultLayout, const bool bCreateDefaultStandaloneMenu, const bool bCreateDefaultToolbar, UObject* ObjectToEdit, const bool bInIsToolbarFocusable = false, const bool bInUseSmallToolbarIcons = false);

DefaultMenu

1.2 Extend Menu

Call AddMenuExtender of FAssetEditorToolkit to extend menu item.

For example:

void FBehaviorTreeEditor::ExtendMenu()
{
	struct Local
	{
		static void FillEditMenu(FMenuBuilder& MenuBuilder)
		{
			MenuBuilder.BeginSection("EditSearch", LOCTEXT("EditMenu_SearchHeading", "Search"));
			{
				MenuBuilder.AddMenuEntry(FBTCommonCommands::Get().SearchBT);
			}
			MenuBuilder.EndSection();
		}
	};

	TSharedPtr<FExtender> MenuExtender = MakeShareable(new FExtender);
	// Extend the Edit menu
	MenuExtender->AddMenuExtension(
		"EditHistory",
		EExtensionHook::After,
		GetToolkitCommands(),
		FMenuExtensionDelegate::CreateStatic(&Local::FillEditMenu));

	AddMenuExtender(MenuExtender);
}

The example add a menu item to Edit.

ExtendMenu


2 Toolbar


2.1 Default Toolbar


Call RegisterTabSpawners of FAssetEditorToolkit to register toolbar tab, so you should call this function in your editor class (subclass of FAssetEditorToolkit).


void FAssetEditorToolkit::RegisterTabSpawners(const TSharedRef<class FTabManager>& InTabManager)
{
	const auto& LocalCategories = InTabManager->GetLocalWorkspaceMenuRoot()->GetChildItems();
	TSharedRef<FWorkspaceItem> ToolbarSpawnerCategory = LocalCategories.Num() > 0 ? LocalCategories[0] : InTabManager->GetLocalWorkspaceMenuRoot();

	InTabManager->RegisterTabSpawner( ToolbarTabId, FOnSpawnTab::CreateSP(this, &FAssetEditorToolkit::SpawnTab_Toolbar) )
		.SetDisplayName( LOCTEXT("ToolbarTab", "Toolbar") )
		.SetGroup( ToolbarSpawnerCategory )
		.SetIcon( FSlateIcon(FEditorStyle::GetStyleSetName(), "Toolbar.Icon") );
}

Call InitAssetEditor in editor class and set parameter bCreateDefaultToolbar to true to spawn default toolbar, including save and browser button.

DefaultToolbar


2.2 Extend Toolbar


Call AddToolbarExtender to extend toolbar.


void FTestEditorToolbar::AddTestToolbar(TSharedPtr<FExtender> Extender)
{
	check(TestEditor.IsValid());
	TSharedPtr<FTestEditor> TestEditorPtr = TestEditor.Pin();

	TSharedPtr<FExtender> ToolbarExtender = MakeShareable(new FExtender);
	ToolbarExtender->AddToolBarExtension("Asset", EExtensionHook::After, TestEditorPtr->GetToolkitCommands(), FToolBarExtensionDelegate::CreateSP(this, &FTestEditorToolbar::FillTestToolbar));
	TestEditorPtr->AddToolbarExtender(ToolbarExtender);
}

void FTestEditorToolbar::FillTestToolbar(FToolBarBuilder& ToolbarBuilder)
{
	check(TestEditor.IsValid());
	TSharedPtr<FTestEditor> TestEditorPtr = TestEditor.Pin();
	ToolbarBuilder.BeginSection("ExtendToolbarItem");
	{
		const FText NewNodeLabel = LOCTEXT("NewNode_Label", "New");
		const FText NewNodeTooltip = LOCTEXT("NewNode_ToolTip", "Create a new node");
		const FSlateIcon NewNodeIcon = FSlateIcon(TEXT("EditorStyle"), "SessionConsole.Clear");

		ToolbarBuilder.AddToolBarButton(
			FUIAction(
				FExecuteAction::CreateSP(TestEditorPtr.Get(), &FTestEditor::CreateNewNode),
				FCanExecuteAction::CreateSP(TestEditorPtr.Get(), &FTestEditor::CanCreateNewNode),
				FIsActionChecked()
			),
			NAME_None,
			NewNodeLabel,
			NewNodeTooltip,
			NewNodeIcon
		);
	}
	ToolbarBuilder.EndSection();
}

FExecuteAction::CreateSP to create click action.

ExtendToolbar





Tags: UE4 Editor Vistied:
Share: Twitter Facebook LinkedIn