1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| UTexture2D* ULibNextBridge::CreateTexture2dAsset_ByPixels(FString TextureName, FString Path, const TArray<FColor>& FillPixels, int32 Width, int32 Height, EPixelFormat PixelFormat, bool sRGB) { TArray<FColor> Pixels = FillPixels; FString UniquePackageName; FString UniqueAssetName; FAssetToolsModule& AssetToolsModule = FModuleManager::Get().LoadModuleChecked<FAssetToolsModule>("AssetTools"); AssetToolsModule.Get().CreateUniqueAssetName(Path + TextureName, TEXT(""), UniquePackageName, UniqueAssetName); UPackage* Package = CreatePackage( *UniquePackageName); Package->FullyLoad(); UTexture2D* res = NewObject<UTexture2D>(Package, *UniqueAssetName, RF_Public | RF_Standalone | RF_MarkAsRootSet); res->AddToRoot(); res->SRGB = sRGB; res->MipGenSettings = TMGS_NoMipmaps; FTexturePlatformData* PlatformData = new FTexturePlatformData(); PlatformData->SizeX = Width; PlatformData->SizeY = Height; PlatformData->PixelFormat = PixelFormat; res->SetPlatformData(PlatformData); res->UpdateResource(); FTexture2DMipMap* Mip = new FTexture2DMipMap(); res->GetPlatformData()->Mips.Add(Mip); Mip->SizeX = Width; Mip->SizeY = Height; Mip->BulkData.Lock(LOCK_READ_WRITE); uint8* TextureData = static_cast<uint8*>(Mip->BulkData.Realloc(Width * Height * sizeof(FColor))); FMemory::Memcpy(TextureData, Pixels.GetData(), Width * Height * sizeof(FColor)); Mip->BulkData.Unlock();
res->UpdateResource(); res->Source.Init(Width,Height,1, 1, ETextureSourceFormat::TSF_BGRA8, reinterpret_cast<uint8*>(Pixels.GetData())); res->UpdateResource(); Package->MarkPackageDirty(); FAssetRegistryModule::AssetCreated(res); FString PackageFileName = FPackageName::LongPackageNameToFilename(UniquePackageName, FPackageName::GetAssetPackageExtension()); FSavePackageArgs SavePackageArgs; SavePackageArgs.TopLevelFlags = RF_Public | RF_Standalone; SavePackageArgs.SaveFlags = SAVE_NoError; bool bSaved = UPackage::SavePackage(Package,NULL,*PackageFileName,SavePackageArgs); check(bSaved) return res; }
|