Download

Is it possible to add custom asset category for custom Data Asset?

I tried to create a custom asset category in Content Browser. This asset is my custom UDataAsset. If I want to create new asset, I need to go through Misc/Data Asset and then choose my Data Asset from the list. So I want to have my own category with frequently used assets.

In theory I wrote a good code. Copied it after AI Module as suggested here
https://answers.unrealengine.com/questions/337715/custom-asset-category.html

Didn’t work, so I look at Object Pool plugin which adds its own asset category. It didn’t work too.

So maybe isn’t possible to move Data Asset into different category? And it has to stay at Misc/Data Asset menu.
Or are there extra steps required?

Yes, it´s possible for any UObject; you just have to create an asset factory for your custom class:

In runtime module:



#include "Runtime/Engine/Classes/Engine/DataAsset.h"

USTRUCT()
struct FMyItemInfo{

    GENERATED_USTRUCT_BODY()

    UPROPERTY(EditAnywhere)
    FString itemName;

    UPROPERTY(EditAnywhere)
    UTexture2D* itemThumbnail;

    UPROPERTY(EditAnywhere)
    UBlueprint* itemBlueprint;

    UPROPERTY(EditAnywhere)
    FColor itemColor;
};

UCLASS(ClassGroup = Synaptech, Category = "Synaptech", BlueprintType, Blueprintable)
class USQLITE_API UMyItemsDatabase : public UDataAsset
{
    GENERATED_BODY()

    UPROPERTY(EditAnywhere)
    TArray<FMyItemInfo> items;

};


In Editor module:



static EAssetTypeCategories::Type MY_AssetCategory;


UCLASS()
class UMYEDITOR_API UMyItemsDatabase_Factory : public UFactory {
    GENERATED_UCLASS_BODY()
protected:
    virtual bool IsMacroFactory() const { return false; }
public:
    virtual UObject* FactoryCreateNew(UClass* Class, UObject* InParent, FName Name, EObjectFlags Flags, UObject* Context, FFeedbackContext* Warn) override;
};


.H:



class FATA_UMyItemsDatabase : public FAssetTypeActions_Base {
public:
    virtual FText GetName() const override { return LOCTEXT("MyDataAsset","My Items Data Asset"); }
    virtual uint32 GetCategories() override { return MY_AssetCategory; }
    virtual FColor GetTypeColor() const override { return FColor(127,255,255); }
    virtual FText GetAssetDescription(const FAssetData &AssetData) const override { return LOCTEXT("MyDataAssetDesc","My DataAsset description."); }
    virtual UClass* GetSupportedClass() const override { return UMyItemsDatabase::StaticClass(); }
};


.CPP:



UMyItemsDatabase_Factory::UMyItemsDatabase_Factory(const class FObjectInitializer &OBJ) : Super(OBJ) {
    SupportedClass = UMyItemsDatabase::StaticClass();
    bEditAfterNew = true;
    bCreateNew = true;
}

UObject* UMyItemsDatabase_Factory::FactoryCreateNew(UClass* Class, UObject* InParent, FName Name, EObjectFlags Flags, UObject* Context, FFeedbackContext* Warn)
{
    check(Class->IsChildOf(UMyItemsDatabase::StaticClass()));
    return NewObject<UMyItemsDatabase>(InParent,Class,Name,Flags|RF_Transactional,Context);
}


Then the key step you have to take is within your Editor Module’s StartupModule() function:



StartupModule() {
    IAssetTools &AssetTools = FModuleManager::LoadModuleChecked<FAssetToolsModule>("AssetTools").Get();
    MY_AssetCategory = AssetTools.RegisterAdvancedAssetCategory(FName(TEXT("MyCustomCategory")),LOCTEXT("MyCustomCategory","MyCustomCategory"));
    {
        TSharedRef<IAssetTypeActions> ACT_UMyItemsDatabase = MakeShareable(new FATA_UMyItemsDatabase);
        AssetTools.RegisterAssetTypeActions(ACT_UMyItemsDatabase);
    }
}


2 Likes

Oh, so I shouldn’t skip Factory. Works like a charm now!

Thanks for putting entire code in one place. It should make it easier for another devs :slight_smile: