在WidgetController内新建一个OverlayAuraWidgetController,然后修改HUD的初始状态
AuraHUD
// Fill out your copyright notice in the Description page of Project Settings. #pragma once #include "CoreMinimal.h" #include "GameFramework/HUD.h" #include "UI/Widget/AuraUserWidget.h" #include "UI/WidgetController/OverlayAuraWidgetController.h" #include "AuraHUD.generated.h" struct FWidgetControllerParams; /** * */ UCLASS() class MYGAS_API AAuraHUD : public AHUD { GENERATED_BODY() protected: public: UPROPERTY() TObjectPtr<UAuraUserWidget> OverlayWidget; UOverlayAuraWidgetController* GetOverlayAuraWidgetController(const FWidgetControllerParams& WCParams); void InitOverlay(APlayerController* PC,APlayerState* PS,UAbilitySystemComponent* ASC , UAttributeSet* AS); private: UPROPERTY(EditAnywhere) TSubclassOf<UAuraUserWidget> OverlayWidgetClass; UPROPERTY() TObjectPtr<UOverlayAuraWidgetController> OverlayAuraWidgetController; UPROPERTY(EditAnywhere) TSubclassOf<UOverlayAuraWidgetController> OverlayAuraWidgetControllerClass; };
AuraHUD.cpp
// Fill out your copyright notice in the Description page of Project Settings. #include "UI/HUD/AuraHUD.h" UOverlayAuraWidgetController* AAuraHUD::GetOverlayAuraWidgetController(const FWidgetControllerParams& WCParams) { if(OverlayAuraWidgetController==nullptr) { OverlayAuraWidgetController = NewObject<UOverlayAuraWidgetController>(this,OverlayAuraWidgetControllerClass); OverlayAuraWidgetController->SetWidgetControllerParams(WCParams); return OverlayAuraWidgetController; } return OverlayAuraWidgetController; } void AAuraHUD::InitOverlay(APlayerController* PC, APlayerState* PS, UAbilitySystemComponent* ASC, UAttributeSet* AS) { checkf(OverlayWidgetClass , TEXT("Overlay Widget Class uninitialized , please fill out BP_AuraHUD")); checkf(OverlayAuraWidgetControllerClass , TEXT("Overlay Widget Class uninitialized , please fill out OverlayAuraWidgetControllerClass")); UUserWidget* Widget = CreateWidget<UUserWidget>(GetWorld(),OverlayWidgetClass); OverlayWidget = Cast<UAuraUserWidget>(Widget); //创建一个 FWidgetControllerParams 结构 const FWidgetControllerParams WidgetControllerParams(PC,PS,ASC,AS); //将 FWidgetControllerParams 传到上面的 GetOverlayAuraWidgetController 内 UOverlayAuraWidgetController* WidgetController = GetOverlayAuraWidgetController(WidgetControllerParams); OverlayWidget->SetWidgetController(WidgetController); Widget->AddToViewport(); }
回到AuraCharacter内修改初始化
AuraCharacter.cpp
void AAuraCharacter::InitAbilityActorInfo() { AAuraPlayerState* AuraPlayerState = GetPlayerState<AAuraPlayerState>(); check(AuraPlayerState); AuraPlayerState->GetAbilitySystemComponent()->InitAbilityActorInfo(AuraPlayerState,this); AbilitySystemComponent = AuraPlayerState->GetAbilitySystemComponent(); AttributesSet = AuraPlayerState->GetAttributeSet(); //当角色初始化的时候加载UI的初始化 if(AAuraPlayerController* AuraPlayerController = Cast<AAuraPlayerController>(GetController())) { //获得HUD AAuraHUD* AuraHUD = Cast<AAuraHUD>(AuraPlayerController->GetHUD()); AuraHUD->InitOverlay(AuraPlayerController,AuraPlayerState,AbilitySystemComponent,AttributesSet); } }
回到蓝图,在WBP_AuraHUD内的OverlayAuraWidgetController内添加新创建的OverlayAuraWidgetController