hi,
is it possible to display an image on a button widget using slate (without using any blueprints) ?
if yes, how can i do that?
i am using UE 4.25.3 with vs 2019
hi,
is it possible to display an image on a button widget using slate (without using any blueprints) ?
if yes, how can i do that?
i am using UE 4.25.3 with vs 2019
Yes, here is an example.
// SThisWidget.h
public:
SLATE_BEGIN_ARGS(SThisWidget) {}
SLATE_ARGUMENT(TWeakObjectPtr<UTexture2D>, ButtonImage)
SLATE_END_ARGS()
TWeakObjectPtr<UTexture2D> ButtonImage;
private:
const FSlateDynamicImageBrush* ButtonBrush;
// SThisWidget.cpp
void SThisWidget::Construct(const FArguments& InArgs)
{
ButtonImage = InArgs._ButtonImage;
ButtonBrush = new FSlateDynamicImageBrush(ButtonImage.Get(), FVector2D(1024, 1024), FName("ButtonImageBrush"));
......
//Button itself in ChildSlot[]
SNew(SButton)
.ButtonColorAndOpacity(Transparency)
.OnClicked(this, &SThisWidget::OnButtonClicked)
.OnHovered(this, &SThisWidget::OnButtonHovered)
.OnUnhovered(this, &SThisWidget::OnButtonUnhovered)
[
SNew(ButtonImg, SImage)
.Image(ButtonBrush)
]
Then you just need to pass the UTexture2D in as an argument when creating the widget.
ThisWidget = SNew(SThisWidget)
.ButtonImage(SelectedButtonImage);
I do this in a custom AHUD which I make a blueprint and then set the desired texture being passed in there. You should be able to load this in with code for your situation in the same AHUD class, perhaps with a constructor helper? I don’t know how to select assets purely in code and then have them package correctly.
I hope this helps though!
Thank you, but i’ve already made a different solution by displaying an image widget and checking its hitbox