Unreal: Dynamic load map from Pak file

目标:在程序运行时加载自定义 Pak 文件,并打开指定关卡,显示其中的完整 map 内容

Unreal 的 Pak 文件内包括了物体,材质,blueprint,map等等。Level 以 map 的形式保存。

Firsr of all, Pak 相关的调试需要 Package Project 执行,不能在 Editor 里面直接 Launch,否则 FCoreDelegates::MountPak IsBound 为 false , 则无法执行后续操作。

Code

在 GameMode 的 InitGame() 内调用如下代码。

void MyMainClass::MyMainClass::Exe(UWorld *world) {
UE_LOG(LogTemp, Warning, TEXT("Init Main class"));
if (FCoreDelegates::MountPak.IsBound())
{
UE_LOG(LogTemp, Warning, TEXT("LoadPakDelegate(): OnMountPak.IsBound()"));
FString pakPath = TEXT("../../../MyProject3/Content/Paks/MyProject1-MacNoEditor.pak");
IPakFile *pakFile = FCoreDelegates::MountPak.Execute(pakPath, 4);
if (pakFile)
{
const auto& mountPoint = pakFile->PakGetMountPoint();
UE_LOG(LogTemp, Warning, TEXT("LoadPakDelegate(): MountPoint %s"), *mountPoint);
FString pakContentPath = mountPoint + "MyProject1/Content/";
// FPackageName::RegisterMountPoint("/Game/", "../../../MyProject1/Content/");
FPackageName::RegisterMountPoint("/Game/", pakContentPath); UGameplayStatics::OpenLevel(world, FName(TEXT("Map2")) );
}
}
else
{
UE_LOG(LogTemp, Warning, TEXT("LoadPakDelegate(): OnMountPak.IsBound() Falied"));
}
}

RegisterMountPoint 的分析

static void  RegisterMountPoint
(
    const FString & RootPath,
    const FString & ContentPath
)

This will insert a mount point at the head of the search chain (so it can overlap an existing mount point and win).

参数解释:

RootPath: Logical Root Path.

ContentPath: Content Path on disk.

Logical Path 是程序运行时会去找的 path 并非实际路径,根据反复尝试,只有在这个参数写成 “/Game/” 的时候才能正确加载到所有资源,若随意命名可能只能加载到 map 资源,连 map_builtdata 都找不到。会有如下报错:

LogStreaming: Error: Couldn't find file for package /Game/StarterContent/Maps/Map2_BuiltData requested by async loading code. NameToLoad: /Game/StarterContent/Maps/Map2_BuiltData

我的猜测是当前运行的程序的默认加载路径是 Game 所以这样写可以成功,应该也有方法自定义加载的路径,但是我暂时不知道。

Content Path 是指需要 mount 到前面这个目录的内容的父路径,这个路径取决于加载的Pak 的当前挂载点和 Pak 内部的文件路径。

通过 PakGetMountPoint() 函数得到挂载点为 "../../../"

而且 Pak 内部文件如下

MyProject1/Content/StarterContent/Blueprints/...
MyProject1/Content/StarterContent/Maps/...
MyProject1/Content/StarterContent/Materials/...
MyProject1/Content/StarterContent/Shapes/...
...

故最终路径为 “../../../MyProject1/Content/”

Missing shader resource

成功加载 map 但是模型材质和 shader 丢失,log 如下:

[UE4] [2021.05.17-03.57.53:734][  0]LogShaders: Error: Missing shader resource for hash '589973CAE03D7F0ECFEC6B825B774136FF9FCB9D' for shader platform 16 in the shader library
LogMaterial: Error: Tried to access an uncooked shader map ID in a cooked application
[UE4] [2021.05.17-08.02.25:186][ 0]LogMaterial: Can't compile BasicShapeMaterial with cooked content, will use default material instead

这个问题原因是在要加载 Pak 的工程设置里面启用了 Share Material Shader Code,启用这个选项会 “Save shader only once” 这样的优化选项导致了外部的 shader 无法被找到。

在工程中关闭此开关即可。

Ref:

https://answers.unrealengine.com/questions/363767/how-to-load-a-map-from-a-dynamic-level.html 参见 TestyRabbit May 03 '18 at 4:07 PM 的评论

sample code: https://pastebin.com/ZWAPtynK

https://answers.unrealengine.com/questions/258386/loading-map-from-pak-at-runtime.html top 回答

https://answers.unrealengine.com/questions/963414/view.html

Unreal: Dynamic load map from Pak file的更多相关文章

  1. BUG:给Nexus7编译Android4.2的时候出现 fatal error: map: No such file or directory

    情况是这样的,某人最近入手一台nexus7,于是在cyanogenmod 将nexus7的原代码下载到本地,编译环境是UBUNTU 12,04 然后编译的时候,出现了如下的错误导致编译失败 <p ...

  2. ObjectARX2012错误1 fatal error C1083: 无法打开包括文件:“arxHeaders.h”: No such file or directory; fatal error C1083: 无法打开包括文件:“map”: No such file or directory

    问题1:fatal error C1083: 无法打开包括文件:“arxHeaders.h”: No such file or directory: 解决办法:这个问题很明显,是因为没有在工程属性里包 ...

  3. dynamic load javascript file.

    $.ajax({ url : ("js/public/" + window.localStorage.getItem("lang") + ".js&q ...

  4. How to load a local .CSS file & JavaScript resources using iPhone UIWebView Class

    This post will cover the basic setup and creation of an application with web content for iPhone that ...

  5. dynamic load jar and init spring

    public class SpringLoader { private Map<String, Class<?>> classMap = new HashMap<> ...

  6. Load an X509 PEM file into Windows CryptoApi

    http://stackoverflow.com/questions/1231178/load-an-x509-pem-file-into-windows-cryptoapi I discovered ...

  7. struts2 Unable to load configuration. - bean - jar:file:struts2-core-2.2.3.jar!/struts-default.xml:29:72

    今天启动tomcat的时候发现如下错误记录一下! 从stackoverflow上找到 原因是加入了多个struts2包 删除相同的包即可!!

  8. gcc dynamic load library

    Linux下一般都是直接在编译生成时挂接上链接库,运行时,把链接库放到系统环境里就可以了 但是windows出现带来了动态链接的概念,也就兴起了非windows世界的插件的概念的范潮 对应于windo ...

  9. 解决HDFS无法启动namenode,报错Premature EOF from inputStream;Failed to load FSImage file, see error(s) above for more info

    一.情况描述 启动hadoop后发现无法打开hdfs web界面,50070打不开,于是jps发现少了一个namenode: 查看日志信息,发现如下报错: 2022-01-03 23:54:10,99 ...

随机推荐

  1. C语言之简易了解程序环境

    C语言之简易了解程序环境 大纲: 程序的翻译环境 预编译 编译 汇编 链接 程序的运行环境 在ANSI C的任何一种实现中,存在两个不同的环境. 第1种是翻译环境,在这个环境中源代码被转换为可执行的机 ...

  2. 你说,怎么把Bean塞到Spring容器?

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 小傅哥,你是怎么学习的? 有很多初学编程或者码了几年CRUD砖的小伙伴问我,该怎么学 ...

  3. Android 学习之活动的生命周期

    •返回栈 Android 中的活动是可以叠层的: 我们每启动一个新的活动,就会覆盖在原活动之上: 然后点击 Back 键会销毁最上面的活动,下面一个活动就会重新显示出来: 其实 Android 是使用 ...

  4. CMS前世今生

    CMS一直是面试中的常考点,今天我们用通俗易懂的语言简单介绍下. 垃圾回收器为什么要分区分代? 如上图:JVM虚拟机将堆内存区域分代了,先生代是朝生夕死的区域,老年代是老不死的区域,不同的年代对象有不 ...

  5. CVPR2021| 继SE,CBAM后的一种新的注意力机制Coordinate Attention

    前言: 最近几年,注意力机制用来提升模型性能有比较好的表现,大家都用得很舒服.本文将介绍一种新提出的坐标注意力机制,这种机制解决了SE,CBAM上存在的一些问题,产生了更好的效果,而使用与SE,CBA ...

  6. Spring Boot 实现配置文件加解密原理

    Spring Boot 配置文件加解密原理就这么简单 背景 接上文<失踪人口回归,mybatis-plus 3.3.2 发布>[1] ,提供了一个非常实用的功能 「数据安全保护」 功能,不 ...

  7. Go-08-函数与指针

    Go语言的函数本身可以作为值进行传递,既支持匿名函数和闭包,又能满足接口. 函数声明 func 函数名 (参数列表)(返回参数列表){ // 函数体 } func funcName(parameter ...

  8. 三. 初步认识Eureka注册中心

    Eureka Eureka是Netflix公司出品,英文直译:发现了,找到了! 认识Eureka (一)首先我们来解决第一问题,服务的管理. Ⅰ.早期开发模式中出现的问题 再早期开发时业务时,首先模块 ...

  9. 09- Linux下压缩和解压命令

    压缩和解压命令 gzip命令: 作用: tar命令 作用:对文件或文件夹进行打包和解包的操作. 格式:tar [选项] 文件名 [文件名或路径] 练习题:

  10. nginx 完美解决tp3.2.3 404问题

    最近我把Apache给换成nginx,当我把tp项目搬过去运行的时候发现404 错误 ,原来是因为nginx不支持 pathinfo 模式,需要自己配置 下面我配置 在server配置里面 locat ...