Unreal Engine/이론

[Unreal Engine UI] 1) UI 화면 만들기

GiveZero 2023. 1. 25. 23:23

Blueprint만 이용하여 Widget 만들기

Widget Blueprint를 선택하여 Widget_Intro 생성

 

다음과 같은 화면 생성

 

 

시작시 UI화면 등장시키기

Open Level Blueprint

Create Widget Intro Widget 에서 Class에서 자신이 원하는 Widget 선택

 

 

화면전환

버튼 Event에서 OnClicked 선택

 

 


C++ 클래스를 이용하여 화면전환 구현하기

먼저, 프로젝트.Build.cs 수정 (UMG,Slate, SlateCore 추가)

public class UIPractice : ModuleRules
{
	public UIPractice(ReadOnlyTargetRules Target) : base(Target)
	{
		PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;

		PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "HeadMountedDisplay", "EnhancedInput", "UMG" });

		PublicDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });

    }
}

 

GameMode.h

// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

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

UCLASS(minimalapi)
class AUIPracticeGameMode : public AGameModeBase
{
	GENERATED_BODY()

public:
	AUIPracticeGameMode();

	UFUNCTION(BlueprintCallable, Category = "UMG_Game")
	void ChangeMenuWidget(TSubclassOf<UUserWidget> NewWidgetClass);

protected:
	virtual void BeginPlay() override;

	UPROPERTY(EditAnywhere,BlueprintReadOnly,Category="UMG_Game")
	TSubclassOf<UUserWidget> StartingWidgetClass;

	UPROPERTY()
	UUserWidget* currentWidget;
};

GameMode.cpp

// Copyright Epic Games, Inc. All Rights Reserved.

#include "UIPracticeGameMode.h"
#include "UIPracticeCharacter.h"
#include "UObject/ConstructorHelpers.h"
#include <Blueprint/UserWidget.h>

AUIPracticeGameMode::AUIPracticeGameMode()
{
	// set default pawn class to our Blueprinted character
	static ConstructorHelpers::FClassFinder<APawn> PlayerPawnBPClass(TEXT("/Game/ThirdPerson/Blueprints/BP_ThirdPersonCharacter"));
	if (PlayerPawnBPClass.Class != NULL)
	{
		DefaultPawnClass = PlayerPawnBPClass.Class;
	}
}

void AUIPracticeGameMode::BeginPlay()
{
	Super::BeginPlay();
	ChangeMenuWidget(StartingWidgetClass);
}


void AUIPracticeGameMode::ChangeMenuWidget(TSubclassOf<UUserWidget> NewWidgetClass)
{
	if (currentWidget != nullptr)
	{
		currentWidget->RemoveFromViewport();
		currentWidget = nullptr;
	}

	if (NewWidgetClass != nullptr)
	{
		currentWidget = CreateWidget(GetWorld(), NewWidgetClass);
		if (currentWidget != nullptr)
		{
			currentWidget->AddToViewport();
		}
	}
}

UMGPlayerController.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include "MyPlayerController.generated.h"

/**
 * 
 */
UCLASS()
class UIPRACTICE_API UMGPlayerController : public APlayerController
{
	GENERATED_BODY()
	
public:
	virtual void BeginPlay() override;
};

UMGPlayerController.cpp

// Fill out your copyright notice in the Description page of Project Settings.


#include "UMGController.h"

void UMGPlayerController::BeginPlay()
{
	Super::BeginPlay();
	SetInputMode(FInputModeGameAndUI());
}

 

이후 Intro의 버튼에서

다음과같이 구현하면 이전과 같이 동작한다

 

이후, GameMode를 상속받는 블루프린트 클래스와, PlayerController를 상속받는 블루프린트를 생성한다.

 

MenuPlayerController은 Cursor를

MenuGameMode에서는 Starting Widget Class를 설정하고,

Default Pawn Class를 Pawn으로 설정한다.

 

이후 Setting -> World Setting에서,  GameMode overriding을 MenuGameMode로 설정한다

(Level Blueprint 설정X)