作为一个在游戏界面中显示的快捷栏,我们需要在游戏运行时就显示出快捷栏UI,故我们创建两个Widget。
1.SlAiGameHUDWidget:负责游戏中界面UI的整体显示
2.SlAiShortcutWidget:负责快捷栏部件的显示与逻辑
然后我们通过:
1.将GameHUDWidget添加进视口:在GameHUD文件中添加GameHUDWidget控件
GameHUDWidget.h:添加Widget指针
1 class SSDGameHUDWidget; 2 class ASDGameMode; 3 4 /** 5 * 6 */ 7 UCLASS() 8 class SANDBOXGAME_API ASDGameHUD : public AHUD 9 { 10 GENERATED_BODY() 11 12 public: 13 ASDGameHUD(); 14 15 protected: 16 virtual void BeginPlay() override; 17 18 private: 19 // 添加Widget指针 20 TSharedPtr<SSDGameHUDWidget> GameHUDWidget; 21 };View Code
GameHUDWidget.cpp:添加组件到视口
1 ASDGameHUD::ASDGameHUD() 2 { 3 // 如果视口存在 4 if (GEngine && GEngine->GameViewport) { 5 // 创建HUD控件 6 SAssignNew(GameHUDWidget, SSDGameHUDWidget); 7 // 添加到视口 8 GEngine->GameViewport->AddViewportWidgetContent(SNew(SWeakWidget).PossiblyNullContent(GameHUDWidget.ToSharedRef())); 9 } 10 }View Code
2.
标签:GameHUDWidget,ASDGameHUD,游戏,C++,添加,UI,快捷 From: https://www.cnblogs.com/wosun/p/18151251