UE5 GAS开发P31 将hud绑定在自己的角色上

在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

相关推荐
DBBH6 小时前
UE5 第三人称学习之动画 control rig
ue5
UTwelve6 小时前
【UE5】一种老派的假反射做法,可以用于移动端,或对反射的速度、清晰度有需求的地方
ue5·虚幻引擎·着色器·虚幻4
UTwelve10 小时前
【UE5】可以实时绘制的体积渲染 【第三章 体积纹理绘制 - 3.绘制体积】
ue5
1204157137 肖哥14 小时前
UE5.4 PCG基础节点
ue5
DBBH2 天前
UE5 材质篇 1 如何偏移顶点
ue5·材质
孤客网络科技工作室2 天前
虚幻引擎5(UE5)学习教程
java·学习·ue5
暮志未晚Webgl2 天前
105. UE5 GAS RPG 搭建主菜单
ue5
异次元的归来2 天前
UE5相机系统初探(一)
ue5·游戏引擎·camera
DBBH4 天前
UE5 材质篇 0 创建一个材质
ue5
暮志未晚Webgl8 天前
102. UE5 GAS RPG 实现范围技能奥术伤害
android·java·ue5