How to do it...

  1. From the Chapter_10Editor folder, create two new files called MyCustomAssetActions.h and MyCustomAssetActions.cpp.
  2. Return to your project file and update your Visual Studio project. Once finished, open up the project in Visual Studio.
  3. Open MyCustomAssetActions.h and use the following code:
#pragma once
#include "AssetTypeActions_Base.h"
#include "Editor/MainFrame/Public/Interfaces/IMainFrameModule.h"

class CHAPTER_10EDITOR_API FMyCustomAssetActions : public FAssetTypeActions_Base
{
public:

virtual bool HasActions(const TArray<UObject*>& InObjects)
const override;

virtual void GetActions(const TArray<UObject*>& InObjects,
FMenuBuilder& MenuBuilder) override;

virtual FText GetName() const override;

virtual UClass* GetSupportedClass() const override;

virtual FColor GetTypeColor() const override;

virtual uint32 GetCategories() override;

void MyCustomAssetContext_Clicked()
{
TSharedRef<SWindow> CookbookWindow = SNew(SWindow)
.Title(FText::FromString(TEXT("Cookbook Window")))
.ClientSize(FVector2D(800, 400))
.SupportsMaximize(false)
.SupportsMinimize(false);

IMainFrameModule& MainFrameModule =
FModuleManager::LoadModuleChecked<IMainFrameModule>
(TEXT("MainFrame"));

if (MainFrameModule.GetParentWindow().IsValid())
{
FSlateApplication::Get().AddWindowAsNativeChild(CookbookWindow,
MainFrameModule.GetParentWindow().ToSharedRef());
}
else
{
FSlateApplication::Get().AddWindow(CookbookWindow);
}

};
};

  1. Open MyCustomAssetActions.cpp and add the following code:
#include "MyCustomAssetActions.h"
#include "Chapter_10Editor.h"
#include "MyCustomAsset.h"

bool FMyCustomAssetActions::HasActions(const TArray<UObject*>& InObjects) const
{
return true;
}

void FMyCustomAssetActions::GetActions(const TArray<UObject*>& InObjects, FMenuBuilder& MenuBuilder)
{
MenuBuilder.AddMenuEntry(
FText::FromString("CustomAssetAction"),
FText::FromString("Action from Cookbook Recipe"),
FSlateIcon(FEditorStyle::GetStyleSetName(),
"LevelEditor.ViewOptions"),
FUIAction(
FExecuteAction::CreateRaw(this,
&FMyCustomAssetActions::MyCustomAssetContext_Clicked),
FCanExecuteAction()
));
}

uint32 FMyCustomAssetActions::GetCategories()
{
return EAssetTypeCategories::Misc;
}

FText FMyCustomAssetActions::GetName() const
{
return FText::FromString(TEXT("My Custom Asset"));
}

UClass* FMyCustomAssetActions::GetSupportedClass() const
{
return UMyCustomAsset::StaticClass();
}

FColor FMyCustomAssetActions::GetTypeColor() const
{
return FColor::Emerald;
}

  1. Open up the Chapter_10Editor.h file and add the following property to the class:
#pragma once

#include "Engine.h"
#include "Modules/ModuleInterface.h"
#include "Modules/ModuleManager.h"
#include "UnrealEd.h"
#include "CookbookCommands.h"
#include "Editor/MainFrame/Public/Interfaces/IMainFrameModule.h"
#include "Developer/AssetTools/Public/IAssetTypeActions.h"

class FChapter_10EditorModule: public IModuleInterface
{
virtual void StartupModule() override;
virtual void ShutdownModule() override;

TArray< TSharedPtr<IAssetTypeActions> > CreatedAssetTypeActions;

TSharedPtr<FExtender> ToolbarExtender;
TSharedPtr<const FExtensionBase> Extension;

Don't forget to add the #include for IAssetTypeActions.h.
  1. Within your editor module (Chapter_10Editor.cpp), add the following code to the StartupModule() function:
#include "Developer/AssetTools/Public/IAssetTools.h"
#include "Developer/AssetTools/Public/AssetToolsModule.h"
#include "MyCustomAssetActions.h"
// ...

void FChapter_10EditorModule::StartupModule()
{

FCookbookCommands::Register();

TSharedPtr<FUICommandList> CommandList = MakeShareable(new FUICommandList());

CommandList->MapAction(FCookbookCommands::Get().MyButton, FExecuteAction::CreateRaw(this, &FChapter_10EditorModule::MyButton_Clicked), FCanExecuteAction());


ToolbarExtender = MakeShareable(new FExtender());

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

IAssetTools& AssetTools =
FModuleManager::LoadModuleChecked<FAssetToolsModule>
("AssetTools").Get();


auto Actions = MakeShareable(new FMyCustomAssetActions);
AssetTools.RegisterAssetTypeActions(Actions);
CreatedAssetTypeActions.Add(Actions);

}
  1. Add the following code inside the module's ShutdownModule() function:
void FChapter_10EditorModule::ShutdownModule()
{

ToolbarExtender->RemoveExtension(Extension.ToSharedRef());

Extension.Reset();
ToolbarExtender.Reset();

IAssetTools& AssetTools = FModuleManager::LoadModuleChecked<FAssetToolsModule>("Asset Tools").Get();

for (auto Action : CreatedAssetTypeActions)
{
AssetTools.UnregisterAssetTypeActions(Action.ToSharedRef());
}

}
  1. Compile your project and launch the editor.
  2. Create an instance of your custom Asset inside the Content Browser by right-clicking and selecting Miscellaneous | My Custom Asset.
  1. Right-click on your new asset to see our custom command in the context menu:

  1. Select the CustomAssetAction command to display a new blank editor window.