首先自定义一个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 }
效果如下: