Unreal Engine 4 遍历文件夹读文件

2020-03-29 365官方体育 阅读

  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也有吧字符串保管到文件的函数。

  

标签: