当前位置: 首页 > article >正文

UE5 GAS开发p30 创建UI HUD 血条

新建AuraUserWidget,AuraWidgetController,AuraHUD

首先设置AuraWidgetController.h

// Fill out your copyright notice in the Description page of Project Settings. #pragma once #include "CoreMinimal.h" #include "AbilitySystemComponent.h" #include "GameFramework/PlayerState.h" #include "UObject/NoExportTypes.h" #include "AuraWidgetController.generated.h" /** * 这段代码定义了一个结构体 FWidgetControllerParams,用于存储与控制 Widget 相关的参数,包括玩家控制器、玩家状态、能力系统组件和属性集等。这些参数可以在 Unreal Engine 中进行编辑,并且可以在蓝图中读写 */ USTRUCT(BlueprintType) struct FWidgetControllerParams { GENERATED_BODY() /*FWidgetControllerParams(){}:这是结构体的默认构造函数。在这个例子中,它被定义为空函数体,因为没有特殊的初始化逻辑需要执行。*/ FWidgetControllerParams(){} /*这是结构体的自定义构造函数。它接受四个参数,分别是指向玩家控制器(PlayerController)、玩家状态(PlayerState)、能力系统组件(AbilitySystemComponent)和属性集(AttributeSet)的指针。*/ FWidgetControllerParams(APlayerController* PC,APlayerState* PS,UAbilitySystemComponent* ASC,UAttributeSet* AS) : PlayerController(PC),PlayerState(PS),AbilitySystemComponent(ASC),AttributeSet(AS) {} /*这里是初始化参数*/ UPROPERTY(EditAnywhere,BlueprintReadWrite) TObjectPtr<APlayerController> PlayerController = nullptr; UPROPERTY(EditAnywhere,BlueprintReadWrite) TObjectPtr<APlayerState> PlayerState = nullptr; UPROPERTY(EditAnywhere,BlueprintReadWrite) TObjectPtr<UAbilitySystemComponent> AbilitySystemComponent = nullptr; UPROPERTY(EditAnywhere,BlueprintReadWrite) TObjectPtr<UAttributeSet> AttributeSet = nullptr; }; UCLASS() class MYGAS_API UAuraWidgetController : public UObject { GENERATED_BODY() public: UFUNCTION(BlueprintCallable) void SetWidgetControllerParams(const FWidgetControllerParams& WidgetControllerParams); protected: UPROPERTY(BlueprintReadOnly,Category="WidgetController") TObjectPtr<APlayerController> PlayerController; UPROPERTY(BlueprintReadOnly,Category="WidgetController") TObjectPtr<APlayerState> PlayerState; UPROPERTY(BlueprintReadOnly,Category="WidgetController") TObjectPtr<UAbilitySystemComponent> AbilitySystemComponent; UPROPERTY(BlueprintReadOnly,Category="WidgetController") TObjectPtr<UAttributeSet> AttributeSet; };

AuraUserWidget.h

// Fill out your copyright notice in the Description page of Project Settings. #pragma once #include "CoreMinimal.h" #include "Blueprint/UserWidget.h" #include "AuraUserWidget.generated.h" /** * */ UCLASS() class MYGAS_API UAuraUserWidget : public UUserWidget { GENERATED_BODY() public: UFUNCTION(BlueprintCallable) void SetWidgetController(UObject* InWidgetController); UPROPERTY(BlueprintReadOnly) TObjectPtr<UObject> WidgetController; protected: UFUNCTION(BlueprintImplementableEvent) void WidgetControllerSet(); };

AuraUserWidget.cpp

// Fill out your copyright notice in the Description page of Project Settings. #include "UI/Widget/AuraUserWidget.h" void UAuraUserWidget::SetWidgetController(UObject* InWidgetController) { if(WidgetController) { WidgetController = InWidgetController; WidgetControllerSet(); } }

AuraHUD.h

// 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 "AuraHUD.generated.h" /** * */ UCLASS() class MYGAS_API AAuraHUD : public AHUD { GENERATED_BODY() protected: virtual void BeginPlay() override; public: UPROPERTY() TObjectPtr<UAuraUserWidget> OverlayWidget; private: UPROPERTY(EditAnywhere) TSubclassOf<UAuraUserWidget> OverlayWidgetClass; };

在资源内容文件夹新建UI文件夹,并且加入HUD,Overlay,ProgressBar

首先是ProgressBarBase,继承AuraUserWidget,然后这里使用的是SizaBox更改大小,创建Overlay覆盖层再创建image和ProgressBar

新建蓝图和变量SlateBrush

新建Widget,继承ProgressBarBase

然后修改里面的图片

在Overlay文件夹内继承AuraWidget

当我自己跟着视频的时候,在创建Overlay这个Panel内加入Health的这一步引擎报错

LoginId:edee00a84bb31aad766903a2392b159c EpicAccountId:1f362366af2948929c3b625a0f149b84 Assertion failed: LocalPropertyContainer != nullptr [File:D:\build\++UE5\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\ScriptCore.cpp] [Line: 2888] UnrealEditor_CoreUObject!UObject::execLet() [D:\build\++UE5\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\ScriptCore.cpp:2888] UnrealEditor_CoreUObject!ProcessLocalScriptFunction() [D:\build\++UE5\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\ScriptCore.cpp:1199] UnrealEditor_CoreUObject!ProcessScriptFunction<void (__cdecl*)(UObject *,FFrame &,void *)>() [D:\build\++UE5\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\ScriptCore.cpp:1029] UnrealEditor_CoreUObject!ProcessLocalFunction() [D:\build\++UE5\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\ScriptCore.cpp:1269] UnrealEditor_CoreUObject!ProcessLocalScriptFunction() [D:\build\++UE5\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\ScriptCore.cpp:1199] UnrealEditor_CoreUObject!ProcessScriptFunction<void (__cdecl*)(UObject *,FFrame &,void *)>() [D:\build\++UE5\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\ScriptCore.cpp:1029] UnrealEditor_CoreUObject!ProcessLocalFunction() [D:\build\++UE5\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\ScriptCore.cpp:1269] UnrealEditor_CoreUObject!ProcessLocalScriptFunction() [D:\build\++UE5\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\ScriptCore.cpp:1199] UnrealEditor_CoreUObject!UObject::ProcessInternal() [D:\build\++UE5\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\ScriptCore.cpp:1296] UnrealEditor_CoreUObject!UFunction::Invoke() [D:\build\++UE5\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\Class.cpp:6455] UnrealEditor_CoreUObject!UObject::ProcessEvent() [D:\build\++UE5\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\ScriptCore.cpp:2127] UnrealEditor_UMG!UUserWidget::NativePreConstruct() [D:\build\++UE5\Sync\Engine\Source\Runtime\UMG\Private\UserWidget.cpp:1355] UnrealEditor_UMG!UUserWidget::OnWidgetRebuilt() [D:\build\++UE5\Sync\Engine\Source\Runtime\UMG\Private\UserWidget.cpp:812] UnrealEditor_UMG!UWidget::TakeWidget_Private() [D:\build\++UE5\Sync\Engine\Source\Runtime\UMG\Private\Components\Widget.cpp:999] UnrealEditor_UMG!UWidget::TakeWidget() [D:\build\++UE5\Sync\Engine\Source\Runtime\UMG\Private\Components\Widget.cpp:916] UnrealEditor_UMG!UCanvasPanelSlot::DragDropPreviewByDesigner() [D:\build\++UE5\Sync\Engine\Source\Runtime\UMG\Private\Components\CanvasPanelSlot.cpp:94] UnrealEditor_UMGEditor!SDesignerView::ProcessDropAndAddWidget() [D:\build\++UE5\Sync\Engine\Source\Editor\UMGEditor\Private\Designer\SDesignerView.cpp:2687] UnrealEditor_UMGEditor!SDesignerView::OnDrop() [D:\build\++UE5\Sync\Engine\Source\Editor\UMGEditor\Private\Designer\SDesignerView.cpp:2979] UnrealEditor_Slate!<lambda_471858b7e70a8b68bd65bc435c2a047c>::operator()() [D:\build\++UE5\Sync\Engine\Source\Runtime\Slate\Private\Framework\Application\SlateApplication.cpp:5088] UnrealEditor_Slate!FEventRouter::Route<FReply,FEventRouter::FBubblePolicy,FPointerEvent,<lambda_471858b7e70a8b68bd65bc435c2a047c> >() [D:\build\++UE5\Sync\Engine\Source\Runtime\Slate\Private\Framework\Application\SlateApplication.cpp:412] UnrealEditor_Slate!FSlateApplication::RoutePointerUpEvent() [D:\build\++UE5\Sync\Engine\Source\Runtime\Slate\Private\Framework\Application\SlateApplication.cpp:5083] UnrealEditor_Slate!FSlateApplication::ProcessMouseButtonUpEvent() [D:\build\++UE5\Sync\Engine\Source\Runtime\Slate\Private\Framework\Application\SlateApplication.cpp:5601] UnrealEditor_Slate!FSlateApplication::OnMouseUp() [D:\build\++UE5\Sync\Engine\Source\Runtime\Slate\Private\Framework\Application\SlateApplication.cpp:5566] UnrealEditor_ApplicationCore!FWindowsApplication::ProcessDeferredMessage() [D:\build\++UE5\Sync\Engine\Source\Runtime\ApplicationCore\Private\Windows\WindowsApplication.cpp:2219] UnrealEditor_ApplicationCore!FWindowsApplication::DeferMessage() [D:\build\++UE5\Sync\Engine\Source\Runtime\ApplicationCore\Private\Windows\WindowsApplication.cpp:2726] UnrealEditor_ApplicationCore!FWindowsApplication::ProcessMessage() [D:\build\++UE5\Sync\Engine\Source\Runtime\ApplicationCore\Private\Windows\WindowsApplication.cpp:1895] UnrealEditor_ApplicationCore!FWindowsApplication::AppWndProc() [D:\build\++UE5\Sync\Engine\Source\Runtime\ApplicationCore\Private\Windows\WindowsApplication.cpp:925] user32 user32 UnrealEditor_ApplicationCore!FWindowsPlatformApplicationMisc::PumpMessages() [D:\build\++UE5\Sync\Engine\Source\Runtime\ApplicationCore\Private\Windows\WindowsPlatformApplicationMisc.cpp:142] UnrealEditor!FEngineLoop::Tick() [D:\build\++UE5\Sync\Engine\Source\Runtime\Launch\Private\LaunchEngineLoop.cpp:5293] UnrealEditor!GuardedMain() [D:\build\++UE5\Sync\Engine\Source\Runtime\Launch\Private\Launch.cpp:202] UnrealEditor!GuardedMainWrapper() [D:\build\++UE5\Sync\Engine\Source\Runtime\Launch\Private\Windows\LaunchWindows.cpp:107] UnrealEditor!LaunchWindowsStartup() [D:\build\++UE5\Sync\Engine\Source\Runtime\Launch\Private\Windows\LaunchWindows.cpp:244] UnrealEditor!WinMain() [D:\build\++UE5\Sync\Engine\Source\Runtime\Launch\Private\Windows\LaunchWindows.cpp:282] UnrealEditor!__scrt_common_main_seh() [D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288] kernel32 ntdll

于是我在AuraUserWidget.cpp内加上了if,判断当前WidgetController是否是真

void UAuraUserWidget::SetWidgetController(UObject* InWidgetController) { if(WidgetController) { WidgetController = InWidgetController; WidgetControllerSet(); } }

加上后就可以正常编译了

在内容内新建HUD,继承AuraHud.h

把Overlay Widget Class修改成创建的WBP_Overlay

最后放入Gamemode内


http://www.kler.cn/a/274826.html

相关文章:

  • BenchmarkSQL使用教程
  • springboot453工资信息管理系统(论文+源码)_kaic
  • Python-存储数据-Thu-Fri
  • 时间序列预测论文阅读和相关代码库
  • 5G 模组 初始化状态检测
  • @pytest.fixture() 跟 @pytest.fixture有区别吗?
  • 【Anaconda】换源常用命令
  • 风丘电动汽车高压测试方案 助您高效应对车辆试验难题
  • Spark on Yarn安装配置
  • Java后台生成多个Excel并用Zip打包后(可以将excel文件放置到不同的目录)下载
  • JS+CSS3点击粒子烟花动画js特效
  • 在CentOS 7服务器上添加和删除用户的方法
  • C++:类和对象(上篇)
  • 网络工程师练习题3
  • RO段、RW段和ZI段的理解
  • 一次完整的 HTTP 请求所经历的步骤
  • XML语言的学习记录5- XSD
  • 智慧城市的发展趋势与挑战:未来展望
  • python 笔记:locals
  • Android Studio实现内容丰富的安卓外卖平台
  • C# ---Extension Method
  • 当人工智能遇上“理解偏差”:一场关于天气的宇宙之旅
  • 【Python 48小时速成 3】输入与输出
  • Yarn 管理的前端项目转换为使用 npm
  • docker入门(五)—— 小练习,docker安装nginx、elasticsearch
  • python大学生健身爱好者交流网站flask-django-nodejs-php