Add UFUNCTION to play Sound2D when button is hovered in UMainMenuUIWidget

This commit is contained in:
baz 2024-04-03 00:01:00 +01:00
parent 7056f530f7
commit 4a74fc1007
2 changed files with 18 additions and 2 deletions

View File

@ -13,21 +13,25 @@ void UMainMenuUIWidget::NativeConstruct()
if (NewGameButton)
{
NewGameButton->OnClicked.AddUniqueDynamic(this, &UMainMenuUIWidget::NewGameButtonOnClicked);
NewGameButton->OnHovered.AddUniqueDynamic(this, &UMainMenuUIWidget::PlayHoveredSound);
}
if (LoadGameButton)
{
LoadGameButton->OnClicked.AddUniqueDynamic(this, &UMainMenuUIWidget::LoadGameButtonOnClicked);
LoadGameButton->OnHovered.AddUniqueDynamic(this, &UMainMenuUIWidget::PlayHoveredSound);
}
if (OptionsButton)
{
OptionsButton->OnClicked.AddUniqueDynamic(this, &UMainMenuUIWidget::OptionsButtonOnClicked);
OptionsButton->OnHovered.AddUniqueDynamic(this, &UMainMenuUIWidget::PlayHoveredSound);
}
if (QuitButton)
{
QuitButton->OnClicked.AddUniqueDynamic(this, &UMainMenuUIWidget::QuitButtonOnClicked);
QuitButton->OnHovered.AddUniqueDynamic(this, &UMainMenuUIWidget::PlayHoveredSound);
}
if (APlayerController* PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), 0))
@ -35,8 +39,6 @@ void UMainMenuUIWidget::NativeConstruct()
UWidgetBlueprintLibrary::SetInputMode_UIOnlyEx(PlayerController, this, EMouseLockMode::LockAlways);
PlayerController->bShowMouseCursor = true;
}
}
void UMainMenuUIWidget::NewGameButtonOnClicked()
@ -81,3 +83,11 @@ void UMainMenuUIWidget::QuitButtonOnClicked()
// For some reason the generic version does not work the same as FWindowsPlatformMisc
FWindowsPlatformMisc::RequestExit(false);
}
void UMainMenuUIWidget::PlayHoveredSound()
{
if (ButtonHoveredSound)
{
UGameplayStatics::PlaySound2D(GetWorld(), ButtonHoveredSound);
}
}

View File

@ -41,6 +41,9 @@ public:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
TSoftObjectPtr<UWorld> NewGameLevel;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
USoundBase* ButtonHoveredSound;
private:
UUserWidget* currentNewGameWidget;
@ -63,4 +66,7 @@ private:
UFUNCTION()
void QuitButtonOnClicked();
UFUNCTION()
void PlayHoveredSound();
};