• UE4 通过按键切换不同的HUD


    咱们在玩游戏的时候,通常会和界面进行各种各样的互动,而且会动都是在不同的界面上,所以需要在不同的界面上进行切换或者多个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;
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    // 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();
    	}
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    定义一个接口类和接口管理类

    #pragma once
    class IChangeHUD {
    
    public:
    	virtual void changeHUD() = 0;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    #pragma once
    
    #define ChangeHUDMgr() (ChangeHUDManager::getInstance())
    
    class ChangeHUDManager {
    private:
    	ChangeHUDManager();
    	static ChangeHUDManager* _instance;
    public:
    	static ChangeHUDManager* getInstance();
    
    	class IChangeHUD* changeHUDInterface;
    };
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    #include "ChangeHUDManager.h"
    #include "IChangeHUD.h"
    
    ChangeHUDManager* ChangeHUDManager::_instance = new ChangeHUDManager;
    
    ChangeHUDManager::ChangeHUDManager() {
    
    }
    
    ChangeHUDManager* ChangeHUDManager::getInstance() {
    	return _instance;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在角色类中通过按键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;
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    // 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);
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    点击【运行】
    在这里插入图片描述
    aaa

  • 相关阅读:
    单调队列 → 常用于动态规划问题的优化
    Docker 的学习笔记-docker的常用命令
    【毕业设计】天气预测与数据分析系统 - 机器学习 python
    c++11 智能指针-辅助类 (std::bad_weak_ptr)
    RK3588 实现温控风扇之通过extcon子系统上报状态(三)
    网页里面的3D交互展示是怎么做的呢?
    重载、重写(覆盖)、重定义(隐藏)
    QNX在车机系统的应用
    CISP-PTE实操练习题讲解一(新版)
    【Filament】立方体贴图(6张图)
  • 原文地址:https://blog.csdn.net/wb175208/article/details/128063743