UE4編輯器ToolBar擴展

問題描述: Sequencer工具欄上後增加定製按鈕,如圖所示:

紅框按鈕後新增定製按鈕。

尋找解決辦法:商城上有一個插件是Substance Source,安裝上之後可以看到如下界面:

跟我們的需求一致,那麼就看下它是如何實現的。

virtual void StartupModule() override
{
   ...
   //Creates the button above the scene viewport to launch source window
   CreateToolbarButton();
    ...
}
void CreateToolbarButton()
	{
		FSubstanceSourceModuleCommands::Register(); //

		// Bind command
		CommandList = MakeShareable(new FUICommandList);

		struct Local
		{
			static void AddToolbarButton(FToolBarBuilder& ToolbarBuilder)
			{
				//NOTE:: Leave spacing in names for auto padding - Might be benificial to alter this later to lock in a solid size
				//and to remove string size
				ToolbarBuilder.AddToolBarButton(FSubstanceSourceModuleCommands::Get().LaunchSubstanceSourceWindow,
				                                NAME_None,
				                                FText::FromString("   Source   "),
				                                FText::FromString("   Source   "),
				                                FSlateIcon("SubstanceSourceStyle", "SubstanceSourceButtonIcon")
				                               );
			}
		};

		const FSubstanceSourceModuleCommands& Commands = FSubstanceSourceModuleCommands::Get();

		TSharedPtr<FExtender> ToolbarExtender = MakeShareable(new FExtender);

		ToolbarExtender->AddToolBarExtension(
		    "Content",
		    EExtensionHook::After,
		    CommandList,
		    FToolBarExtensionDelegate::CreateStatic(&Local::AddToolbarButton)
		);

		CommandList->MapAction(
		    Commands.LaunchSubstanceSourceWindow,
		    FExecuteAction::CreateRaw(this, &FSubstanceSourceModule::OnLaunchSubstanceSourceWindow),
		    FCanExecuteAction()
		);

		FLevelEditorModule& LevelEditorModule = FModuleManager::LoadModuleChecked<FLevelEditorModule>("LevelEditor");

		LevelEditorModule.GetToolBarExtensibilityManager()->AddExtender(ToolbarExtender);
	}

這個有幾個點: 

大部分的編輯器的ToolBar都支持擴展的,想知道該ToolBar是否支持擴展,可看下這個模塊是否有提供類似於GetToolBarExtensibilityManager類似的函數,這個ExtensManager是在Toolbar構建的時候傳入的,這樣就支持了Toolbar擴展。舉例看下LevelEditor的Toolbar構建時的代碼:

FLevelEditorModule& LevelEditorModule = FModuleManager::LoadModuleChecked<FLevelEditorModule>("LevelEditor");
TSharedPtr<FExtender> Extenders = LevelEditorModule.GetToolBarExtensibilityManager()->GetAllExtenders();

static const FName LevelEditorToolBarName = "LevelEditorToolBar";
FToolBarBuilder ToolbarBuilder( InCommandList, FMultiBoxCustomization::AllowCustomization( LevelEditorToolBarName ), Extenders );

那麼我們需要做的就是構建FExtender內容,獲取到指定模塊的ExtendsManager,然後執行AddExtender操作就可以了。

構建Extender主要是構建擴展的是按鈕的樣貌,點擊後的響應,還是就是添加到哪個Section後面,SubstanceSource上的按鈕是放在了“市場”後面,“市場”屬於“Content”Section.(可以通過看下編輯器Toolbar構造代碼可以知道按鈕屬於哪個Section以及Section的名字)。

就這些內容。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章