Unreal DataAsset 自动收集项目资源
问题:有很多资源,然后需要配置一张表去包含这些资源,然后用 GameplayTag 去找资源。然后就是每次增加资源都需要维护这个表比较麻烦,所以弄了个自动化收集。
class UTagNamedAssetContainer : public UDataAsset
UPROPERTY(BlueprintReadWrite, EditAnywhere)
FGameplayTagRequirements NameTagRequirements;
UPROPERTY(BlueprintReadWrite, EditAnywhere, meta = (ContentDir, LongPackageName))
TArray<FDirectoryPath> PathRequirements;
UPROPERTY(BlueprintReadWrite, EditAnywhere)
TArray<FSoftObjectPath> Assets;
UFUNCTION(BlueprintCallable, CallInEditor)
void Collect();
void UTagNamedAssetContainer::Collect()
{
Assets.Empty();
TArray<FAssetData> AssetDatas;
FAssetRegistryModule& AssetRegistry = FModuleManager::GetModuleChecked<FAssetRegistryModule>("AssetRegistry");
FARFilter Filter;
for(FDirectoryPath& Path: PathRequirements)
{
Filter.PackagePaths.Add(FName(Path.Path));
}
TMap<UClass*, TSet<UClass*>> Map = GetAllDerivedClasses();
// 收集所有继承 TagNamedInterface 的对象
UClass* Class = UTagNamedInterface::StaticClass();
Filter.ClassPaths.Add(Class->GetClassPathName());
Filter.bRecursivePaths = true;
Filter.bRecursiveClasses = true;
AssetRegistry.Get().GetAssets(Filter, AssetDatas);
for(FAssetData& Asset: AssetDatas)
{
// 因为需要访问成员变量,所以这里需要加载,不加载会快很多
FSoftObjectPath Path = Asset.GetSoftObjectPath();
UObject* Object = Path.TryLoad();
if(Object && Object->Implements<UTagNamedInterface>())
{
ITagNamedInterface* Obj = Cast<ITagNamedInterface>(Object);
const FGameplayTag& Tag = Obj->GetNameTag();
if(NameTagRequirements.RequirementsMet(FGameplayTagContainer(Tag)))
{
Assets.Add(Path);
}
}
}
// 自动保存
AssetViewUtils::SavePackages({GetPackage()});
}
原文地址:https://blog.csdn.net/jk_chen_acmer/article/details/143928665
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!