Uses the Asset Registry to determine whether a SoftClass is a child of another class, without loading it. NOTE: This snippet has limited utility, traversing the asset registry may be slower than just loading the class in some cases!


void UMyAssetManager::SetAssetRegistryTempCachingEnabled(const bool bEnabled)
{
	IAssetRegistry::Get()->SetTemporaryCachingMode(bEnabled);
}

bool UMyAssetManager::SoftClassIsChildOf(const TSoftClassPtr<UObject>& SoftClass, const TSubclassOf<UObject>& Class)
{
	if (SoftClass.IsNull() || Class == nullptr)
	{
		// Class is invalid, do nothing
		return false;
	}
	else if (const UClass* LoadedClass = SoftClass.Get())
	{
		// Class is already loaded, use the fast path
		return LoadedClass->IsChildOf(Class);
	}
	else
	{
		// Use Asset Registry for Unloaded Classes
		// Caution: This may be slow if TempCaching is disabled.
		FAssetData AssetData;
		if (UAssetManager::Get().GetAssetDataForPath(SoftClass.ToSoftObjectPath(), AssetData))
		{
			if (AssetData.AssetClass == Class->GetFName())
			{
				return true;
			}
			else
			{
				// Unfortunately the temp class hierarchy data is private, so we can't walk it manually.
				TArray<FName> AncestorClasses;
				IAssetRegistry::Get()->GetAncestorClassNames(AssetData.AssetClass, AncestorClasses);

				return AncestorClasses.Contains(Class->GetFName());
			}
		}

		return false;
	}
}
Author James
Published
Categories Snippet
Views 283
0