[UE4]加载外部图片,及获取文件夹下所有文件名

1 篇文章 0 订阅

.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;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值