UE4 User-Defined Asset Type Action

UE4 User-Defined Asset Type Action

1 Asset Class Definition


Creating a class derived from UObject.

UCLASS(BlueprintType)
class UUserDefinedAsset : public UObject
{
	GENERATED_UCLASS_BODY()
}

Add to this class to runtime module.


2 Asset Type Action Definition


Add asset type action class to create editor entry for this asset.

class FAssetTypeActions_UserDefinedAsset : public FAssetTypeActions_Base
{
	public:
		virtual FText GetName() const override;
		virtual FColor GetTypeColor() const override;
		virtual UClass* GetSupportedClass() const override;
		virtual void OpenAssetEditor(const TArray<UObject*>& InObjects, TSharedPtr<class IToolkitHost> EditWithinLevelEditor = TSharedPtr<IToolkitHost>()) override;
		virtual uint32 GetCategories() override;
};

  • GetName() : return the asset type name shown in editor.

  • GetTypeColor() : return the asset type color shown in editor.

  • GetSupportedClass() : return your asset class (UUserDefinedAsset).

  • OpenAssetEditor(): entry to open asset editor.

  • GetCategories(): category shown in editor while creating asset, you can define custom category bit and return.

Add this class to editor module and register this asset type in StartupModule().

void FXXX_ModuleEidtorModule::StartupModule()
{
	...
	//Register
	IAssetTools& AssetToolsModule =  FModuleManager::LoadModuleChecked<FAssetToolsModule>("AssetTools").Get();
	//AssetCategory
	AssetCategoryBit = AssetToolsModule.RegisterAdvancedAssetCategory(FName(TEXT("UserDefinedAsset")), LOCTEXT("UserDefinedAssetCategory", "User Defined Asset");
	TSharedPtr<FAssetTypeActions_UserDefinedAsset> AssetTypeAction = MakeShareable(new FAssetTypeActions_UserDefinedAsset(AssetCategoryBit));
	AssetToolsModule.RegisterAssetTypeActions(AssetTypeAction.ToSharedRef());
}

After adding this class and compiling your project, you can see your custom asset in the editor.

AssetTypeAction

Notice that you can add different asset type action for any asset class derived from UObject.


3 Asset Factory


In order to UE4 recognize user defined asset and create it, you need to add Factory class to do this.

UCLASS()
class UUserDefinedAssetFactory : public UFactory
{
	GENERATED_UCLASS_BODY()
	// UFactory interface
	virtual UObject* FactoryCreateNew(UClass* Class, UObject* InParent, FName Name, EObjectFlags Flags, UObject* Context, FFeedbackContext* Warn) override;
	virtual bool CanCreateNew() const override;
	// End of UFactory interface
};

UObject* UTestFactory::FactoryCreateNew(UClass* Class, UObject* InParent, FName Name, EObjectFlags Flags, UObject* Context, FFeedbackContext* Warn)
{
	check(Class->IsChildOf(UUserDefinedAsset::StaticClass()));
	return NewObject<UUserDefinedAsset>(InParent, Class, Name, Flags);;
}
  • FactoryCreateNew: create user defined asset object.

  • CanCreateNew: conditions to create user defined asset.

Also, add this class to editor module .




Tags: UE4 Editor Vistied:
Share: Twitter Facebook LinkedIn