添加UI显示
编辑工程的.build.cs 添加UMG Slate SlateCore
// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
using UnrealBuildTool;
public class BatteryCollector : ModuleRules
{
public BatteryCollector(TargetInfo Target)
{
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "UMG", "HeadMountedDisplay" });
PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore"});
}
}
编辑BatteryCollectorGameMode.h 添加UserWidget class 和UserWIdget PowerToWin(这节用不到) 添加BeginPlay函数
编辑BatteryCollectorGameMode.cpp 添加头文件
include "Blueprint/UserWidget.h"
在BeginPlay函数中 初始化PowerToWin 并且调用CreateWidget 创建并添加UI
最终BatteryCollectorGameMode.h
// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
pragma once
include "GameFramework/GameModeBase.h"
include "BatteryCollectorGameMode.generated.h"
UCLASS(minimalapi)
class ABatteryCollectorGameMode : public AGameModeBase
{
GENERATED_BODY()
public:
ABatteryCollectorGameMode();
virtual void Tick(float DeltaSeconds) override;
UFUNCTION(BlueprintPure, Category = "Power")
float GetPowerToWin() const;
virtual void BeginPlay() override;
protected:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Power")
float DecayRate;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Power")
float PowerToWin;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Power")
TSubclassOf<class UUserWidget> HUDWidgetClass;
UPROPERTY()
class UUserWidget* CurrentWidget;
};
最终BatteryCollectorGameMode.cpp
// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
include "BatteryCollector.h"
include "BatteryCollectorGameMode.h"
include "BatteryCollectorCharacter.h"
include "Kismet/GameplayStatics.h"
include "Blueprint/UserWidget.h"
ABatteryCollectorGameMode::ABatteryCollectorGameMode()
{
// set default pawn class to our Blueprinted character
static ConstructorHelpers::FClassFinder<APawn> PlayerPawnBPClass(TEXT("/Game/ThirdPersonCPP/Blueprints/ThirdPersonCharacter"));
if (PlayerPawnBPClass.Class != NULL)
{
DefaultPawnClass = PlayerPawnBPClass.Class;
}
DecayRate = 0.01f;
PrimaryActorTick.bCanEverTick = true;
}
void ABatteryCollectorGameMode::BeginPlay()
{
Super::BeginPlay();
ABatteryCollectorCharacter* MyCharacter = Cast<ABatteryCollectorCharacter>(UGameplayStatics::GetPlayerPawn(this, 0));
if (MyCharacter)
{
PowerToWin = MyCharacter->GetInitialPower()*1.25f;
}
if (HUDWidgetClass != NULL)
{
CurrentWidget = CreateWidget<UUserWidget>(GetWorld(), HUDWidgetClass);
if(CurrentWidget != NULL)
{
CurrentWidget->AddToViewport();
}
}
}
void ABatteryCollectorGameMode::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);
ABatteryCollectorCharacter* MyCharacter = Cast<ABatteryCollectorCharacter>(UGameplayStatics::GetPlayerPawn(this, 0));
if (MyCharacter)
{
if (MyCharacter->GetCurrentPower() > 0)
{
MyCharacter->UpdatePower(-DeltaSecondsDecayRate(MyCharacter->GetInitialPower()));
}
}
}
//编译通过 临时添加
float ABatteryCollectorGameMode::GetPowerToWin()const
{
return 0.0f;
}
编译如果出现无法找到"Blueprint/UserWidget" 的情况 按照下图刷新工程 重新编译