I am trying to make a version of Image Widget that automatically converts the brush into a dynamic material for further customization.
In my implementation I do this by extending the Image class and do the conversion on SynchronizeProperties.
Below is a minimized version of my code
UCLASS()
class GAME_API UMaterialImage : public UImage
{
GENERATED_BODY()
public:
virtual void SynchronizeProperties() override;
UPROPERTY(BlueprintReadOnly, Transient, Category=Appearance)
UMaterialInstanceDynamic* DynamicMaterial;
UPROPERTY(EditAnywhere, BlueprintReadOnly)
UObject* OriginalResource;
};
void UMaterialImage::SynchronizeProperties()
{
UObject* Resource = Brush.GetResourceObject();
if (!Resource && OriginalResource)
Resource = OriginalResource;
if (Resource)
{
DynamicMaterial = Cast<UMaterialInstanceDynamic>(Resource);
if(!DynamicMaterial)
{
auto MaterialInterface = Cast<UMaterialInterface>(Resource);
if (MaterialInterface)
{
OriginalResource = Resource;
DynamicMaterial = GetDynamicMaterial();
}
}
}
}
The idea is not to store the dynamic material, but instead store the resource that was applied to the brush before conversion and do the conversion again on load.
I though having OriginalResource be a UPROPERTY would make it store the data, but it just resets it back to null whenever I compile the Widget Blueprint and the data is missing when saving as well.
Manually setting the property using the Details panel works, but that kinda defeats the point. The intention is for the OriginalResource property to be assigned trough the conversion and hidden from the Details panel, it is just made visible for debugging.
Is it possible to set UPROPERTIES from code and have it store that value, or can it only work by setting it using the Details Panel?
Or is there a different approach that would give the result that I should do instead?