介绍UE4工程目录结构[1]以及常用的目录操作接口[2]。UE4将目录分为:
其中一些子目录是两个都有的,称之为通用目录,有一些目录是引擎特有的。当我们从github
https://github.com/EpicGames/UnrealEngine.git
上检出工程到本地时,起始目录叫做根目录。此外,开发过程中,为了方便起见,资源之间的引用是通过沙盒路径(虚拟路径)来进行标识的。
根目录说明如下:
Setup 和 GenerateProjectFiles还有对应的 .sh 文件,作用与.bat相同,用于在Mac平台下是生成从xcode工程文件。.command是Mac平台下的一种快捷方式,可以支持双击打开,而不在命令行模式执行。
一些子目录是在引擎和游戏项目目录之间通用的:
游戏项目目录中的源文件按模块分组,一个模块一个目录。每个模块包含以下内容:
.cpp
文件,包括游戏类实现文件和模块实现文件。引擎Source目录下分为以下目录:
部分子目录特定于Engine目录。
.usf
)。当我们在编辑器状态下浏览资源时,显示的资源路径,通常是沙盒路径
/Game/ThirdPerson/Meshes/LeftArm_StaticMesh
/Engine/Maps/Entry
其中 /Game 是一个虚拟路径,实际表示的是项目的 FPaths::ProjectContentDir() 。/Engine 也是一个虚拟路径,实际路径是引擎的 FPaths::EngineContentDir()。更多虚拟路径可以查阅源码[3]
K:UnrealEngineEngineSourceRuntimeCoreUObjectPrivateMiscPackageName.cpp
类 FLongPackagePathsSingleton 的定义。
内容浏览器视图选项勾选显示引擎内容操作如下所示:
#include "BlankProgram.h"#include "RequiredProgramMainCPPInclude.h"
#include "Misc/Paths.h"DEFINE_LOG_CATEGORY_STATIC(LogBlankProgram, Log, All);
IMPLEMENT_APPLICATION(BlankProgram, "BlankProgram");INT32_MAIN_INT32_ARGC_TCHAR_ARGV()
{
GEngineLoop.PreInit(ArgC, ArgV);
UE_LOG(LogBlankProgram, Display, TEXT("Hello World"));// 常用路径获取接口
UE_LOG(LogBlankProgram, Display, TEXT("EngineDir: %s"), *FPaths::EngineDir());
UE_LOG(LogBlankProgram, Display, TEXT("EngineSavedDir: %s"), *FPaths::EngineSavedDir());
UE_LOG(LogBlankProgram, Display, TEXT("EngineIntermediateDir: %s"), *FPaths::EngineIntermediateDir());
UE_LOG(LogBlankProgram, Display, TEXT("ProjectDir: %s"), *FPaths::ProjectDir());
UE_LOG(LogBlankProgram, Display, TEXT("ProjectContentDir: %s"), *FPaths::ProjectContentDir());
UE_LOG(LogBlankProgram, Display, TEXT("ProjectConfigDir: %s"), *FPaths::ProjectConfigDir());
UE_LOG(LogBlankProgram, Display, TEXT("ProjectSavedDir: %s"), *FPaths::ProjectSavedDir());
UE_LOG(LogBlankProgram, Display, TEXT("ProjectIntermediateDir: %s"), *FPaths::ProjectIntermediateDir());FString TestFilename(TEXT("ParentDirectory/Directory/FileName.extion"));
FString Extension = FPaths::GetExtension(TestFilename);
FString BaseFilename = FPaths::GetBaseFilename(TestFilename);
FString CleanFilename = FPaths::GetCleanFilename(TestFilename);
FString Directory = FPaths::GetPath(TestFilename);
bool bFileExists = FPaths::FileExists(TestFilename);
bool bDirectoryExists = FPaths::DirectoryExists(Directory);UE_LOG(LogBlankProgram, Display, TEXT("TestFilename: %s"), *TestFilename);
// 获取文件扩展名
UE_LOG(LogBlankProgram, Display, TEXT("Extension: %s"), *Extension);
// 获取文件名,不带扩展名
UE_LOG(LogBlankProgram, Display, TEXT("BaseFilename: %s"), *BaseFilename);
// 获取文件名,带扩展名
UE_LOG(LogBlankProgram, Display, TEXT("CleanFilename: %s"), *CleanFilename);
// 获取路径文件夹,去除CleanFilename后的路径
UE_LOG(LogBlankProgram, Display, TEXT("Directory: %s"), *Directory);
// 检测文件是否存在
UE_LOG(LogBlankProgram, Display, TEXT("FileExists: %s"), bFileExists ? TEXT("True") : TEXT("False"));
// 检测文件夹是否存在
UE_LOG(LogBlankProgram, Display, TEXT("DirectoryExists: %s"), bDirectoryExists ? TEXT("True") : TEXT("False"));// 路径拼接
FString NewFilePath = FPaths::Combine(Directory, TEXT("NewFilePath.txt"));
// 简便写法
FString NewFilePathEasy = Directory / TEXT("NewFilePath.txt");
// 相对路径转换为绝对路径
FString FullPath = FPaths::ConvertRelativePathToFull(FPaths::EngineDir());
UE_LOG(LogBlankProgram, Display, TEXT("NewFilePath: %s"), *NewFilePath);
UE_LOG(LogBlankProgram, Display, TEXT("NewFilePathEasy: %s"), *NewFilePathEasy);
UE_LOG(LogBlankProgram, Display, TEXT("FullPath: %s"), *FullPath);return 0;
}
输出
LogBlankProgram: Display: Hello World
LogBlankProgram: Display: EngineDir: ../../../Engine/
LogBlankProgram: Display: EngineSavedDir: ../../../Engine/Saved/
LogBlankProgram: Display: EngineIntermediateDir: ../../../Engine/Intermediate/
LogBlankProgram: Display: ProjectDir: ../../../Engine/Programs/BlankProgram/
LogBlankProgram: Display: ProjectContentDir: ../../../Engine/Programs/BlankProgram/Content/
LogBlankProgram: Display: ProjectConfigDir: ../../../Engine/Programs/BlankProgram/Config/
LogBlankProgram: Display: ProjectSavedDir: ../../../Engine/Programs/BlankProgram/Saved/
LogBlankProgram: Display: ProjectIntermediateDir: ../../../Engine/Programs/BlankProgram/Intermediate/
LogBlankProgram: Display: TestFilename: ParentDirectory/Directory/FileName.extion
LogBlankProgram: Display: Extension: extion
LogBlankProgram: Display: BaseFilename: FileName
LogBlankProgram: Display: CleanFilename: FileName.extion
LogBlankProgram: Display: Directory: ParentDirectory/Directory
LogBlankProgram: Display: FileExists: False
LogBlankProgram: Display: DirectoryExists: False
LogBlankProgram: Display: NewFilePath: ParentDirectory/Directory/NewFilePath.txt
LogBlankProgram: Display: NewFilePathEasy: ParentDirectory/Directory/NewFilePath.txt
LogBlankProgram: Display: FullPath: K:/UnrealEngine/Engine/