Hi everyone!
I have loaded a UTexture2D from image file successfully, and then I try to save it as an asset sothat I can use it in my Content Browser, but it did not work, I can not find Texture2D asset in my Content Browser, what should i do?
Here are my codes:
//save an asset
FString ImageAssetPackagePath = TEXT("/Game/ImageAsset/ModelDataAsset");
UPackage* MyImagePackage = CreatePackage(nullptr, *ImageAssetPackagePath);
bool isValid;
int32 texWidth, texHeight;
FString texturepath = TEXT("H:/ue4/CADTEST/test2/export/Brick_Non_Uniform_Running_Red.jpg");
UTexture2D* textureParam = LoadTexture2DFromFile(texturepath, isValid, texWidth, texHeight);
textureParam->Rename(TEXT("Brick_Non_Uniform_Running_Red"), MyImagePackage);
FAssetRegistryModule::AssetCreated(textureParam);
textureParam->GetOutermost()->MarkPackageDirty();
LoadTexture2DFromFile():
UTexture2D* ABuilding::LoadTexture2DFromFile(const FString& FullFilePath, bool& IsValid, int32& Width, int32& Height)
{
IsValid = false;
UTexture2D* LoadedT2D = NULL;
IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
EImageFormat InFormat;
if (FullFilePath.EndsWith(".png"))
{
InFormat = EImageFormat::PNG;
}
else if (FullFilePath.EndsWith(".jpg") || FullFilePath.EndsWith(".jpeg"))
{
InFormat = EImageFormat::JPEG;
UE_LOG(LogTemp, Warning, TEXT("Load image JPEG"));
}
else if (FullFilePath.EndsWith(".bmp"))
{
InFormat = EImageFormat::BMP;
}
else
{
UE_LOG(LogTemp, Warning, TEXT("Load image fail"));
return NULL;
}
TSharedPtr<IImageWrapper> ImageWrapper = ImageWrapperModule.CreateImageWrapper(InFormat);
//Load From File
//FString FullFilePath = UWebImageDownloader::WebImageFolder() + "/" + FileName + GetJoyImageExtension(ImageFormat);
if (!FPlatformFileManager::Get().GetPlatformFile().FileExists(*FullFilePath))
{
UE_LOG(LogTemp, Warning, TEXT("read image fail"));
return NULL;
}
TArray<uint8> RawFileData;
if (!FFileHelper::LoadFileToArray(RawFileData, *FullFilePath)) { return NULL; }
//Create T2D!
if (ImageWrapper.IsValid() && ImageWrapper->SetCompressed(RawFileData.GetData(), RawFileData.Num()))
{
TArray<uint8> UncompressedBGRA;
if (ImageWrapper->GetRaw(ERGBFormat::BGRA, 8, UncompressedBGRA))
{
LoadedT2D = UTexture2D::CreateTransient(ImageWrapper->GetWidth(), ImageWrapper->GetHeight(), PF_B8G8R8A8);
//Valid?
if (!LoadedT2D) {
UE_LOG(LogTemp, Warning, TEXT("Load T2D fail"));
return NULL;
}
//~~~~~~~~~~~~~~
//Out!
Width = ImageWrapper->GetWidth();
Height = ImageWrapper->GetHeight();
//Copy!
void* TextureData = LoadedT2D->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
FMemory::Memcpy(TextureData, UncompressedBGRA.GetData(), UncompressedBGRA.Num());
LoadedT2D->PlatformData->Mips[0].BulkData.Unlock();
//Update!
LoadedT2D->UpdateResource();
}
}
// Success!
IsValid = true;
//UE_LOG(LogTemp, Warning, TEXT("LoadedT2D success"));
return LoadedT2D;
}
It shows the asset I created is an Empty Package: