I try to create a UProgressBar by cpp. I have already create my userwidget, and i successfully create a uprogressbar. Then i want to do more about it, suck as set background image and the fill image, i know that is very simple to do with blueprint, but i also want to try it by cpp.
But i find there is always a crash when i set a UTexture to the UProgressBar.
UCharacterUserWidget::UCharacterUserWidget(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
static ConstructorHelpers::FObjectFinder<UTexture> HT(TEXT("/Game/HUDAssets/Health_NoFill"));
HealthText = HT.Object;
}
void UCharacterUserWidget::NativeConstruct()
{
Super::NativeConstruct();
}
TSharedRef<SWidget> UCharacterUserWidget::RebuildWidget()
{
UCanvasPanel* RootWidget = WidgetTree->ConstructWidget<UCanvasPanel>(UCanvasPanel::StaticClass(), TEXT("RootWidget"));
UCanvasPanelSlot* RootWidgetSlot = Cast<UCanvasPanelSlot>(RootWidget->Slot);
if (RootWidgetSlot)
{
RootWidgetSlot->SetAnchors(FAnchors(0.0f, 0.0f, 1.0f, 1.0f));
RootWidgetSlot->SetOffsets(FMargin(0.0f, 0.0f));
}
WidgetTree->RootWidget = RootWidget;
TSharedRef<SWidget> Widget = Super::RebuildWidget();
//
HealthProgressBar = WidgetTree->ConstructWidget<UProgressBar>(UProgressBar::StaticClass(), TEXT("HealthProgressBar"));
HealthProgressBar->RenderTransform.Translation.X = 160.0f;
HealthProgressBar->RenderTransform.Translation.Y = 70.0f;
HealthProgressBar->BackgroundImage_DEPRECATED->Brush.SetResourceObject(HealthText);
//HealthProgressBar->RenderTransformPivot.X = 0.0f;
//HealthProgressBar->RenderTransformPivot.Y = 0.0f;
//static ConstructorHelpers::FObjectFinder<UTexture> HealthFillBrush(TEXT("/Game/HUDAssets/Health_NoFill"));
//static ConstructorHelpers::FObjectFinder<UTexture2D> HelathFillBursh2D(TEXT("/Game/HUDAssets/Health_NoFill"));
//HealthProgressBar->FillImage_DEPRECATED = HealthFillBrush.Object;
//
ManaProgressBar = WidgetTree->ConstructWidget<UProgressBar>(UProgressBar::StaticClass(), TEXT("ManaProgressBar"));
ManaProgressBar->RenderTransform.Translation.X = 160.0f;
ManaProgressBar->RenderTransform.Translation.Y = 110.0f;
ManaProgressBar->RenderTransform.Scale.X = 340.0f;
ManaProgressBar->RenderTransform.Scale.Y = 40.0f;
ManaProgressBar->RenderTransformPivot.X = 0.0f;
ManaProgressBar->RenderTransformPivot.Y = 0.0f;
RootWidget->AddChild(HealthProgressBar);
//RootWidget->AddChild(ManaProgressBar);
return Widget;
}
I use many ways to load the image asset, such as ConstructorHelpers or LoadObject, but when i call HealthProgressBar->BackgroundImage_DEPRECATED->Brush.SetResourceObject(). It always crash. I don’t know why. I have already decided to use the blueprint extend from cpp…but i really want to know the reason or if there is any method can do more about UProgressbar by cpp. Need your help.Thanks!