美文网首页
UE 在Editor的资源右键菜单上添加两个按钮

UE 在Editor的资源右键菜单上添加两个按钮

作者: 老铁码农 | 来源:发表于2022-09-08 11:43 被阅读0次

    今天接到个任务,需要在Editor的资源右键菜单上添加两个按钮,用来上传资源,一个是默认参数上传,一个是用户输入描述去上传,研究了半天,最后在UE的源码里找到的作业抄了下来,做个记录,由于周五了实在是太累了,不想详细写了,直接贴代码吧,后续有心情再改。效果如下

    企业微信截图_16578864552915.png
    #pragma once
    #include "CoreMinimal.h"
    #include "Templates/SharedPointer.h"
    #include "Framework/MultiBox/MultiBoxExtender.h"
    #include "UObject/NoExportTypes.h"
    #include "Framework/MultiBox/MultiBoxBuilder.h"
    #include "IPlatformFilePak.h"
    #include "ResourceLoader.h"
    #include "Blueprint/UserWidget.h"
    #include "Templates/SubclassOf.h"
    #include "ToolMenuSection.h"
    #include "AssetMeta.h"
    #include "AssetHttpUploader.h"
    
    
    class XVERSEASSETUPLOADER_API FAssetUploaderMenu : public TSharedFromThis<FAssetUploaderMenu>
    {
    private:
        TArray<FName> AssetPathArray;
    
        FAssetUploaderMenu& operator=(const FAssetUploaderMenu&) = delete;
        FAssetUploaderMenu(const FAssetUploaderMenu&) = delete;
    
    public:
    
        FAssetUploaderMenu() = default;
    
        TSubclassOf<class UUserWidget> HUDAssetUpload = nullptr;
    
        UUserWidget* HUDAssetUploadUI;
    
        static FAssetUploaderMenu& Get();
    
        void RegisterMenu();
    
        void AssetUpload(const FNewAsset& NewAsset);
    
        void AssetUploadDefaultParameter();
    
        bool CanExecuteAssetUpload() const;
    
        void AddAssetUploaderMenu(UToolMenu* InMenu);
    
        void ShowDialog();
    
    };
    
    
    
    #include "AssetUploaderMenu.h"
    #include "ContentBrowserDelegates.h"
    #include "Modules/ModuleManager.h"
    
    #include "Framework/MultiBox/MultiBoxExtender.h"
    #include "ToolMenus.h"
    #include "Framework/Commands/UIAction.h"
    #include "ContentBrowserDataMenuContexts.h"
    #include "ContentBrowserItem.h"
    #include "ResourceLoader.h"
    #include "GenericSingletons.h"
    #include "AssetMeta.h"
    #include "AssetHttpUploader.h"
    #include "Engine/World.h"
    #include "UObject/NoExportTypes.h"
    #include "UMGEditor/Public/WidgetBlueprint.h"
    #include "Blutility/Classes/EditorUtilityWidget.h"
    #include "LevelEditor.h"
    #include "UObject/UObjectGlobals.h"
    #include "Modules/ModuleManager.h"
    #include "EditorAssetLibrary.h"
    #include "EditorUtilitySubsystem.h"
    #include "ContentBrowserMenuContexts.h"
    #include "EditorUtilitySubsystem.h"
    #include "Blutility/Classes/EditorUtilityWidgetBlueprint.h"
    
    #if WITH_EDITOR
    #include "ContentBrowserModule.h"
    #endif
    
    #define LOCTEXT_NAMESPACE "AssetUploadMenu"
    
    
    
    void FAssetUploaderMenu::AssetUploadDefaultParameter()
    {
    //公司隐私代码,已经删除,个人根据自己业务需求来处理,这里是收到了,用户点击按钮的事件
    }
    
    
    void FAssetUploaderMenu::AssetUpload(const FNewAsset& NewAsset)
    {
        //公司隐私代码,已经删除,个人根据自己业务需求来处理,这里是收到了
    }
    
    /* 
    显示对话框
    */
    void FAssetUploaderMenu::ShowDialog()
    {
        const FString Path = FString(TEXT("/Game/AssetManager/AssetUpload_Dialog.AssetUpload_Dialog"));
        UObject* LoadedObject = LoadObject<UObject>(NULL, *Path, NULL, LOAD_NoRedirects, NULL);
    
        //LoadedObject->GetClass()->IsA(UStaticMesh::StaticClass());
        FString Name = LoadedObject->GetClass()->GetName();
        UE_LOG(LogTemp, Display, TEXT("Name:%s"), *Name);
        /*if(LoadedObject->GetClass()->GetName() == "StaticMesh")
        {
            UE_LOG(LogTemp, )
        }*/
        UWidgetBlueprint* Blueprint = Cast<UWidgetBlueprint>(LoadedObject);
        if (Blueprint->GeneratedClass->IsChildOf(UEditorUtilityWidget::StaticClass()))
        {
            UEditorUtilityWidgetBlueprint* EditorWidget = Cast<UEditorUtilityWidgetBlueprint>(Blueprint);
            if (EditorWidget)
            {
                UEditorUtilitySubsystem* EditorUtilitySubsystem = GEditor->GetEditorSubsystem<UEditorUtilitySubsystem>();
                EditorUtilitySubsystem->SpawnAndRegisterTab(EditorWidget);
            }
        }
    }
    
    
    bool FAssetUploaderMenu::CanExecuteAssetUpload() const
    {
        return AssetPathArray.Num() > 0;
    }
    
    void FAssetUploaderMenu::AddAssetUploaderMenu(UToolMenu* InMenu)
    {
        const UContentBrowserDataMenuContext_FileMenu* ContextObject = InMenu->FindContext<UContentBrowserDataMenuContext_FileMenu>();
        checkf(ContextObject, TEXT("Required context UContentBrowserDataMenuContext_FileMenu was missing!"));
    
        for (const FContentBrowserItem& SelectedItem : ContextObject->SelectedItems)
        {
            const FContentBrowserItemData* SelectedItemData = SelectedItem.GetPrimaryInternalItem();
            FName ResourcePath = SelectedItemData->GetVirtualPath();
            AssetPathArray.Add(ResourcePath);
        }
    
        FToolMenuSection& Section = InMenu->AddSection("UploadAsset");
        {
        /*  UContentBrowserAssetContextMenuContext* Context = Section.FindContext<UContentBrowserAssetContextMenuContext>();
            TArray<UObject*> Objects = Context->GetSelectedObjects();*/
            // 添加两个按钮
            Section.AddMenuEntry("SCCUploadAsset",
                             LOCTEXT("SCCUploadAsset", "Upload Asset..."),
                             LOCTEXT("SCCUploadAssetTooltip", "You will upload asset to consol."),
                             FSlateIcon(),
                                 FUIAction(FExecuteAction::CreateSP(this, &FAssetUploaderMenu::ShowDialog),
                                     FCanExecuteAction::CreateSP(this, &FAssetUploaderMenu::CanExecuteAssetUpload)));
    
            Section.AddMenuEntry("SCCUploadAssetDefaultParameter",
                                 LOCTEXT("SCCUploadAssetDefaultParameter", "Upload Asset Default Parameter"),
                                 LOCTEXT("SCCUploadAssetDefaultParameterTooltip", "You will need to write Asset parameter."),
                                 FSlateIcon(),
                                 FUIAction(FExecuteAction::CreateSP(this, &FAssetUploaderMenu::AssetUploadDefaultParameter),
                                 FCanExecuteAction::CreateSP(this, &FAssetUploaderMenu::CanExecuteAssetUpload)));
        }
    }
    
    FAssetUploaderMenu& FAssetUploaderMenu::Get()
    {
        static TSharedPtr<FAssetUploaderMenu> Single = MakeShared<FAssetUploaderMenu>();
        return *Single;
    }
    
    void FAssetUploaderMenu::RegisterMenu()
    {
        // 获取到UToolMenu
        if (UToolMenu* Menu = UToolMenus::Get()->ExtendMenu("ContentBrowser.AssetContextMenu"))
        {
            Menu->AddDynamicSection(FName(TEXT("DynamicSection_DataSource_AssetUploaderMenu")), FNewToolMenuDelegate::CreateLambda([this](UToolMenu* InMenu) {
                AddAssetUploaderMenu(InMenu);
            }));
        }
    }
    
    #undef LOCTEXT_NAMESPACE
    
    

    相关文章

      网友评论

          本文标题:UE 在Editor的资源右键菜单上添加两个按钮

          本文链接:https://www.haomeiwen.com/subject/beecirtx.html