Unreal中Blueprint asset的Tag从哪里来

一、资产创建时

在将一个对象添加到资源文件的时候,会调用UObject中声明的GetAssetRegistryTags函数,不同的实现可以重新实现这个接口,从而完成自定义标签项的写入。
/** Constructor taking a UObject. By default trying to create one for a blueprint class will create one for the UBlueprint instead, but this can be overridden */
FAssetData(const UObject* InAsset, bool bAllowBlueprintClass = false)
{
……
TArray<UObject::FAssetRegistryTag> ObjectTags;
InAsset->GetAssetRegistryTags(ObjectTags);
……
}

例如,蓝图派生类就添加了一些标准的Tag,这些Tag在资源加载的时候都可以使用。
void UBlueprint::GetAssetRegistryTags(TArray<FAssetRegistryTag>& OutTags) const
{
……
OutTags.Add(FAssetRegistryTag(FBlueprintTags::BlueprintPathWithinPackage, GetPathName(GetOutermost()), FAssetRegistryTag::TT_Hidden));
OutTags.Add(FAssetRegistryTag(FBlueprintTags::GeneratedClassPath, GeneratedClassVal, FAssetRegistryTag::TT_Hidden));
OutTags.Add(FAssetRegistryTag(FBlueprintTags::ParentClassPath, ParentClassName, FAssetRegistryTag::TT_Alphabetical));
OutTags.Add(FAssetRegistryTag(FBlueprintTags::NativeParentClassPath, NativeParentClassName, FAssetRegistryTag::TT_Alphabetical));
……
}

二、写入磁盘时

void UPackage::SaveAssetRegistryData(UPackage* InOuter, FLinkerSave* Linker, FStructuredArchive::FSlot Slot)
{
……
TArray<FAssetRegistryTag> SourceTags;
Object->GetAssetRegistryTags(SourceTags);

TArray<FAssetRegistryTag> Tags;
for (FAssetRegistryTag& SourceTag : SourceTags)
{
FAssetRegistryTag* Existing = Tags.FindByPredicate([SourceTag](const FAssetRegistryTag& InTag) { return InTag.Name == SourceTag.Name; });
if (Existing)
{
Existing->Value = SourceTag.Value;
}
else
{
Tags.Add(SourceTag);
}
}
……
}

三、资产加载时

bool FPackageReader::ReadAssetRegistryData (TArray<FAssetData*>& AssetDataList)
{
……

// UAsset files usually only have one asset, maps and redirectors have multiple
for(int32 ObjectIdx = 0; ObjectIdx < ObjectCount; ++ObjectIdx)
{
FString ObjectPath;
FString ObjectClassName;
int32 TagCount = 0;
*this << ObjectPath;
*this << ObjectClassName;
*this << TagCount;

FAssetDataTagMap TagsAndValues;
TagsAndValues.Reserve(TagCount);

for(int32 TagIdx = 0; TagIdx < TagCount; ++TagIdx)
{
FString Key;
FString Value;
*this << Key;
*this << Value;

if (!Key.IsEmpty() && !Value.IsEmpty())
{
TagsAndValues.Add(FName(*Key), Value);
}
}
……
}
……
}

原文地址:https://www.cnblogs.com/tsecer/p/14860501.html