How can I set a material I created in my content folder to display as an Image in a slate widget?

I have a functional slate widget.

ChildSlot
	[
		SNew(SOverlay)
		+ SOverlay::Slot()
		.HAlign(HAlign_Fill)
		.VAlign(VAlign_Fill)
		[
			SNew(SImage)
			.ColorAndOpacity(FColor::Yellow)
		]
       ]

I want this SImage to display an image I have placed into my content folder and converted into a material. it appears I need to 1: create an FSlateBrush 2: set the “resource object” to the desired material using the material’s filepath somehow 3: set the SImage image attribute to this freshly created FSlateBrush. If someone could walk me through these steps that would be great but any and all suggestions are welcome.

DUDE… Its done. Strap in this solution is more complex than expected. First I will explain the solution then show the exact code.
1: to access assets in the content browser the only solution I have found is ConstructorHelpers::FObjectFinder this works in my case and can likely be used for other object types. However the slate ‘Construct’ function is not compatible with ConstructorHelpers::FObjectFinder. to use ConstructorHelpers::FObjectFinder with a slate widget I instead call ConstructorHelpers::FObjectFinder in a constructor in my hud cpp file. Then when the hud initializes the slate widget the results of the FObjectFinder can be passed into the slate widget as an argurment.
2: I then declare and initialize a new FSlateBrush in the slate widget ‘Construct’ function, and set its resource object to this argument.
3: setting the slate brush to display as the content of a SImage is then routine.

hud.h:

UCLASS()
class MARBLEGAMEBLUEPRINT_API ATestHud : public AHUD
{
	GENERATED_BODY()

public:
	ATestHud();

	UMaterial* grass_VMUI_1;
//...
}

hud.cpp

ATestHud::ATestHud()
{
	static ConstructorHelpers::FObjectFinder<UMaterial> tempVarGrass_VMUI_1(TEXT("'/Game/Movies/videoMaterialsForUI/grass_VMUI_1.grass_VMUI_1'"));

	if (tempVarGrass_VMUI_1.Object != NULL)
	{
		grass_VMUI_1 = (UMaterial*)tempVarGrass_VMUI_1.Object;
	}

}

void ATestHud::BeginPlay()
{
	Super::BeginPlay();

	if (GEngine && GEngine->GameViewport)
	{
		slateWidget = SNew(STestWidgetThree)
			.OwningHUD(this)
			.grass_VMUI_1(grass_VMUI_1);

		GEngine->GameViewport->AddViewportWidgetContent(SAssignNew(slateWidgetContainer, SWeakWidget).PossiblyNullContent(slateWidget.ToSharedRef()));
	}
}

slateWidget.h

class STestWidgetThree : public SCompoundWidget
{
public:

	SLATE_BEGIN_ARGS(STestWidgetThree) {}

	SLATE_ARGUMENT(TWeakObjectPtr<class ATestHud>, OwningHUD)

	SLATE_ARGUMENT(UMaterial*, grass_VMUI_1)

	SLATE_END_ARGS()

	//every widget needs a construction function
	void Construct(const FArguments& InArgs);

        UMaterial* grass_VMUI_1;

slateWidget.cpp

    grass_VMUI_1 = InArgs._grass_VMUI_1;

	FSlateBrush* grass_1_SB = new FSlateBrush();

	grass_1_SB->SetResourceObject(grass_VMUI_1);

ChildSlot
	[
		SNew(SOverlay)
		+ SOverlay::Slot()
		.HAlign(HAlign_Fill)
		.VAlign(VAlign_Fill)
		[
			SNew(SImage)
			.Image(grass_1_SB)
		]
	]

if you have any questions or any trouble with headers Id be happy to help but you shouldnt

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.