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