# How Load Asset From Pak File at Runtime?

**URL:** <https://forums.unrealengine.com/t/how-load-asset-from-pak-file-at-runtime/343451>\
**Category:** C++\
**Tags:** dlc, asynchronous-asset-l, pak, unreal-engine, question, CPP\
**Created:** [January 8, 2016, 7:49am UTC](https://forums.unrealengine.com/t/how-load-asset-from-pak-file-at-runtime/343451 "2016-01-08T07:49:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![linqing](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@linqing](https://forums.unrealengine.com/u/linqing)\
**Post date:** [January 8, 2016, 7:49am UTC](https://forums.unrealengine.com/t/how-load-asset-from-pak-file-at-runtime/343451/1 "2016-01-08T07:49:41Z")

</div>

This problem has been bothering me at Several weeks。

The StaticLoadObject() it aways is Null with return Value;

Who can help me ?

![73451-x3vlk10l0`{h$j00{s_p}iu.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/b/71bce17471b35b6ad48bb77993be716d649f72d5.png)

```
void AMyFPS_Character::test_asynLoad()
{
	FPakPlatformFile *temp_pakFile = new FPakPlatformFile();
	IPlatformFile* temp_platform = &FPlatformFileManager::Get().GetPlatformFile();

	IPlatformFile *temp_platform1 = NULL;

	temp_pakFile->Initialize(temp_platform, TEXT(""));
	if (!FCoreDelegates::OnMountPak.IsBound())
	{
		return;
	}
	FString engine_dir(FPaths::GameContentDir().Append("PAK/")); // = FPaths::GameContentDir().Append("PAK/");
	FString mount_file = FPaths::GameContentDir().Append(TEXT("DLC/OutPak.pak"));
	
	GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, *engine_dir);
	GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, *mount_file);
	
	FPakFile pakFile(*mount_file, false);
	if (!pakFile.IsValid())
	{
		GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("FPakFile IsInvalid"));
		return;
	}
	pakFile.SetMountPoint(*engine_dir);

	if (!temp_pakFile->Mount(*mount_file, 0, *engine_dir))
	{
		GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, TEXT("Mount Failed"));
		return;
	}

	FStringAssetReference itemRef("/Game/Content/PAK/2_2.2_2");
	TSet<FString> FileList; 
	pakFile.FindFilesAtPath(FileList, *pakFile.GetMountPoint(), true, false, true);
	for (auto item : FileList)
	{
		if (item.EndsWith(FPackageName::GetAssetPackageExtension()) ||
			item.EndsWith(FPackageName::GetMapPackageExtension()))
		{
			GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Cyan, FString::Printf(TEXT("PakFile Assert: %s "), *item));
		}
	}
	FStreamableManager *streamableMgr = new FStreamableManager();
	auto temp_matrial_test1 = streamableMgr->SynchronousLoad(itemRef);
	GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Green, FString::Printf(TEXT("Try Convert Object to UMaterial : %s "), temp_matrial_test1 != nullptr ? TEXT("True") : TEXT("False")));
	
	streamableMgr->RequestAsyncLoad(itemRef, FStreamableDelegate::CreateLambda([&]()
	{
		auto temp_matrial_test = StaticLoadObject(UMaterial::StaticClass(), NULL, TEXT("/Game/Content/PAK/2_2.2_2"));
		GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, FString::Printf(TEXT("Try Convert Object to UMaterial : %s "), temp_matrial_test != nullptr ? TEXT("True") : TEXT("False")));
	}));
}
```

---

<div class="post-metadata">

**Author:** ![Leicy](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Leicy](https://forums.unrealengine.com/u/Leicy)\
**Post date:** [January 12, 2016, 7:08am UTC](https://forums.unrealengine.com/t/how-load-asset-from-pak-file-at-runtime/343451/2 "2016-01-12T07:08:49Z")

</div>

你解决了吗？我也在研究类似的问题

---

<div class="post-metadata">

**Author:** ![Anonymous\_22f547d62f00bac65c2343d67ba1c173](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Anonymous\_22f547d62f00bac65c2343d67ba1c173](https://forums.unrealengine.com/u/Anonymous_22f547d62f00bac65c2343d67ba1c173)\
**Post date:** [October 20, 2016, 1:11pm UTC](https://forums.unrealengine.com/t/how-load-asset-from-pak-file-at-runtime/343451/3 "2016-10-20T13:11:49Z")

</div>

/Game/ 就是对应的…/…/…/游戏名字/content/ 目录 也就是从编辑Game目录的目录结构 推荐加载pak到GameContent 目录  
那么 通过UnrealPak.exe 创建Pak 时用 -create = 时 文件的内容是 真实目录 …/…/…/游戏名字/content/xxxx

用 你通过获取pak 里的文件索引 就得到 后面的 …/…/…/游戏名字/content/xxxx 那么 如果你加载到gamecontent 目录的话  
那么 路径是/Game/xxxx 如果你加载到enginecontent 目录的话 就是/Enigine/xxxx 如果你的资源还有map 的话 首先通过资源路径得到UObject 然后得到FAssetData 然后通过OpenMap 那个静态方法加载地图 参数为map 的路径 否则没在内存的话 会全盘搜索 费时间  
[http://dorgon.horizon-studio.net/archives/693](http://dorgon.horizon-studio.net/archives/693)  
[https://www.epicgames.com/unrealtournament/forums/showthread.php?19505-How-to-retrieve-the-map-name-URL-from-pak-files-(short-list-all-maps)](https://www.epicgames.com/unrealtournament/forums/showthread.php?19505-How-to-retrieve-the-map-name-URL-from-pak-files-(short-list-all-maps))
