虚幻引擎_C++_游戏开始菜单

如果要做一个开始菜单,实现的方法有很多

最佳实践:

  1. 创建一个MainMenuWidget, 继承自UUserWidget类, 然后再创建对应子类BP_MainMenuWidget

  2. 创建一个MainMenuLevel关卡, 专门用于播放开始菜单

  3. 为这个关卡创建一个MainMenuGameMode类,继承自GameModeBase类, 在创建对应子类BP_MainMenuGameMode

  4. 为这个MainMenuLevel关卡挂载这个游戏模式类

5.在MainMenuGameMode游戏模式类中, 创建MainMenuWidget蓝图子类

这就是最佳架构!

MainMenuWidget.h

cpp 复制代码
#pragma once

#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "Components/Button.h"
#include "MainMenuWidget.generated.h"

UCLASS()
class BATTLEBLASTER_API UMainMenuWidget : public UUserWidget
{
	GENERATED_BODY()

protected:
	// meta = (BindWidget) 必须确保蓝图中按钮的名字与此处变量名完全一致
	UPROPERTY(meta = (BindWidget))
	UButton* BtnSinglePlayer;

	UPROPERTY(meta = (BindWidget))
	UButton* BtnTwoPlayers;

	UPROPERTY(meta = (BindWidget))
	UButton* BtnQuitGame;

	// 必须重写此函数来绑定点击事件
	virtual void NativeConstruct() override;

	// 点击回调函数
	UFUNCTION()
	void OnSinglePlayerClicked();

	UFUNCTION()
	void OnTwoPlayersClicked();

	UFUNCTION()
	void OnQuitClicked();
};

MainMenuWidget.cpp

cpp 复制代码
#include "MainMenuWidget.h"
#include "Kismet/GameplayStatics.h"
#include "Kismet/KismetSystemLibrary.h"

void UMainMenuWidget::NativeConstruct()
{
	Super::NativeConstruct();

	// 绑定回调函数
	if (BtnSinglePlayer)
		BtnSinglePlayer->OnClicked.AddDynamic(this, &UMainMenuWidget::OnSinglePlayerClicked);

	if (BtnTwoPlayers)
		BtnTwoPlayers->OnClicked.AddDynamic(this, &UMainMenuWidget::OnTwoPlayersClicked);

	if (BtnQuitGame)
		BtnQuitGame->OnClicked.AddDynamic(this, &UMainMenuWidget::OnQuitClicked);
}

void UMainMenuWidget::OnSinglePlayerClicked()
{
	// 跳转到游戏关卡,并传递单人参数
	UGameplayStatics::OpenLevel(GetWorld(), TEXT("SinglePlayerLevel_1"), true, TEXT("Players=1"));
}

void UMainMenuWidget::OnTwoPlayersClicked()
{
	// 跳转到游戏关卡,并传递双人参数
	UGameplayStatics::OpenLevel(GetWorld(), TEXT("Level_1"), true, TEXT("Players=2"));
}

void UMainMenuWidget::OnQuitClicked()
{
	// 退出游戏逻辑
	UKismetSystemLibrary::QuitGame(GetWorld(), GetOwningPlayer(), EQuitPreference::Quit, false);
}

MainMenuGameMode.h

cpp 复制代码
#pragma once

#include "CoreMinimal.h"
#include "GameFramework/GameModeBase.h"
#include "MainMenuGameMode.generated.h"

UCLASS()
class BATTLEBLASTER_API AMainMenuGameMode : public AGameModeBase
{
	GENERATED_BODY()

protected:
	// 重写 BeginPlay
	virtual void BeginPlay() override;

	// 定义一个变量,用来在编辑器里选择你的 WBP_MainMenuWidget 蓝图
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "UI")
	TSubclassOf<class UUserWidget> MainMenuWidgetClass;

	// 用于保存创建出来的 UI 实例指针
	UPROPERTY()
	class UUserWidget* CurrentWidget;
};

MainMenuGameMode.cpp

cpp 复制代码
#include "MainMenuGameMode.h"
#include "Blueprint/UserWidget.h"
#include "Kismet/GameplayStatics.h"

void AMainMenuGameMode::BeginPlay()
{
	Super::BeginPlay();

	APlayerController* PC = GetWorld()->GetFirstPlayerController();

	if (PC && MainMenuWidgetClass)
	{
		// 1. 创建 Widget
		CurrentWidget = CreateWidget<UUserWidget>(GetWorld(), MainMenuWidgetClass);

		if (CurrentWidget)
		{
			// 2. 添加到视口
			CurrentWidget->AddToViewport();

			// 3. 设置输入模式为 UI Only
			FInputModeUIOnly InputMode;
			InputMode.SetWidgetToFocus(CurrentWidget->TakeWidget());
			InputMode.SetLockMouseToViewportBehavior(EMouseLockMode::DoNotLock);

			PC->SetInputMode(InputMode);

			// 4. 显示鼠标
			PC->bShowMouseCursor = true;
		}
	}
}

用 UMG 设计菜单 UI

  1. 在内容浏览器右键 → 用户界面Widget 蓝图 ,命名为 WBP_MainMenu
  2. 在蓝图的 "类设置" 中,将父类改为我们刚创建的 UMainMenu(C++ 类)。
  3. 打开 Widget 蓝图,拖入 3 个 Button 控件,分别命名为:
    • Btn_SinglePlayer
    • Btn_MultiPlayer
    • Btn_Quit
  4. 给按钮设置文本为 "单人模式"、"双人模式"、"退出游戏",并按你的图片布局排列。
相关推荐
云边散步14 分钟前
godot2D游戏教程系列二(15)
笔记·学习·游戏·游戏开发
条tiao条16 小时前
从 “猜数字游戏” 入门 BST:C 语言从零实现与核心操作
c语言·网络·游戏
派葛穆17 小时前
Unity-鼠标悬停改变物体层级
unity·游戏引擎
yingxiao88819 小时前
土耳其拟加强数字平台监管;腾讯或参投派拉蒙收购华纳兄弟交易
游戏·ai·腾讯·手游出海·任天堂·clash royale
云边散步20 小时前
godot2D游戏教程系列二(14)
笔记·学习·游戏·游戏开发
雾江流1 天前
AGG 3.6.35 | GG游戏解锁器魔改版,界面更优美
游戏·软件工程
林鸿群1 天前
Visual Studio 2026 工程升级实战:184 个游戏项目的自动化迁移之路
游戏·自动化·visual studio
Yasin Chen2 天前
Unity TMP_SDF 分析(二)数据来源2
unity·游戏引擎
相信神话20212 天前
《酒魂》游戏开发实战——从设计思想到 Godot 实现(单机完整版)
游戏引擎·godot
王杨游戏养站系统2 天前
360蜘蛛冲52W游戏站自动养站系统
游戏·游戏下载站养站系统·游戏养站系统