.h:
//
UFUNCTION(BlueprintCallable, Category = "LoadLocalTexture")
static class UTexture2D*LoadTexture2D(const FString Path, bool& IsValid, int32& OutWidth, int32&OutHeight);
//
UFUNCTION(BlueprintCallable, Category = "LoadLocalTexture")
static TArray<FString> GetFolderFiles(const FString path);
//
.cpp:
//Texture --------------------------------------------------------------------------------
static IImageWrapperPtr GetImageWrapperByExtention(const FString InImagePath)
{
IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
if (InImagePath.EndsWith(".png"))
{
return ImageWrapperModule.CreateImageWrapper(EImageFormat::PNG);
}
else if (InImagePath.EndsWith(".jpg")&& InImagePath.EndsWith(".jpeg"))
{
return ImageWrapperModule.CreateImageWrapper(EImageFormat::JPEG);
}
else if (InImagePath.EndsWith(".bmp"))
{
return ImageWrapperModule.CreateImageWrapper(EImageFormat::BMP);
}
else if (InImagePath.EndsWith(".ico"))
{
return ImageWrapperModule.CreateImageWrapper(EImageFormat::ICO);
}
else if (InImagePath.EndsWith("exr"))
{
return ImageWrapperModule.CreateImageWrapper(EImageFormat::EXR);
}
else if (InImagePath.EndsWith(".icns"))
{
return ImageWrapperModule.CreateImageWrapper(EImageFormat::ICNS);
}
return nullptr;
}
UTexture2D* UTAToolsBlueprintFunctionLibrary::LoadTexture2D(const FString Path, bool& IsValid, int32& OutWidth, int32&OutHeight)
{
UTexture2D* Texture = nullptr;
IsValid = false;
if (!FPlatformFileManager::Get().GetPlatformFile().FileExists(*Path))
{
return nullptr;
}
//
TArray<uint8>RawFileData;
if (!FFileHelper::LoadFileToArray(RawFileData, *Path))
{
return nullptr;
}
//
IImageWrapperPtr ImageWrapper = GetImageWrapperByExtention(Path);
if (ImageWrapper.IsValid() && ImageWrapper->SetCompressed(RawFileData.GetData(), RawFileData.Num()))
{
const TArray<uint8>* UncompressedRGBA = nullptr;
if (ImageWrapper->GetRaw(ERGBFormat::RGBA, 8, UncompressedRGBA))
{
Texture = UTexture2D::CreateTransient(ImageWrapper->GetWidth(), ImageWrapper->GetHeight(), PF_R8G8B8A8);
if (Texture != nullptr)
{
IsValid = true;
OutWidth = ImageWrapper->GetWidth();
OutHeight = ImageWrapper->GetHeight();
void *TextureData = Texture->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
FMemory::Memcpy(TextureData, UncompressedRGBA->GetData(), UncompressedRGBA->Num());
Texture->PlatformData->Mips[0].BulkData.Unlock();
Texture->UpdateResource();
}
}
}
return Texture;
}
TArray<FString> UTAToolsBlueprintFunctionLibrary::GetFolderFiles(FString path)
{
TArray<FString> files;
FPaths::NormalizeDirectoryName(path);
IFileManager& FileManager = IFileManager::Get();
FString FinalPath = path / TEXT("*");
FileManager.FindFiles(files, *FinalPath, true, true);
return files;
}