TSoftObjectPtr<> (Texture2D) loads wrong size values.

I have a weapon class that has a reference to a UI icon in it, and it’s a TSoftObjectPtr<>.

When I need to use this icon, I do Icon.LoadSynchronous() and use it to set a UI image, changing the size of the image to match the Texture size.

The issue is, the first time I load the project and try setting the image, the result is 32x32, although the correct asset is being loaded. All the subsequent times it sets to a correct size, but after relaunching the projects, the first time is always 32x32.

I’ve tried this with TSoftObjectRef<UObject> and TSoftObjectRef<UTexture2D>, the result is the same. If I use a regular pointer, everything works fine.

How do I make the object from the soft pointer load correctly for the first time?

Thank you!

2 Likes

I have exactly same problem at unreal 5.1 version

did you find any workaround?

It seems this bug only happens at editor build.

at packaged build, It’s ok
but it is still problem.

Yes, it seems that it’s editor only problem.

I was having the same problem in 5.2.1 but I think I found a fix for it.

In the editor, when calling MyImage->SetBrushFromTexture(newTexture, true); for the first time, the image would always be 32 x 32. If I stopped the game and played it again, the image was the correct size. Image sizes would always be correct in packaged builds.

After tracing through the code, I found that GetSizeX() in Image.cpp was always returning a default checkerboard texture with a size of 32 x 32 in the editor, and that’s where I found this comment:

While compiling the platform data in editor, we will return the placeholders value to ensure rendering works as expected and that there are no thread-unsafe access to the platform data being built. Any process requiring a fully up-to-date platform data is expected to call FTextureCompiler:Get().FinishCompilation on UTexture first.

So, then I tried this line of code before using SetBrushFromTexture and now the size of the image is correct every time:
FTextureCompilingManager::Get().FinishCompilation({newTexture});

The include for the function is:
#include "TextureCompiler.h"

2 Likes

FTextureCompilingManager::Get().FinishCompilation({newTexture});

I’d never have guessed that in a million years, thank you! Now my synchronous textures have the right size immediately.

2 Likes

One thing I should mention is that this will not compile when you make a build. Be sure to surround it with editor preprocessor directives like this:

#if WITH_EDITOR
	FTextureCompilingManager::Get().FinishCompilation({newTexture});
#endif
1 Like

I have exactly same problem. And my project is all blueprint. I wish that there was a solution for this.