ue5 c++ slate插件中资源查看的上下文菜单扩展 完整代码
时间: 2024-05-08 22:15:13 浏览: 193
上下文菜单
下面是一个示例的完整代码,它可以在Slate插件中添加资源查看的上下文菜单扩展:
```cpp
#include "MySlateModule.h"
#include "MyAsset.h"
#include "MyAssetEditor.h"
#include "MyAssetActions.h"
#define LOCTEXT_NAMESPACE "MySlateModule"
void FMySlateModule::StartupModule()
{
// 注册资源类型
IAssetTools& AssetTools = FModuleManager::LoadModuleChecked<FAssetToolsModule>("AssetTools").Get();
TSharedPtr<IAssetTypeActions> AssetTypeActions = MakeShareable(new FMyAssetActions);
AssetTools.RegisterAssetTypeActions(AssetTypeActions.ToSharedRef());
// 添加上下文菜单扩展
if (UAssetEditorSubsystem* AssetEditorSubsystem = GEditor->GetEditorSubsystem<UAssetEditorSubsystem>())
{
FAssetEditorExtenderDelegate AssetEditorExtenderDelegate = FAssetEditorExtenderDelegate::CreateRaw(this, &FMySlateModule::AddAssetEditorContextMenuExtender);
AssetEditorSubsystem->OnExtendersChanged().AddRaw(this, &FMySlateModule::OnAssetEditorExtendersChanged);
AssetEditorSubsystem->AddAssetEditorContextMenuExtender(AssetEditorExtenderDelegate);
}
}
void FMySlateModule::ShutdownModule()
{
// 取消注册资源类型
IAssetTools& AssetTools = FModuleManager::LoadModuleChecked<FAssetToolsModule>("AssetTools").Get();
for (TSharedPtr<IAssetTypeActions>& AssetTypeActions : AssetTools.GetAllAssetTypeActions())
{
AssetTools.UnregisterAssetTypeActions(AssetTypeActions.ToSharedRef());
}
// 移除上下文菜单扩展
if (UAssetEditorSubsystem* AssetEditorSubsystem = GEditor->GetEditorSubsystem<UAssetEditorSubsystem>())
{
AssetEditorSubsystem->OnExtendersChanged().RemoveAll(this);
AssetEditorSubsystem->RemoveAssetEditorContextMenuExtender(AssetEditorContextMenuExtenderDelegateHandle);
}
}
void FMySlateModule::AddAssetEditorContextMenuExtender(UObject* Object, UBlueprintEditor* Editor, TArray<FExtender>& Extenders)
{
if (UMyAsset* MyAsset = Cast<UMyAsset>(Object))
{
TSharedPtr<FExtender> Extender = MakeShareable(new FExtender);
Extender->AddMenuExtension(
"AssetEditorActions",
EExtensionHook::After,
nullptr,
FMenuExtensionDelegate::CreateRaw(this, &FMySlateModule::AddAssetEditorContextMenuExtension, MyAsset)
);
Extenders.Add(Extender.ToSharedRef());
}
}
void FMySlateModule::AddAssetEditorContextMenuExtension(FMenuBuilder& MenuBuilder, UMyAsset* MyAsset)
{
MenuBuilder.BeginSection("MyAssetSection", LOCTEXT("MyAssetSectionLabel", "My Asset"));
{
MenuBuilder.AddMenuEntry(
LOCTEXT("MyAssetViewSource", "View Source"),
LOCTEXT("MyAssetViewSourceTooltip", "View the source code for this asset."),
FSlateIcon(),
FUIAction(
FExecuteAction::CreateRaw(this, &FMySlateModule::OnViewSourceClicked, MyAsset),
FCanExecuteAction::CreateLambda([]() { return true; })
),
NAME_None,
EUserInterfaceActionType::Button
);
}
MenuBuilder.EndSection();
}
void FMySlateModule::OnAssetEditorExtendersChanged()
{
if (UAssetEditorSubsystem* AssetEditorSubsystem = GEditor->GetEditorSubsystem<UAssetEditorSubsystem>())
{
AssetEditorContextMenuExtenderDelegateHandle = AssetEditorSubsystem->GetAssetEditorContextMenuExtender(GetModularFeatureName(), FName("BlueprintEditor")).GetHandle();
}
}
void FMySlateModule::OnViewSourceClicked(UMyAsset* MyAsset)
{
// 打开资源的源代码编辑器
FMyAssetEditor::CreateEditor(EToolkitMode::Standalone, nullptr, MyAsset);
}
#undef LOCTEXT_NAMESPACE
IMPLEMENT_MODULE(FMySlateModule, MySlateModule)
阅读全文