UE4C++(17):FFileHelper外部⽂件读写
11/22/2020
⽂章⽬录
FFileHelper
FFileHelper 是⼀个API,提供对外部⽂件操作的接⼝,在UE4的\Engine\Source\Runtime\Core\Public\Misc⽬录中。⾸先明确这是⼀个静态库。
函数接⼝
/**
* Load a text file to an FString. Supports all combination of ANSI/Unicode files and platforms.
*
* @param Result String representation of the loaded file
* @param Filename Name of the file to load
* @param VerifyFlags Flags controlling the hash verification behavior ( see EHashOptions )
*/
static bool LoadFileToString( FString& Result,const TCHAR* Filename, EHashOptions VerifyFlags = EHashOptions::None, uint32 ReadFlags =0);
/**
* Write the FString to a file.
* Supports all combination of ANSI/Unicode files and platforms.
*/
static bool SaveStringToFile(const FString& String,const TCHAR* Filename, EEncodingOptions EncodingOptions = EEncodingOptions::AutoDetect, IFile Manager* FileManager =&IFileManager::Get(), uint32 WriteFlags =0);
/**
* Saves a 24/32Bit BMP file to disk
*
* @param Pattern filename with path, must not be 0, if with "bmp" extension (e.g. "out.bmp") the filename stays like this, if without (e.g. "out") automatic in dex numbers are addended (e.g. "out00002.bmp")
* @param DataWidth - Width of the bitmap supplied in Data >0
* @param DataHeight - Height of the bitmap supplied in Data >0
* @param Data must not be 0
* @param SubRectangle optional, specifies a sub-rectangle of the source image to save out. If NULL, the whole bitmap is saved
* @param FileManager must not be 0
* @param OutFilename optional, if specified filename will be output
* @param bInWriteAlpha optional, specifies whether to write out the alpha channel. Will force BMP V4 format.
*
* @return true if success
*/
static bool CreateBitmap(const TCHAR* Pattern, int32 DataWidth, int32 DataHeight,const struct FColor* Data,struct FIntRect* SubRectangle =NULL, IFil eManager* FileManager =&IFileManager::Get(), FString* OutFilename =NULL,bool bInWriteAlpha =false);
LoadFileToString:加载外部⽂件到FString
SaveStringToFile:FString到外部⽂件
CreateBitmap保存位图,即图⽚格式
创建图⽚位图(.bmp)
TArray<FColor> colorData;
colorData.Init(FColor(255,0,0,255),1920*1080);
TChar* outFileName =TEXT("D:/Bitmap.bmp");
FFileHelper::CreateBitmap(d,1920,1080,&colorData[0],nullptr);
创建图⽚(.png)
TArray<FColor> colorData;
colorData.Init(FColor(255,0,0,255),1920*1080);
TCHAR* fileName =TEXT("D:/TestBitmap02.png");
//图⽚打包,即压缩图⽚
IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper")); TSharedPtr<IImageWrapper> ImageWrapper;
TArray<uint8> PNGData;
FImageUtils::CompressImageArray(1920,1080, colorData, PNGData);
bool success = FFileHelper::SaveArrayToFile(PNGData, fileName);
库⽂件
#include<Runtime\Core\Public\Misc\FileHelper.h>
#include"ImageUtils.h"
#include"IImageWrapper.h"
#include"IImageWrapperModule.h"
FPaths (路径查)
#include<Runtime/Core/Public/Misc/Paths.h>
GEngine->AddOnScreenDebugMessage(-1,10.0,FColor::Red,FPaths::GetProjectFilePath());
GEngine->AddOnScreenDebugMessage(-1,10.0, FColor::Red, FPaths::EngineConfigDir());
GEngine->AddOnScreenDebugMessage(-1,10.0, FColor::Red, FPaths::GameSavedDir());
FPaths库命名⽅式⼤致分为⼆中种
Engine⽂件夹查询
Game⽂件夹查询
重载运算符
//UnrealString.h
/**
* Concatenate this path with given path ensuring the / character is used between them
*
* @param Lhs Path to concatenate onto.
* @param Rhs Path to concatenate.
* @return new FString of the path
*/
FORCEINLINE friend FString operator/(FString&& Lhs,const TCHAR* Rhs)
{
checkSlow(Rhs);
int32 StrLength = FCString::Strlen(Rhs);
FString Result(MoveTemp(Lhs), StrLength +1);
Result.PathAppend(Rhs, StrLength);
return Result;
getsavefilename}
//Example
FString newDire = FPaths::GameSavedDir()/TEXT("Screenshot");
operator/ 运算符可以快速⽅便的进⼊下⼀级⽬录
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论