M411
(M411)
October 30, 2022, 6:08pm
1
Hello, I’m trying to load a image from a file in the disk and convert it to a UTexture2D but unreal keeps crashing.
I already tried to use LoadObject, but unreal keeps crashing.
Does anyone know a solution?
FString FullPath = FPlatformProcess::UserDir() + "Game/logos/countries/ad.png"
if(LoadObject<UTexture2D>(NULL, *FullPath)){
}
3dRaven
(3dRaven)
October 30, 2022, 7:27pm
2
I wrote this for my own library not long ago. This should work for you.
UTexture2D* UImageFunctionLibrary::FileToTexture2d(FString File) {
FString contentPath = FPaths::ProjectContentDir();
FString fullContentPath = FPaths::ConvertRelativePathToFull(contentPath);
FString FullPath = fullContentPath + File;
UTexture2D* texture = nullptr;
if (FullPath.Len() > 0) {
FullPath.ReplaceInline(TEXT("\\"), TEXT("/"), ESearchCase::CaseSensitive);
FullPath.ReplaceInline(TEXT("//"), TEXT("/"), ESearchCase::CaseSensitive);
FullPath.RemoveFromStart(TEXT("/"));
FullPath.RemoveFromEnd(TEXT("/"));
FPlatformMisc::NormalizePath(FullPath);
texture = FImageUtils::ImportFileAsTexture2D(FullPath);
}
return texture;
}
M411
(M411)
November 1, 2022, 1:42pm
3
This worked.
Many thanks mate <3
yeah LoadObject wont work for disk file its for cooked uassets only thats why it crashes. you need ImageWrapper route.
quick c++ way:
FString Path = FPaths::ProjectSavedDir() + TEXT(“logos/ad.png”);
UTexture2D* Tex = FImageUtils::ImportFileAsTexture2D(Path);
if(Tex) MyMesh->SetTextureParam…
for bp just use Import File as Texture 2D node without file:// prefix, just normal path. also check file exists before you read it, if its still copying you get empty bytes and crash.
dont do it on game thread for big pngs — decode async or you hitch
btw runtime directory watcher from fab Runtime Directory Watcher — Hot-Folder File Events & Async Texture Loader | Fab worked for me