I use a Widget Switcher as my HUD root widget.
It switches between several Canvas Panels, which contain the actual widgets I want to display.
In one of these canvas panels, I need to display several RenderTargetTextures as well as one Texture2D.
The RenderTargetTextures work fine, they are displayed. However, when I add my Texture2D (whose color I set via code, it is not read from memory), that UImage widget is not displayed.
I have tried to use another RenderTargetTexture instead, which displays the UImage widget, however then I cannot set the pixel colors via code (it is just black).
I have tried both Texture2D and Texture2DDynamic, but both have the same problem, there is no UImage displayed. The texture is however correctly set (for debugging purposes, I chose to set a white color for every pixel and I checked in RenderDoc that the texture is indeed being used, which it is).
Here is the current code I use for adding everything to my Canvas Panel:
UImage* img = NewObject<UImage>(UImage::StaticClass());
img->SetVisibility(ESlateVisibility::Visible);
canvasPanelWidget->AddChild(img);
TArray<UPanelSlot*> slots = canvasPanelWidget->GetSlots();
//the image is at the now last slot
UCanvasPanelSlot* imageSlot = Cast<UCanvasPanelSlot>(slots[slots.Num() - 1]);
imageSlot->SetPosition(FVector2D(0, 0));
imageSlot->SetSize(FVector2D(width, height));
UTexture2D* headerTexture = UTexture2D::CreateTransient(texSettings.width, texSettings.height, PF_R8G8B8A8_UINT, FName("CPU computed Texture"));
headerTexture->MipGenSettings = TextureMipGenSettings::TMGS_NoMipmaps;
headerTexture->UpdateResource();
img->SetBrushFromTexture(headerTexture, true);
img->Brush.DrawAs = ESlateBrushDrawType::Image;
img->Brush.ImageType = ESlateBrushImageType::FullColor;
What do I have to do, to correctly display an UTexture2D object in an UImage widget?
UPDATE
I have now also tried to use a Material for the brush, which uses a texture sampler and just connect’s that texture sampler’s output to the base color input and nothing else. This works neither with my own texture I created on the fly, nor with a hard coded texture in the material.