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

来源:互联网 发布:程序员必备手机应用 编辑:程序博客网 时间:2024/06/03 04:17

  • 前言
  • Overview
  • How It Works
        • InstallDirWindowsNoEditorGameNameBinariesWin64
        • InstallDirWindowsNoEditor or other OS
        • InstallDirWindowsNoEditorGameName
        • InstallDirWindowsNoEditorGameNameContent
        • InstallDirWindowsNoEditorGameNameSaved
        • InstallDirWindowsNoEditorGameNameSavedLogs
        • InstallDirWindowsNoEditorGameNamePlugins新增
  • Sandbox File Path
  • Dynamic Relocation of Project动态获取路径
  • Conclusion
  • 参考


前言

有时候,我们可能会需要外部文件来实现一些功能,那么这时候就需要获取相关路径了。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/Win64FString ThePath = FString(FPlatformProcess::BaseDir());

InstallDir/WindowsNoEditor/ (or other OS)

//InstallDir/WindowsNoEditor/FString ThePath = FPaths::ConvertRelativePathToFull(FPaths::RootDir());

InstallDir/WindowsNoEditor/GameName

//InstallDir/WindowsNoEditor/GameNameFString ThePath = FPaths::ConvertRelativePathToFull(FPaths::GameDir());

InstallDir/WindowsNoEditor/GameName/Content

//InstallDir/WindowsNoEditor/GameName/ContentFString ThePath = FPaths::ConvertRelativePathToFull(FPaths::GameContentDir());

InstallDir/WindowsNoEditor/GameName/Saved

//InstallDir/WindowsNoEditor/GameName/SavedFString ThePath = FPaths::ConvertRelativePathToFull(FPaths::GameSavedDir());

InstallDir/WindowsNoEditor/GameName/Saved/Logs

//InstallDir/WindowsNoEditor/GameName/Saved/LogsFString ThePath = FPaths::ConvertRelativePathToFull(FPaths::GameLogDir());

InstallDir/WindowsNoEditor/GameName/Plugins(新增)

//InstallDir/WindowsNoEditor/GameName/PluginsFString 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

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

参考

  • Packaged Game Paths, Obtain Directories Based on Executable Location

  • API: FPaths

原创粉丝点击