Load assets from mounted Pak files

It depends on whether you require references between assets to work. If you create a DLC .pak with the root /TestDLC/ then you will have to make sure that Unreal accesses all files with that root. If you only have Texture2D assets, that don’t reference anything, then you can register any root path you like.

You are a gentleman and as scholar.
Been scratching my head for days trying to getting BP related assets to populate the registry on runtime.
Any normal assets drop into the asset registry just fine once mounting and scanning but bp ones dont work for some reason

Get all Actors from mounted .pak - Community & Industry Discussion - Epic Developer Community Forums

hey, how can i mount a pak that is in another directory? i need a way to load in paks from the steam workshop and also i need to clarify on how to register the new assets…

@BlueMountainsIO did a plugin to mount pakfiles. Yes 20Bucks. Do it yourself and you will spend multiple weeks on doing it.
Note: I did not try the plugin and I did not have a deeper look at it.

https://www.unrealengine.com/marketp…-loader-plugin

I found a solution that can work for some without code but through the Unreal Engine editor. The key to making it work is to make another Unreal project with the same name as the Original Project in which we want to put our Assets. This Second Project is the one that will be packaged with the Asset that you want to add to the Original Project. Then, in the Original Project, you need to include the package of the Second Project.

Anyway, I am looking for a solution independent of the Second Project name. I have tried to use the code proposed by @Rumbleball, but I couldn’t make it work.
After mounting the package, I tried to use this function.

GetAssetsInDirectory(const TSubclassOf<UObject> assetClass, const FString path, const bool bRecursive, TArray<UObject*>& assets)

but I don’t know what arguments to pass. I also tried to use these functions:

StaticLoadObject() and LoadSynchronous()

to load the Assets, but they return NULL.

Has anyone found a solution?

I am currently studying how to loads assets from .pak that packed by unrealpack.exe , and I have already gained some results.There are relatively few materials, if necessary, you can refer to my plan.

i have load and create a UUserWidget.

There are a few points to note.

I currently only implement creating a component and displaying it.

1.const auto& mountPoint = pPakFile->PakGetMountPoint();
FPackageName::RegisterMountPoint(“/DLG/”, pakContentPath);
i register this pack like this when PakFile is loaded. then we can find the asset at “/DLG/PakTest.PakTest_C”

  1. when we want to create the asset,we should use the code ,i have tryed to use function “loadobject” loadclass,and all failed.
		FStringClassReference clsRef = sAssetFile;
		//AMyActor is my own c++ implemented actor that inherited from AActor
		UObject* pObject = clsRef.TryLoad();

or

		FStringClassReference MyClassRef(sAsset);
		return MyClassRef.TryLoadClass<T>();
  1. Assets are best to be independent or the directory is positive or not, otherwise an error may be reported that the dependent package does not exist

4.The packaged file that named .pak must be cooked

5.UE working mode is developer

class CxPakLoader
{
public:
	CxPakLoader() {}
	~CxPakLoader() {}

	bool Init(FString const& sPakPath)
	{
		pNewPakPlatForm = new FPakPlatformFile();
		pPlatformFile = &FPlatformFileManager::Get().GetPlatformFile();
		if (!pNewPakPlatForm->Initialize(pPlatformFile, TEXT("")))
			return false;
		// Not sure about this, see: https://answers.unrealengine.com/questions/574388/how-to-mount-a-pak-file-at-game-content-directory.html
		pNewPakPlatForm->InitializeNewAsyncIO();
		// Now give the new PlatformFile to the engine.
		FPlatformFileManager::Get().SetPlatformFile(*pNewPakPlatForm);
		pPakFile = new FPakFile(pPlatformFile, *sPakPath, false);
		const auto& mountPoint = pPakFile->PakGetMountPoint();
		pNewPakPlatForm->Mount(*sPakPath, 4, *mountPoint);
		FString pakContentPath = mountPoint;
		FPackageName::RegisterMountPoint("/DLG/", pakContentPath);
		//TArray<FString> AssetList;
		//pPakFile->FindPrunedFilesAtPath(AssetList, *pPakFile->GetMountPoint(), true, false, true);
		return true;
	}

	//"/DLG/PakTest.PakTest_C"
	UObject* FindObject(FString const& sAssetFile)
	{
		FStringClassReference clsRef = sAssetFile;
		//AMyActor is my own c++ implemented actor that inherited from AActor
		UObject* pObject = clsRef.TryLoad();
		return pObject;
	}

	template <class T>
	TSubclassOf<T> TryLoadClass(FString const& sAsset) {
		FStringClassReference MyClassRef(sAsset);
		return MyClassRef.TryLoadClass<T>();
	}

	void UnInit()
	{

	}

private:
	FPakFile* pPakFile = NULL;
	IPlatformFile* pPlatformFile = NULL;
	FPakPlatformFile* pNewPakPlatForm = NULL;
};

//UClass* BP_PakTestClass = LoadClass<AActor>(nullptr, TEXT("Blueprint'/Game/DLC/BP_PakTest1.BP_PakTest1_C'"));
//if (BP_PakTestClass) GetWorld()->SpawnActor<AActor>(BP_PakTestClass, FVector::ZeroVector, FRotator::ZeroRotator);

bool UCxActor::LoadPak(const FString& PakPath)
{
	CxPakLoader xPakLoader;
	xPakLoader.Init(PakPath);
	TSubclassOf<UUserWidget> pSubClass = xPakLoader.TryLoadClass<UUserWidget>("/DLG/PakTest.PakTest_C");

	if (pSubClass) {
		UUserWidget* TestWidget = UWidgetBlueprintLibrary::Create(GetWorld(), pSubClass, NULL);
		if (TestWidget)
			TestWidget->AddToViewport();
		OutputDebugString(L"------------xPakLoader.FindObject-------Success---------\r\n");
		return true;
	}
	else {
		OutputDebugString(L"------------xPakLoader.FindObject-------Failed---------\r\n");
		return true;
	}

	return false;
}