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

相关推荐
咖肥猫8 小时前
【ue5学习笔记2】在场景放入一个物体的蓝图输入事件无效?
笔记·学习·ue5
我的巨剑能轻松搅动潮汐3 天前
【UE5】pmx导入UE5,套动作。(防止“气球人”现象。
ue5
windwind20003 天前
UE5 跟踪能力的简单小怪
ue5
Deveuper5 天前
UE5 C+、C++、C# 构造方法区别示例
c++·ue5·c#·ue4
windwind20005 天前
UE5 学习方法的思考
ue5·学习方法
ue星空7 天前
UE材质常用节点
ue5·虚幻·材质·虚幻引擎
Zhichao_977 天前
【UE5 C++课程系列笔记】09——多播委托的基本使用
笔记·ue5
异次元的归来9 天前
UE5的TRS矩阵
线性代数·矩阵·ue5·游戏引擎·unreal engine
电子云与长程纠缠9 天前
UE5编辑器下将RenderTarget输出为UTexture并保存
学习·ue5·编辑器·虚幻
ue星空10 天前
虚幻5描边轮廓材质
ue5·材质