UE4學習筆記(23)獲取路徑的方法

有時候,我們可能會需要外部文件來實現一些功能,那麼這時候就需要獲取相關路徑了。unreal 中在C++ 提供關於FPath這個api,基本可以滿足所有情況。本文會在翻譯參考文章的同時,會加入一些實際使用的相關函數,以方便以後來查詢。

 

Overview

 

小夥伴們,這裏將爲你提供打包遊戲後如何獲取多種路徑的方法。你不需要在Editor模式下測試時使用### ConverRelativePathToFull這個方法,即使它依然有用。

但它對於打包(exe)模式來說,卻是必不可少的。

一下是UE4 C++ FPath:: functions 返回的各種路徑。

你可以使用其中任何一種在你的打包文件中來構建你自己的路徑目錄。

 

How It Works

 

所有其他路徑都基於exe所在路徑 BaseDir的。

這是在運行時決定的,因此如果項目移動位置了,以下所獲取的路徑依然是正確的。

 

InstallDir/WindowsNoEditor/GameName/Binaries/Win64


 

 

1

//InstallDir/WindowsNoEditor/GameName/Binaries/Win64

2

FString ThePath = FString(FPlatformProcess::BaseDir());

 

InstallDir/WindowsNoEditor/ (or other OS)


 

 

1

//InstallDir/WindowsNoEditor/

2

FString ThePath = FPaths::ConvertRelativePathToFull(FPaths::RootDir());

 

InstallDir/WindowsNoEditor/GameName


 

 

1

//InstallDir/WindowsNoEditor/GameName

2

FString ThePath = FPaths::ConvertRelativePathToFull(FPaths::GameDir());

 

InstallDir/WindowsNoEditor/GameName/Content


 

 

1

//InstallDir/WindowsNoEditor/GameName/Content

2

FString ThePath = FPaths::ConvertRelativePathToFull(FPaths::GameContentDir());

 

InstallDir/WindowsNoEditor/GameName/Saved


 

 

1

//InstallDir/WindowsNoEditor/GameName/Saved

2

FString ThePath = FPaths::ConvertRelativePathToFull(FPaths::GameSavedDir());

 

InstallDir/WindowsNoEditor/GameName/Saved/Logs


 

 

1

//InstallDir/WindowsNoEditor/GameName/Saved/Logs

2

FString ThePath = FPaths::ConvertRelativePathToFull(FPaths::GameLogDir());

 

InstallDir/WindowsNoEditor/GameName/Plugins(新增)


 

 

1

//InstallDir/WindowsNoEditor/GameName/Plugins

2

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 將返回路徑名。


 

 

1

IFileManager& FileManager = IFileManager::Get();

2

FString DiskFilename = FileManager.GetFilenameOnDisk(*FullFilename);

 

Dynamic Relocation of Project(動態獲取路徑)

 

以上方法在你移動打包遊戲後,依然有效。

 

Conclusion

 

現在,你將知道如何基於你的遊戲路徑來指定你想要的路徑了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章