基于C++代码的UE4学习(三十六)——用Canvas创建HUD

 

首先自定义一个GameMode。(在此之前不要忘了进入xx.Build.cs文件中,把带有Slate,SlateCore模块的语句进行启用)

 

 1 // Fill out your copyright notice in the Description page of Project Settings.
 2 
 3 #pragma once
 4 
 5 #include "CoreMinimal.h"
 6 #include "GameFramework/GameModeBase.h"
 7 #include "CustomModeBase.generated.h"
 8 
 9 /**
10  * 
11  */
12 
13 UCLASS()
14 class MYPROJECT16_UMG_API ACustomModeBase : public AGameModeBase
15 {
16     GENERATED_BODY()
17 
18 public:
19     ACustomModeBase();
20     
21 };

 

 

1 // Fill out your copyright notice in the Description page of Project Settings.
2 
3 
4 #include "CustomModeBase.h"
5 #include "CustomHUD.h"
6 
7 ACustomModeBase::ACustomModeBase() {
8     HUDClass = ACustomHUD::StaticClass();
9 }

 

在构造函数中创建一个HUDClass的实例。

在创建继承于HUD类的子类,起名为CustomHUD。

 

1 // Fill out your copyright notice in the Description page of Project Settings.
 2 
 3 #pragma once
 4 
 5 #include "CoreMinimal.h"
 6 #include "GameFramework/HUD.h"
 7 #include "CustomHUD.generated.h"
 8 
 9 /**
10  * 
11  */
12 UCLASS()
13 class MYPROJECT16_UMG_API ACustomHUD : public AHUD
14 {
15     GENERATED_BODY()
16 public:
17     ACustomHUD();
18 
19     virtual void DrawHUD() override;
20 };

 

准备重写DrawHUD方法,该方法来自父类。

 

 1 // Fill out your copyright notice in the Description page of Project Settings.
 2 
 3 
 4 #include "CustomHUD.h"
 5 #include "Engine.h"
 6 #include "Engine/Canvas.h"
 7 
 8 ACustomHUD::ACustomHUD() {
 9     GEngine->AddOnScreenDebugMessage(-1,10,FColor::Red,TEXT("HUD is Set!"));
10 }
11 
12 void ACustomHUD::DrawHUD()
13 {
14     Super::DrawHUD();
15 
16     Canvas->DrawText(GEngine->GetSmallFont(), TEXT("HUD Test String is Setting!"), 10, 10,2,2); //给个文字说明
17     FCanvasBoxItem ProgressBar(FVector2D(5,25), FVector2D(100,5)); //方块坐标与宽高
18     Canvas->DrawItem(ProgressBar);
19     //DrawRect(FColor::Blue, 5, 25, 100, 5);
20 }


效果如下:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值