在UE的开发中,有些项目需要针对不同版本出不同的包,并有一个对应的GUI界面,供大家使用。
本文就来实现一下这个功能。
先使用UE4自己的插件模板创建插件,做成插件形式
然后注册Slate UI,编写打开逻辑。并在按钮点击函数PluginButtonClicked内触发。
.cpp部分代码如下:
#include "PackageHelper.h" #include "PackageHelperStyle.h" #include "PackageHelperCommands.h" #include "Misc/MessageDialog.h" #include "ToolMenus.h" #include "GameMapsSettings.h" #include "Misc/FileHelper.h" #include "FileHelpers.h" static const FName PackageHelperTabName("PackageHelper"); #define LOCTEXT_NAMESPACE "FPackageHelperModule" TSharedRef<SDockTab> FPackageHelperModule::WindowBody(const FSpawnTabArgs& SpawnTabArgs) { return SNew(SDockTab) .TabRole(ETabRole::NomadTab) [ SNew(SVerticalBox) + SVerticalBox::Slot() [ SNew(SButton) .Content() [ SNew(STextBlock) .Justification(ETextJustify::Center) .Text(LOCTEXT("Build Test Package1", "Build Test Package1")) ] .OnClicked_Lambda([this]() { //Clicked Test Package1. return FReply::Handled(); }) ] + SVerticalBox::Slot() [ SNew(SButton) .Content() [ SNew(STextBlock) .Justification(ETextJustify::Center) .Text(LOCTEXT("Build Test Package2", "Build Test Package2")) ] .OnClicked_Lambda([this]() { //Clicked Test Package2. return FReply::Handled(); }) ] + SVerticalBox::Slot() [ SNew(SButton) .Content() [ SNew(STextBlock) .Justification(ETextJustify::Center) .Text(LOCTEXT("Build Test Package3", "Build Test Package3")) ] .OnClicked_Lambda([this]() { //Clicked Test Package3. return FReply::Handled(); }) ] ]; } void FPackageHelperModule::StartupModule() { FPackageHelperStyle::Initialize(); FPackageHelperStyle::ReloadTextures(); FPackageHelperCommands::Register(); PluginCommands = MakeShareable(new FUICommandList); PluginCommands->MapAction( FPackageHelperCommands::Get().PluginAction, FExecuteAction::CreateRaw(this, &FPackageHelperModule::PluginButtonClicked), FCanExecuteAction()); UToolMenus::RegisterStartupCallback(FSimpleMulticastDelegate::FDelegate::CreateRaw(this, &FPackageHelperModule::RegisterMenus)); FGlobalTabmanager::Get()->RegisterNomadTabSpawner(PackageHelperTabName , FOnSpawnTab::CreateRaw(this, &FPackageHelperModule::WindowBody)) .SetDisplayName(LOCTEXT("PackageHelper", "PackageHelper")) .SetMenuType(ETabSpawnerMenuType::Hidden); } void FPackageHelperModule::ShutdownModule() { UToolMenus::UnRegisterStartupCallback(this); UToolMenus::UnregisterOwner(this); FPackageHelperStyle::Shutdown(); FPackageHelperCommands::Unregister(); FGlobalTabmanager::Get()->UnregisterNomadTabSpawner(PackageHelperTabName); } void FPackageHelperModule::PluginButtonClicked() { FGlobalTabmanager::Get()->TryInvokeTab(PackageHelperTabName); } void FPackageHelperModule::RegisterMenus() { FToolMenuOwnerScoped OwnerScoped(this); { UToolMenu* Menu = UToolMenus::Get()->ExtendMenu("LevelEditor.MainMenu.Window"); { FToolMenuSection& Section = Menu->FindOrAddSection("WindowLayout"); Section.AddMenuEntryWithCommandList(FPackageHelperCommands::Get().PluginAction, PluginCommands); } } { UToolMenu* ToolbarMenu = UToolMenus::Get()->ExtendMenu("LevelEditor.LevelEditorToolBar"); { FToolMenuSection& Section = ToolbarMenu->FindOrAddSection("Settings"); { FToolMenuEntry& Entry = Section.AddEntry(FToolMenuEntry::InitToolBarButton(FPackageHelperCommands::Get().PluginAction)); Entry.SetCommandList(PluginCommands); } } } } #undef LOCTEXT_NAMESPACE IMPLEMENT_MODULE(FPackageHelperModule, PackageHelper)