EM_Task/AssetRegistry/Public/AssetRegistry/AssetRegistryHelpers.h
Boshuang Zhao 5144a49c9b add
2026-02-13 16:18:33 +08:00

144 lines
5.1 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "AssetRegistryModule.h"
#include "AssetRegistry/AssetData.h"
#include "AssetRegistry/ARFilter.h"
#include "AssetRegistry/IAssetRegistry.h"
#include "AssetRegistryHelpers.generated.h"
USTRUCT(BlueprintType)
struct FTagAndValue
{
GENERATED_BODY()
UPROPERTY(BlueprintReadWrite, Transient, Category = AssetData)
FName Tag;
UPROPERTY(BlueprintReadWrite, Transient, Category = AssetData)
FString Value;
};
UCLASS(transient)
class UAssetRegistryHelpers: public UObject
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintPure, Category = "Asset Registry")
static TScriptInterface<IAssetRegistry> GetAssetRegistry();
/**
* Creates asset data from a UObject.
*
* @param InAsset The asset to create asset data for
* @param bAllowBlueprintClass By default trying to create asset data for a blueprint class will create one for the UBlueprint instead
*/
UFUNCTION(BlueprintPure, Category = "Asset Data")
static FAssetData CreateAssetData(const UObject* InAsset, bool bAllowBlueprintClass = false);
/** Checks to see if this AssetData refers to an asset or is NULL */
UFUNCTION(BlueprintPure, Category = "Asset Data", meta = (ScriptMethod))
static bool IsValid(const FAssetData& InAssetData);
/** Returns true if this is the primary asset in a package, true for maps and assets but false for secondary objects like class redirectors */
UFUNCTION(BlueprintPure, Category = "Asset Data", meta = (ScriptMethod))
static bool IsUAsset(const FAssetData& InAssetData);
/** Returns true if the this asset is a redirector. */
UFUNCTION(BlueprintPure, Category = "Asset Data", meta = (ScriptMethod))
static bool IsRedirector(const FAssetData& InAssetData);
/** Returns the full name for the asset in the form: Class ObjectPath */
UFUNCTION(BlueprintPure, Category = "Asset Data", meta = (ScriptMethod))
static FString GetFullName(const FAssetData& InAssetData);
/** Convert to a SoftObjectPath for loading */
UFUNCTION(BlueprintPure, Category = "Asset Data", meta = (ScriptMethod))
static FSoftObjectPath ToSoftObjectPath(const FAssetData& InAssetData);
UFUNCTION(BlueprintPure, Category = "Asset Data", meta = (ScriptMethod))
static UClass* GetClass(const FAssetData& InAssetData);
/** Returns the asset UObject if it is loaded or loads the asset if it is unloaded then returns the result */
UFUNCTION(BlueprintPure, Category = "Asset Data", meta = (ScriptMethod))
static UObject* GetAsset(const FAssetData& InAssetData);
/** Returns true if the asset is loaded */
UFUNCTION(BlueprintPure, Category = "Asset Data", meta = (ScriptMethod))
static bool IsAssetLoaded(const FAssetData& InAssetData);
/** Returns the name for the asset in the form: Class'ObjectPath' */
UFUNCTION(BlueprintPure, Category = "Asset Data", meta = (ScriptMethod))
static FString GetExportTextName(const FAssetData& InAssetData);
/** Gets the value associated with the given tag as a string */
UFUNCTION(BlueprintPure, Category = "Asset Data", meta = (ScriptMethod))
static bool GetTagValue(const FAssetData& InAssetData, const FName& InTagName, FString& OutTagValue);
/**
* Populates the FARFilters tags and values map with the passed in tags and values
*/
UFUNCTION(BlueprintPure, Category = "Asset Registry")
static FARFilter SetFilterTagsAndValues(const FARFilter& InFilter, const TArray<FTagAndValue>& InTagsAndValues);
/** Enable/disable asset registry caching mode for the duration of the scope */
struct ASSETREGISTRY_API FTemporaryCachingModeScope
{
FTemporaryCachingModeScope(bool InTempCachingMode);
~FTemporaryCachingModeScope();
private:
bool PreviousCachingMode;
};
};
inline void GetReferencersData(UObject* Object, UClass* MatchClass, TArray<FAssetData>& OutAssetDatas)
{
FAssetRegistryModule& AssetRegistryModule = FModuleManager::LoadModuleChecked<
FAssetRegistryModule>("AssetRegistry");
IAssetRegistry& AssetRegistry = AssetRegistryModule.Get();
TArray<FAssetIdentifier> Referencers;
AssetRegistry.GetReferencers(Object->GetOuter()->GetFName(), Referencers);
for (auto AssetIdentifier: Referencers)
{
TArray<FAssetData> Assets;
AssetRegistry.GetAssetsByPackageName(AssetIdentifier.PackageName, Assets);
for (auto AssetData: Assets)
{
if (MatchClass != nullptr)
{
if (AssetData.GetClass()->IsChildOf(MatchClass))
{
OutAssetDatas.AddUnique(AssetData);
}
}
else
{
OutAssetDatas.AddUnique(AssetData);
}
}
}
}
template <class T>
void GetReferencersOfType(UObject* Object, TArray<T*>& OutObjects)
{
TArray<FAssetData> AssetDatas;
GetReferencersData(Object, T::StaticClass(), AssetDatas);
for (auto Data: AssetDatas)
{
T* TypedAsset = Cast<T>(Data.GetAsset());
if (TypedAsset != nullptr)
{
OutObjects.Add(TypedAsset);
}
}
}