[UE4][C++]保存Texture2d到本地

1.官方蓝图有接口 Export Texture 2D ,但是有些Texture 2D不支持,会报警

会有这个警告:Unsupported source texture format provided

2.自定义,先获取data,然后把data保存成图片

 

bool ExprotUTexture2D(UTexture2D* Img, const FString& fileDestination)
{
    FTexture2DMipMap & mipmap = Img->PlatformData->Mips[0];
    unsigned char *Data = (unsigned char *)mipmap.BulkData.Lock(LOCK_READ_WRITE);
    int texturex = Img->PlatformData->SizeX;
    int texturey = Img->PlatformData->SizeY;
    TArray<FColor> nColors;
    float bPerUV_v = 1.0 / texturey;
    for (int32 y = 0; y < texturey; y++)
    {
        for (int32 x = 0; x < texturex; x++)
        {
            FColor bColor;
            bColor.B = Data[(y*texturex + x) * 4 + 0];//B
            bColor.G = Data[(y*texturex + x) * 4 + 1];//G
            bColor.R = Data[(y*texturex + x) * 4 + 2];//R
            bColor.A = Data[(y*texturex + x) * 4 + 3];//A 0:全透明;255:全不透明
            nColors.Add(bColor);
        }
    }
    //int32 strwide = (int32)(sizeof(uint8) * 4);
    //FMemory::Memcpy(Data, colors.GetData(), strwide*texturey*texturex);
    mipmap.BulkData.Unlock();
    Img->UpdateResource();
    
    //保存,思路,拿到Color的数据,然后利用FImageUtils转换图片数据,最后写入
    TArray<uint8> ImgData;
    FImageUtils::CompressImageArray(texturex, texturey, nColors, ImgData);
    bool re= FFileHelper::SaveArrayToFile(ImgData, *fileDestination);
    return re;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值