简体   繁体   中英

How to edit AssetRegistry tags with function?

For a workflow (and for the learning purposes as well) i try to create a function so we can add AssetRegistry tags to the existing StaticMesh objects in Content Browser (from a EditorUtilityWidget or any blueprint) and therefore to be able sort it by these tags, without necessity to go through context menus "Asset Actions"->"Show Metadata" to see them(as we can do with "Set Metadata Tag"-Node from Editor Scripting Utilities plugin).

ExpBlueprintFunctionLibrary.h

#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "ExpBlueprintFunctionLibrary.generated.h"


UCLASS()
class DP_API UExpBlueprintFunctionLibrary : public UBlueprintFunctionLibrary
{
    GENERATED_BODY()

public:
    UFUNCTION(BlueprintCallable, Category = "")
    static void AddCustomTag(UObject* Asset);

};

ExpBlueprintFunctionLibrary.cpp

#include "AssetRegistry/IAssetRegistry.h"
#include "ExpBlueprintFunctionLibrary.h"
void UExpBlueprintFunctionLibrary::AddCustomTag(UObject* Asset)
{
    int32 NewParam = 454;
    static TArray<FAssetRegistryTag> AssetTags;
    Asset->UObject::GetAssetRegistryTags(AssetTags);
    AssetTags.Add(FAssetRegistryTag(
        "ExtraTag", 
        FString::FromInt(NewParam),
        FAssetRegistryTag::ETagType::TT_Numerical));
    for (const FAssetRegistryTag& AssetTag : AssetTags)
    {    
        UE_LOG(LogTemp, Log, TEXT("Tag : %s :: %s"), *AssetTag.Name.ToString(), *AssetTag.Value)
    }
    //Super::GetAssetRegistryTags(AssetTags);
}

the tags list that i mean to add to AssetRegistryTagsScreenshot

the code above actually adds tag to it, but it is not in the list nor filter to search

OutputLog:

LogTemp: Tag : Triangles :: 48
LogTemp: Tag : Vertices :: 54
LogTemp: Tag : UVChannels :: 2
LogTemp: Tag : Materials :: 1
LogTemp: Tag : ApproxSize :: 100x100x100
LogTemp: Tag : CollisionPrims :: 1
LogTemp: Tag : LODs :: 1
LogTemp: Tag : MinLOD :: 0
LogTemp: Tag : SectionsWithCollision :: 1
LogTemp: Tag : DefaultCollision :: BlockAll
LogTemp: Tag : CollisionComplexity :: CTF_UseSimpleAndComplex
LogTemp: Tag : AssetImportData :: []
LogTemp: Tag : LODGroup :: None
LogTemp: Tag : NeverStream :: False
LogTemp: Tag : ExtraTag :: 454

i have a feeling that i have to somehow register it and didn't quite understand what i miss.

the way i connect it in blueprint BlueprintScreenshot

with small portions of examples i could find, we managed to create a custom Class-objects and override GetAssetRegistryTags , but it doesn't work for existing instances of StaticMeshes:

.h

...
public:
        UPROPERTY(EditAnywhere, BlueprintReadOnly, AssetRegistrySearchable, Category = Max)
        int32 MaxCount;
    
        int32 DummyData;

.cpp

void UExpBlueprintFunctionLibrary::GetAssetRegistryTags(TArray<FAssetRegistryTag>& OutTags) const
{
    Super::GetAssetRegistryTags(OutTags);

    OutTags.Add(FAssetRegistryTag(
        "DummyData",
        FString::FromInt(DummyData),
        FAssetRegistryTag::ETagType::TT_Numerical
    ));
}

(here are really huge thanks for explanations Alex Stevens (@MilkyEngineer) in his tweetorial )

Is that possible what i try to achieve?

PS: any exact and conceptual explanations are welcome and would be greatly helpful! i've already spent a lot of time trying to figure out how should it work and feel a little bit desperate and useless right now

Your code doesn't add a tag on the UObject you are passing in, it just adds a tag to your static function-local AssetTags array.

The only way I see that doesn't require overriding UObject::GetAssetRegistryTags is to bind to the static OnGetExtraObjectTags delegate and add the tags in the callback. This would require you to keep your own data structure of which tags should be added to which object.

In other words, your call to AddCustomTag would add a tag on some static or singleton map that keeps an array of extra tags for each object, eg

TMap<UObject*, TArray<FAssetRegistryTag>> ExtraTags;

void UExpBlueprintFunctionLibrary::AddCustomTag(UObject* Asset)
{
    int32 NewParam = 454;
    ExtraTags.FindOrAdd(Asset).Add(FAssetRegistryTag(
        "ExtraTag", 
        FString::FromInt(NewParam),
        FAssetRegistryTag::ETagType::TT_Numerical));
}

In the callback you would check if the object passed in should receive any extra tags:

void UExpBlueprintFunctionLibrary::ModifyTags(const UObject* Obj, TArray<FAssetRegistryTag>& InOutTags)
{
    if (const TArray<FAssetRegistryTag>* pTags = ExtraTags.Find(Obj))
    {
        InOutTags.Append(*pTags);
    }
}

Keep in mind the tags won't actually be added until you save the asset.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM