UE5 中的文件路径转换
UE中提供的有关路径的转换方式和什么时候用的是什么路径简直是超级无敌模糊,因此在这里总结整理一下。
本地绝对路径
// 可以获得Content文件夹的绝对路径,但是格式是/../../这种形式的
FPath::ProjectContentDir()
//才能获得真正的文件绝对路径,比如C://aaa/Project/Content 之类的
FPaths::Combine(FPaths::ConvertRelativePathToFull(FPaths::ProjectContentDir()));
相对路径转换为本地绝对路径
FString Path = "/Game/Textures”; //Content下面的Texture文件夹
FString ContentFolder = FPaths::ConvertRelativePathToFull(FPaths::ProjectContentDir());
FString AbsolutePath = FPaths::Combine(ContentFolder, "Textures"); // D:/fishing/Content/Textures
//或者
FString AbsolutePath = Path.Replace(TEXT("/Game"),*ContentFolder);
一般本地绝对路径用于比如判断文件是否存在,文件夹是否存在,遍历文件夹下的全部文件等等.
//创建文件夹
IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
if (!PlatformFile.DirectoryExists(*AbsolutePath))
{
PlatformFile.CreateDirectoryTree(*AbsolutePath);
}
本地绝对路径转换为相对路径
FString ContentFolder = FPaths::ConvertRelativePathToFull(FPaths::ProjectContentDir());
FString Path = Path.Replace(*ContentFolder, TEXT("/Game"));
Path = FPaths::Combine(FPaths::GetPath(Path), FPaths::GetBaseFilename(Path)); //获得更加标准的相对路径
相对路径
UE中大多数资源获取的路径都是/Game/Textures这样的形式,比如
FString AssetPath = AssetData.GetSoftObjectPath().GetAssetPathString(); // /Game/Res/food.food