Greetings! I am attempting to create a asynchronous load system for hundreds of textures that are defined by TAssetPtrs in datatables. Something like:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Textures")
TAssetPtr<UTexture> buttonImage;
In order to load this texture, I have created a function in the game instance:
.h
UPROPERTY(EditAnywhere, BlueprintReadWrite)
FStreamableManager AssetLoader;
UFUNCTION(BlueprintCallable, Category="GUI / Core Interaction")
void LoadTexture2dForAsset(const TAssetPtr<UTexture> &image);
.c
void UGameInstance::LoadTexture2dForAsset(const TAssetPtr<UTexture> &image)
{
if (image.IsValid())
{
FStringAssetReference AssetToLoad;
AssetToLoad = image.ToStringReference();
AssetLoader.SimpleAsyncLoad(AssetToLoad);
}
else
{
UE_LOG(LogTemp, Error, TEXT("Error Loading Button Texture"));
}
}
So, the only weird thing is that the TAssetPtr is defined as a const – the code doesn’t seem to work when it is not.
Now, in blueprints I am using a function that formats buttons by changing the brush widget. It looks something like:
So basically before I set the image for the brush widget, I call LoadTexture2dForAsset(). In theory, this is supposed to look at the TAssetPtr, see if it is valid and then load the texture if it is not loaded.
It sorta works, but only if I load / edit the texture in the editor before.
Run game in editor. No button:
Now open up the texture in the editor:
Now run the game again:
So basically, this works, but only if I open the texture in the editor; it seems the texture is not being loaded properly by SimpleAsyncLoad().
It seems someone else had this problem. Their solution was to change the TAssetPtr to a texture pointer. This would, though, seem to negate the memory advantage of using Asynchronous loading.
Any help would be appreciated! Really just want to be able to load textures as needed for the GUI.