12:为MainMenuMap创建GameModeBase类和HUD类和playcontroller类。即继承GameModeBase类和HUD类和playcontroller类,playcontroller类是为了将鼠标显示出来,重写HUD类的beginplay函数,并且在begameplay时,创建widget,并addtoviewport,如下图。
13:在MainMenuMap的GamemodeBase类中,设置默认的HUD类成员为12步骤创建的HUD,playcontroller为12创建的类。
14:设置MainMenuMap的默认gamemodebase及hud。如下图:
15:设置mainmenu的button对应的函数,使之点击按钮实现相应的动作,如点击开始按钮,则进入第三人称模板。如下图:
16:设置默认地图,Edit->ProjectSettings,如下图:
到此,设置菜单关卡就已经完成。测试成功如下:
下面贴出涉及到的代码。
MainMenuUserWidget.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "MainMenuUserWidget.generated.h"
/**
*
*/
UCLASS()
class BLOGPROJECT_API UMainMenuUserWidget : public UUserWidget
{
GENERATED_BODY()
UPROPERTY(meta = (BindWidget))
class UButton* StartGameBtn;
UPROPERTY(meta = (BindWidget))
class UButton* SettingBtn;
UPROPERTY(meta = (BindWidget))
class UButton* QuitGameBtn;
virtual bool Initialize() override;
public:
UFUNCTION()
void OnClickedStartGame();
UFUNCTION()
void OnClickedSetting();
UFUNCTION()
void OnClickedQuitGame();
};
MainMenuUserWidget.CPP
// Fill out your copyright notice in the Description page of Project Settings.
#include "MainMenuUserWidget.h"
#include "Components/Button.h"
bool UMainMenuUserWidget::Initialize()
{
Super::Initialize();
if (StartGameBtn != nullptr)
{
StartGameBtn->OnClicked.AddDynamic(this, &UMainMenuUserWidget::OnClickedStartGame);
}
if (SettingBtn != nullptr)
{
SettingBtn->OnClicked.AddDynamic(this, &UMainMenuUserWidget::OnClickedSetting);
}
if (QuitGameBtn != nullptr)
{
QuitGameBtn->OnClicked.AddDynamic(this, &UMainMenuUserWidget::OnClickedQuitGame);
}
return true;
}
void UMainMenuUserWidget::OnClickedStartGame()
{
FString NewMap = FString::Printf(TEXT("/Game/ThirdPersonCPP/Maps/ThirdPersonExampleMap"));
GetWorld()->ServerTravel(NewMap);
}
void UMainMenuUserWidget::OnClickedSetting()
{
UE_LOG(LogTemp, Warning, TEXT("OnClicked Setting"));
}
void UMainMenuUserWidget::OnClickedQuitGame()
{
APlayerController* PC = GetWorld()->GetFirstPlayerController();
if (PC != nullptr)
{
PC->ConsoleCommand(TEXT("quit"));
}
}
MainMenuPlayerController.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include "MainMenuPlayerController.generated.h"
/**
*
*/
UCLASS()
class BLOGPROJECT_API AMainMenuPlayerController : public APlayerController
{
GENERATED_BODY()
AMainMenuPlayerController();
};
MainMenuPlayerController.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MainMenuPlayerController.h"
AMainMenuPlayerController::AMainMenuPlayerController()
{
bShowMouseCursor = true;
}
MainMenuHUD.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/HUD.h"
#include "MainMenuHUD.generated.h"
/**
*
*/
UCLASS()
class BLOGPROJECT_API AMainMenuHUD : public AHUD
{
GENERATED_UCLASS_BODY()
virtual void BeginPlay() override;
private:
TSubclassOf MainMenuWidgetClass;
};
MainMenuHUD.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MainMenuHUD.h"
#include "UObject/ConstructorHelpers.h"
#include "MainMenuUserWidget.h"
AMainMenuHUD::AMainMenuHUD(const FObjectInitializer& Initailizer)
: Super(Initailizer)
{
static ConstructorHelpers::FClassFinder MainMenuWidgetBP(TEXT("/Game/ThirdPersonCPP/UI/MainMenu")); // 找到蓝图类
if (MainMenuWidgetBP.Succeeded())
{
MainMenuWidgetClass = MainMenuWidgetBP.Class; // 得到class
}
}
void AMainMenuHUD::BeginPlay()
{
if (MainMenuWidgetClass != nullptr)
{
UMainMenuUserWidget* Widget = CreateWidget(GetWorld()->GetGameInstance(), MainMenuWidgetClass);
if (Widget != nullptr)
{
Widget->AddToViewport();
}
}
}
MainMenuGameModeBase.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/GameModeBase.h"
#include "MainMenuGameModeBase.generated.h"
/**
*
*/
UCLASS()
class BLOGPROJECT_API AMainMenuGameModeBase : public AGameModeBase
{
GENERATED_BODY()
AMainMenuGameModeBase();
};
MainMenuGameModeBase.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MainMenuGameModeBase.h"
#include "MainMenuHUD.h"
#include "MainMenuPlayerController.h"
AMainMenuGameModeBase::AMainMenuGameModeBase()
{
HUDClass = AMainMenuHUD::StaticClass();
PlayerControllerClass = AMainMenuPlayerController::StaticClass();
}