咱们在玩游戏的时候,通常会和界面进行各种各样的互动,而且会动都是在不同的界面上,所以需要在不同的界面上进行切换或者多个HUD重叠显示在一起。
首先创建两个HUD
2.重新定义一个PlayerController类
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include "ChangeHUD/IChangeHUD.h"
#include "CameraPlayerController.generated.h"
/**
*
*/
UCLASS()
class TRUEFPSPROJECT_API ACameraPlayerController
: public APlayerController
, public IChangeHUD {
GENERATED_BODY()
public:
ACameraPlayerController();
//编辑器内UMG资产的引用
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AAA")
TSubclassOf<class UUserWidget> mainHUDAsset;
//创建后用于空间变量
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AAA")
UUserWidget* mainHUDMenu;
//编辑器内UMG资产的引用
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AAA")
TSubclassOf<class UUserWidget> subHUDAsset;
//创建后用于空间变量
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AAA")
UUserWidget* subHUDMenu;
virtual void BeginPlay();
virtual void changeHUD()override;
private:
int32 HUDChoose = 0;//切换不同的HUD界面
UUserWidget* _currentHUD = nullptr;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "CameraPlayerController.h"
#include "Blueprint/UserWidget.h"
#include "ChangeHUD/ChangeHUDManager.h"
ACameraPlayerController::ACameraPlayerController() {
ChangeHUDMgr()->changeHUDInterface = this;
}
void ACameraPlayerController::BeginPlay() {
Super::BeginPlay();
if (mainHUDAsset) {
mainHUDMenu = CreateWidget<UUserWidget>(this, mainHUDAsset);
}
if (subHUDAsset) {
subHUDMenu = CreateWidget<UUserWidget>(this, subHUDAsset);
}
}
void ACameraPlayerController::changeHUD() {
if (_currentHUD) {
_currentHUD->RemoveFromViewport();
}
HUDChoose += 1;
if (HUDChoose%3 == 0){
_currentHUD = mainHUDMenu;
_currentHUD->AddToViewport();
} else if(HUDChoose % 3 == 1) {
_currentHUD = subHUDMenu;
_currentHUD->AddToViewport();
}else{
mainHUDMenu->AddToViewport();
subHUDMenu->AddToViewport();
}
}
定义一个接口类和接口管理类
#pragma once
class IChangeHUD {
public:
virtual void changeHUD() = 0;
};
#pragma once
#define ChangeHUDMgr() (ChangeHUDManager::getInstance())
class ChangeHUDManager {
private:
ChangeHUDManager();
static ChangeHUDManager* _instance;
public:
static ChangeHUDManager* getInstance();
class IChangeHUD* changeHUDInterface;
};
#include "ChangeHUDManager.h"
#include "IChangeHUD.h"
ChangeHUDManager* ChangeHUDManager::_instance = new ChangeHUDManager;
ChangeHUDManager::ChangeHUDManager() {
}
ChangeHUDManager* ChangeHUDManager::getInstance() {
return _instance;
}
在角色类中通过按键H来调用:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "SpawnActor.generated.h"
UCLASS()
class TRUEFPSPROJECT_API ASpawnActor : public AActor {
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ASpawnActor();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
public:
UPROPERTY(EditAnywhere, Category = "AAA")
TSubclassOf<class AFloatingActor> subFloatingActor;
float baseTime = 0.0;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "SpawnActor.h"
#include "FloatingActor.h"
// Sets default values
ASpawnActor::ASpawnActor() {
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void ASpawnActor::BeginPlay() {
Super::BeginPlay();
}
// Called every frame
void ASpawnActor::Tick(float DeltaTime) {
Super::Tick(DeltaTime);
if ((baseTime += DeltaTime) <= 3.0){
return;
}
baseTime = 0.0;
UWorld* world = GetWorld();
if (world){
FVector spawnLocation = GetActorLocation();
FRotator spwanRotation = GetActorRotation();
FActorSpawnParameters spawnParam;
spawnParam.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButDontSpawnIfColliding;
world->SpawnActor<AFloatingActor>(subFloatingActor, spawnLocation, spwanRotation, spawnParam);
}
}
点击【运行】
aaa