Hey guys.
I’m trying to create a new texture asset that will be saved to the game packages/content browser. But it’s crashing when I do the usual checks before writing to it.
UTexture2D * NewAsset = NewObject<UTexture2D>(Package, AssetName, RF_Public | RF_Standalone); // Save as local asset
if (&NewAsset
&& !NewAsset->PlatformData->Mips[0].BulkData.IsLocked()) {
The last line triggers this error:
First-chance exception at 0x000007FED1E3C82E (UE4Editor-OctaneRenderPlugin-Win64-DebugGame.dll) in UE4Editor.exe: 0xC0000005: Access violation reading location 0x0000000000000018.
Yeah, I don’t know what I’m doing, but it does seem like the texture object needs some more initialization. Any ideas?
So, new problem. I started using my existing Texture object as a template for NewAsset and got rid of the texture write entirely. It creates and saves an empty asset (0x0 pixels, black texture, crashes if I open it in the editor, file path exists but no actual uasset is saved), and I get this in the log:
LogTexture:Error: Texture2D /Game/Octane/BakedAssets/MyTexture.MyTexture is unknown which is not supported.
Any idea what that might be about? The compression seems to be set (I set it earlier on Texture), so that google answer wasn’t much help. Oddly enough, SavePackage() returns true. Here’s some code:
bool UMyThingOutput::SaveToPackage(MyThing::CustomImage Image, FName AssetName){
FString PackageName = TEXT("/Game/MyThing/BakedAssets/" + AssetName.ToString());
UPackage * Package = CreatePackage(NULL, *PackageName);
UTexture2D * NewAsset = NewObject<UTexture2D>(Package, AssetName, RF_Public | RF_Standalone, Texture, true);
FAssetRegistryModule::AssetCreated(NewAsset);
NewAsset->MarkPackageDirty();
NewAsset->PostEditChange();
Package->SetDirtyFlag(true);
FString PackageFileName = FPackageName::LongPackageNameToFilename(PackageName, FPackageName::GetAssetPackageExtension());
if (UPackage::SavePackage(Package, NewAsset, RF_Public | RF_Standalone, *PackageName, GError, nullptr, true, true, SAVE_NoError)) {
UE_LOG(LogTemp, Warning, TEXT("[MyThing] Saving %s to package successful"), *FString(AssetName.ToString()));
}
else {
UE_LOG(LogTemp, Warning, TEXT("[MyThing] Saving %s to package failed"), *FString(AssetName.ToString()));
}
return true;
}
And sorted. This is what I had to do:
NewAsset->PlatformData = Texture->PlatformData;
NewAsset->UpdateResource();
…then back to the normal asset saving routine.
1 Like