一、首先设置UE4引擎的系统环境变量
找到:D:\UE\UE_4.27\Engine\Binaries\Win64,
WindowS+R打开CMD命令
运行UnrealPak,运行结果如下
二、打开虚幻引擎,Cook资源
注意如果想要加载Pak内资源,那么这些资源必须是经过Cook的。如果打包的是未Cook的资源,那么即使Pak挂载成功,也不可能会成功加载Pak内资源。
Cook好之后,存储的路径在你的I:\DBJTest\Saved\Cooked\WindowsNoEditor\DBJTest\Content\DLC
使用命令开始打包
第一种:打包单个文件夹,此处的文件夹为DLC
打包Pak的命令行格式
UnrealPak “存储Pak的路径+Pak包名” -create="Cook好的资源的路径"
成功之后会出现文件DLCtest.pak
第二种:打包整个Content文件
unrealpak "I:\DLCtest.pak" -create=I:\DBJTest\Saved\Cooked\WindowsNoEditor\DBJTest\Content
第三种:打包单个文件
unrealpak "I:\DLCtest.pak" -create=I:\DBJTest\Saved\Cooked\WindowsNoEditor\DBJTest\Content\DLC\download.uasset
打包完成后就能在你对应的文件夹中找到相对应的文件
三、挂载Pak包
1.自动挂载
如果手动将Pak放在以下这些项目路径中,引擎将会自动加载Pak
[ProjectName]/Content/Paks
[ProjectName]/Saved/Paks
Engine/Content/Paks
2.手动挂载
在Bulid.cs里面加入模块PakFile
public class DBJTest : ModuleRules
{
public DBJTest(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange
(
new string[]
{
"Core",
"CoreUObject",
"Engine",
"InputCore",
"HeadMountedDisplay",
"PakFile",
}
);
}
}
MyPlayerController.h文件
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include "MyPlayerController.generated.h"
/**
*
*/
UCLASS()
class DBJTEST_API AMyPlayerController : public APlayerController
{
GENERATED_BODY()
public:
virtual void BeginPlay() override;
UFUNCTION(Exec)
void TestLoadPak(FString InPakFullPath);
TSharedPtr<class FPakPlatformFile> PakPlatform;
class IPlatformFile* OldPlatform;
};
MyPlayerController.cpp文件
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyPlayerController.h"
#include "IPlatformFilePak.h"
#include "PlatformFilemanager.h"
#include "Runtime/Engine/Classes/Engine/StaticMeshActor.h"
#include "GenericPlatformFile.h"
void AMyPlayerController::BeginPlay()
{
Super::BeginPlay();
OldPlatform = &FPlatformFileManager::Get().GetPlatformFile();
PakPlatform = MakeShareable(new FPakPlatformFile());
PakPlatform->Initialize(&FPlatformFileManager::Get().GetPlatformFile(), TEXT(""));
}
void AMyPlayerController::TestLoadPak(FString InPakFullPath)
{
FPlatformFileManager::Get().SetPlatformFile(*PakPlatform.Get());
FString PakFileFullPath = InPakFullPath;
FPakFile* TmpPak = new FPakFile(PakPlatform.Get(), *PakFileFullPath, false);
FString PakMountPoint = TmpPak->GetMountPoint();
int32 Pos = PakMountPoint.Find("Content/");
FString NewMountPoint = PakMountPoint.RightChop(Pos);
NewMountPoint = FPaths::ProjectDir() + NewMountPoint;
//FString NewMountPoint = "../../../DBJTest/Content/DLC/";
TmpPak->SetMountPoint(*NewMountPoint);
if (PakPlatform->Mount(*PakFileFullPath, 1, *NewMountPoint))
{
TArray<FString> FoundFilenames;
TmpPak->FindFilesAtPath(FoundFilenames, *TmpPak->GetMountPoint(), true, false, false);
if (FoundFilenames.Num() > 0)
{
for (FString& Filename : FoundFilenames)
{
if (Filename.EndsWith(TEXT(".uasset")))
{
FString NewFileName = Filename;
NewFileName.RemoveFromEnd(TEXT(".uasset"));
int32 Pos = NewFileName.Find("/Content/");
NewFileName = NewFileName.RightChop(Pos + 8);
NewFileName = "/Game" + NewFileName;
UObject* LoadedObj = StaticLoadObject(UObject::StaticClass(), NULL, *NewFileName);
UStaticMesh* SM = Cast<UStaticMesh>(LoadedObj);
if (SM)
{
AStaticMeshActor* MeshActor = GetWorld()->SpawnActor<AStaticMeshActor>(AStaticMeshActor::StaticClass(), FVector(0,0,460), FRotator(0,0,0) );
MeshActor->SetMobility(EComponentMobility::Movable);
MeshActor->GetStaticMeshComponent()->SetStaticMesh(SM);
break;
}
}
}
}
}
FPlatformFileManager::Get().SetPlatformFile(*OldPlatform);
}
世界设置要选择GameMode
运行Play,按下~按键,输入命令testLoadPak I:\DLCtest.pak