UE4/UE5 python打包Pak和Runtime加载Pak

利用python进行打包:

import unreal
import os,shutil

#config 配置项
ProRoot='D:/UEProject'
EnginRoot='D:/Epic Games/UE_5.0EA'
ProName='testpak.uproject'
UnrealEditorCMD='UnrealEditor-Cmd.exe'

UE5Win64Path=EnginRoot+'/Engine/Binaries/Win64'
UE5BatchFiles=EnginRoot+'/Engine/Build/BatchFiles'
ProjectPath=ProRoot+'/'+ProName


#首先是Cook
os.chdir(UE5BatchFiles)
cookebat='RunUAT.bat -ScriptsForProject="'+ProjectPath+'" Turnkey -command=VerifySdk -platform=Win64 -UpdateIfNeeded -EditorIO -project="'+ProjectPath+'" BuildCookRun -nop4 -utf8output -nocompileeditor -cook  -project="'+ProjectPath+'"  -ue4exe="'+UnrealEditorCMD+'" -platform=Win64 -ddc=InstalledDerivedDataBackendGraph -installed -skipstage" -nocompile'
os.system(cookebat)


#打包后pak路径
exp_path=ProRoot+'/PakCache/Mod/'+UUID+'.pak'

#执行打包  UnrealPak.exe相关参数可百度,当前为 压缩未加密的方式
os.chdir(UE5Win64Path)
os.system('UnrealPak.exe '+exp_path+'  -create='+ProRoot+'/Saved/Cooked/Windows/Ehome/Content/Mod/'+UUID+' -compress' )


Mount Pak(c++):

bool UEHomePak::LoadPakComplete(FString PakPath,FString MountPoint)
{

	FPakPlatformFile* pakFile = (FPakPlatformFile*)(FPlatformFileManager::Get().FindPlatformFile(FPakPlatformFile::GetTypeName()));

	if (pakFile->Mount(*PakPath, 4, *MountPoint))
	{
		return true;
	}
	else
	{
		return false;
	}

}

Moun后进行资源加载(Assetload),加载后需要 cast_to强制转化为对应的类型。

UObject* LoadObj = StaticLoadObject(UObject::StaticClass(), NULL, *NewPath);
if (LoadObj) {
	UStaticMesh* Mesh = Cast(LoadObj);
}

你可能感兴趣的:(UE4&UE5)