zoukankan      html  css  js  c++  java
  • UE4 将本地图片转成UTexture2D 在runtime显示

    UFUNCTION(BlueprintCallable, Category = "TextureFromDisk")
            static class UTexture2D* GetTexture2DFromDiskFile(const FString& FilePath);
    class UTexture2D* UTextureFromDiskFunctionLibrary::GetTexture2DFromDiskFile(const FString& FilePath)
    {
        TArray<uint8> RawFileData;
        UTexture2D* MyTexture = NULL;
        if (FFileHelper::LoadFileToArray(RawFileData, *FilePath /*"<path to file>"*/))
        {
            IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
            // Note: PNG format.  Other formats are supported
            IImageWrapperPtr ImageWrapper = ImageWrapperModule.CreateImageWrapper(EImageFormat::PNG);
            if (ImageWrapper.IsValid() && ImageWrapper->SetCompressed(RawFileData.GetData(), RawFileData.Num()))
            {
                const TArray<uint8>* UncompressedBGRA = NULL;
                if (ImageWrapper->GetRaw(ERGBFormat::BGRA, 8, UncompressedBGRA))
                {
                    // Create the UTexture for rendering
                    MyTexture = UTexture2D::CreateTransient(ImageWrapper->GetWidth(), ImageWrapper->GetHeight(), PF_B8G8R8A8);
    
                    // Fill in the source data from the file
                    void* TextureData = MyTexture->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
                    FMemory::Memcpy(TextureData, UncompressedBGRA->GetData(), UncompressedBGRA->Num());
                    MyTexture->PlatformData->Mips[0].BulkData.Unlock();
    
                    // Update the rendering resource from data.
                    MyTexture->UpdateResource();
                }
            }
        }
        return MyTexture;
    }

    别忘记头文件的引用

    #include "Developer/ImageWrapper/Public/Interfaces/IImageWrapper.h"
    #include "Developer/ImageWrapper/Public/Interfaces/IImageWrapperModule.h"
  • 相关阅读:
    数据表分区, 全新分区
    SSIS基础设计最佳实践
    数据表分区, 普通表转分区表
    关于游标[二]
    弹窗代码汇集
    关于分页显示中的换行显示原理代码
    C# 装箱和拆箱[整理]
    insert into tablename select * from tablename与Select * into tablename from tablename 比较[转]
    SQL Server应用程序中的高级SQL注入
    CHARINDEX使用【转】
  • 原文地址:https://www.cnblogs.com/cindyOne/p/4338646.html
Copyright © 2011-2022 走看看