【UE4学习】23_获取路径的方法

本文介绍了在UE4中,如何在C++中使用FPath API获取打包游戏后的各种路径,包括BaseDir、Content、Saved、Logs和Plugins等,并强调这些路径在项目移动后仍然有效。还提到了Sandbox File Path和动态获取路径的方法,帮助开发者正确进行文件I/O操作。


前言

有时候,我们可能会需要外部文件来实现一些功能,那么这时候就需要获取相关路径了。unreal 中在C++ 提供关于FPath这个api,基本可以满足所有情况。

本文会在翻译参考文章的同时,会加入一些实际使用的相关函数。以方便以后来查询。


Overview

小伙伴们,这里将为你提供打包游戏后如何获取多种路径的方法。你不需要在Editor模式下测试时使用### ConverRelativePathToFull这个方法,即使它依然有用。

但它对于打包(exe)模式来说,却是必不可少的。

一下是UE4 C++ FPath:: functions 返回的各种路径。

你可以使用其中任何一种在你的打包文件中来构建你自己的路径目录。


How It Works

所有其他路径都基于exe所在路径 BaseDir的。
这是在运行时决定的,因此如果项目移动位置了,以下所获取的路径依然是正确的。

InstallDir/WindowsNoEditor/GameName/Binaries/Win64
//InstallDir/WindowsNoEditor/GameName/Binaries/Win64
FString ThePath = FString(FPlatformProcess::BaseDir());
InstallDir/WindowsNoEditor/ (or other OS)
//InstallDir/WindowsNoEditor/
FString ThePath = FPaths::ConvertRelativePathToFull(FPaths::RootDir());
InstallDir/WindowsNoEditor/GameName
//InstallDir/WindowsNoEditor/GameName
FString ThePath = FPaths::ConvertRelativePathToFull(FPaths::GameDir());
InstallDir/WindowsNoEditor/GameName/Content
//InstallDir/WindowsNoEditor/GameName/Content
FString ThePath = FPaths::ConvertRelativePathToFull(FPaths::GameContentDir());
InstallDir/WindowsNoEditor/GameName/Saved
//InstallDir/WindowsNoEditor/GameName/Saved
FString ThePath = FPaths::ConvertRelativePathToFull(FPaths::GameSavedDir());
InstallDir/WindowsNoEditor/GameName/Saved/Logs
//InstallDir/WindowsNoEditor/GameName/Saved/Logs
FString ThePath = FPaths::ConvertRelativePathToFull(FPaths::GameLogDir());
InstallDir/WindowsNoEditor/GameName/Plugins(新增)
//InstallDir/WindowsNoEditor/GameName/Plugins
FString ThePath = FPaths::ConvertRelativePathToFull(FPaths::ProjectPluginsDir());

ps:
(1)4.18版本可能要求你更改为FPaths::ProjectDir();
(2)需要更多的方法可以查找FPath相关api。

Sandbox File Path

当程序以WindowsNoEditor 模式运行时,所有文件的 I/O 将存于沙盒Sandbox文件夹下。

在本例中,其实际磁盘路径为ConvertToSandboxPath所包裹,定义于 IPlatformFileSandboxWrapper.cpp当中。此处的ConvertToSandboxPath 将有别于 FPaths::ConvertToSandboxPath

为了获取磁盘上实际的文件名,你需要调用GetFilenameOnDisk 来获取实际的文件名。
注意:该文件必须写入磁盘。否则,GetFilenameOnDisk 将返回路径名。

IFileManager& FileManager = IFileManager::Get();
FString DiskFilename = FileManager.GetFilenameOnDisk(*FullFilename);

Dynamic Relocation of Project(动态获取路径)

以上方法在你移动打包游戏后,依然有效。


Conclusion

现在,你将知道如何基于你的游戏路径来指定你想要的路径了。

参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值