Unreal Engine 4 遍历文件夹读文件

1.遍历文件夹,加载所有uasset

TArray LoadedObjects;


 FString ResourcePath("/Game/GameResource/UI");

 if (EngineUtils::FindOrLoadAssetsByPath(ResourcePath, LoadedObjects, EngineUtils::ATL_Regular))
 {
  for (auto ObjectIter = LoadedObjects.CreateConstIterator(); ObjectIter; ++ObjectIter)
  {
   UUI_BorderAsset* board_asset = Cast(*ObjectIter);
  }
 }

2.遍历文件夹,得到所有文件名,可以是其他的文件比如TXT等

class ItemTableVisitor : public IPlatformFile::FDirectoryVisitor
{

 virtual bool Visit(const TCHAR* FilenameOrDirectory, bool bIsDirectory) override
 {
  if ( !bIsDirectory )
  {
   FString filepath = FString(FilenameOrDirectory);
  }
  return true;
 }

};


void ReadAll()
{
 ItemTableVisitor Visitor;
 FPlatformFileManager::Get().GetPlatformFile().IterateDirectoryRecursively( *(FPaths::GameContentDir() + L"GameData/"), Visitor);
}


读文件:

1.自己二进制方式读取

IPlatformFile& platFormFile = FPlatformFileManager::Get().GetPlatformFile();
IFileHandle *fileHandle = platFormFile.OpenRead(*(FPaths::GameContentDir() + Path));
if (!fileHandle)
{
return nullptr;
}


uint8* readbuffer = new uint8[fileHandle->Size() + 1];
fileHandle->Read(readbuffer, fileHandle->Size());
readbuffer[fileHandle->Size()] = 0;
delete fileHandle;
FString filestring(ANSI_TO_TCHAR((char*)readbuffer));
delete[] readbuffer;


读取一个文件的另一种方式:

FArchive* file = IFileManager::Get().CreateFileWriter(*filepathtosave);


2.读取一个字符串:

FFileHelper::LoadFileToString(filestring, *(FPaths::GameContentDir() + Path));

Engine\Source\Runtime\Core\Public\Misc\CoreMisc.h

FFileHelper也有吧字符串保存到文件的函数。








你可能感兴趣的:(虚幻4)