esc를 누르면 게임이 일시정지되면서 계속하기 종료하기가 나오는 위젯을 만들어보겠습니다
일단 우선 게임을 실행하고 esc키가 눌려지면 엔진이 종료되니 엔진을 종료하는 단축키를 변경하겠습니다
해당 단축키를 변경해주시면됩니다 저는 shit + esc로 변경하도록 하겠습니다
사용할 위젯을 만들어줍니다
플레이어의 esc 키와 바인드해줍니다
사용할 위젯을 만들어줍니다
esc키를 누르면 위젯이 생성되도록 합니다
단 위젯이 이미 떠있다면 삭제시키고 없으면 띄우는 토글 방식으로 뷰포트에 띄웁니다
UPROPERTY(EditAnywhere, Category = "UI")
TSubclassOf<class UPauseWidget> PauseWidgetClass;
class UPauseWidget* pauseWidget;
if (pauseWidget && pauseWidget->IsInViewport())
{
pauseWidget->RemoveFromParent();
pauseWidget = nullptr;
}
else
{
pauseWidget = CreateWidget<UPauseWidget>(swordPlayerController, PauseWidgetClass);
if (pauseWidget)
{
pauseWidget->AddToViewport();
}
}
디테일적인 버튼 위젯 애니메이션을 추가해줍니다
계속하기를 눌렀을때는 RemoveFromParent()로 위젯을 지워줍니다
RemoveFromParent();
종료하기버튼을 눌렀을때는 OpenLevel로 처음화면으로 이동합니다
페이드아웃 한뒤 메인메뉴창으로 이동합니다
2025.05.15 - [Unreal5 프로젝트 다이어리] - Unreal - 페이드 인 / 아웃
Unreal - 페이드 인 / 아웃
레벨을 이동해보겠습니다레벨을 이동시켜줄 콜리전을 가지고있는 액터를 만들어주었습니다. 오버랩 상호작용할 boxCollision과 루트컴포넌트 씬 컴포넌트를 선언해주고 만들어주었습니다UPROPERTY(
lucodev.tistory.com
esc를 누르면 사운드, 게임이 모든것이 정지되어야합니다
게임모드에서 BGM을 출력하는함수를 수정하겠습니다
오디오 컴포넌트를 사용하여 제어하겠습니다
#include "Components/AudioComponent.h"
UPROPERTY()
UAudioComponent* currentBGM;
매개변수로 들어간 SoundBase의 bgm을 재생시킵니다
void ASwordPlayerGameBase::PlayBGM(USoundBase* bgm)
{
if (currentBGM && currentBGM->IsPlaying())
{
currentBGM->Stop();
}
if (bgm)
{
currentBGM = UGameplayStatics::SpawnSound2D(this, bgm);
if (currentBGM)
{
currentBGM->bIsUISound = false;
currentBGM->SetVolumeMultiplier(1.0f);
currentBGM->SetPitchMultiplier(1.0f);
currentBGM->Play();
}
}
}
해당 사운드를 재생시키거나 뮤트시키는 함수를 만듭니다
void ASwordPlayerGameBase::SetBGMMute(bool bMute)
{
if (currentBGM)
{
if (bMute)
{
currentBGM->SetVolumeMultiplier(0.0f);
}
else
{
currentBGM->SetVolumeMultiplier(1.0f);
}
}
}
게임모드에 만들어놓은 뮤트하는 함수의 bool값을 조절해서 뮤트하거나 재생시킵니다
또한 TimeDilation으로 게임의 정지, 플레이를 제어합니다
void ASwordCharacter::SetPauseWidget()
{
//NoPause
if (pauseWidget && pauseWidget->IsInViewport())
{
pauseWidget->RemoveFromParent();
pauseWidget = nullptr;
gameMode->SetBGMMute(false);
UGameplayStatics::SetGlobalTimeDilation(GetWorld(), 1.0f);
}
//Pause
else
{
pauseWidget = CreateWidget<UPauseWidget>(swordPlayerController, PauseWidgetClass);
if (pauseWidget)
{
pauseWidget->AddToViewport();
gameMode->SetBGMMute(true);
UGameplayStatics::SetGlobalTimeDilation(GetWorld(), 0.0f);
}
}
}
bgm의 사운드 priority를 디폴트값인 1보다 높게해주어 우선순위를 높게 해주었습니다
currentBGM->Priority = 10.0f;
문제가 있습니다
TimeDilation이 0이되었다가 1으로 되돌아오면 bgm이 초기화되버리는 현상이 발생합니다
사운드의 시간을 추적할 변수를 만들어줍니다
float saveBGMPlayTime;
bool bBGMPlaying = false;
tick에서 bgm이 재생중일때 추적할 변수에 deltatime을 누적
if (bBGMPlaying && currentBGM && currentBGM->IsPlaying())
{
saveBGMPlayTime += DeltaTime;
}
saveBGMPlayingTime을 초기화해주고
void ASwordPlayerGameBase::PlayBGM(USoundBase* bgm)
{
if (currentBGM && currentBGM->IsPlaying())
{
currentBGM->Stop();
}
if (bgm)
{
currentBGM = UGameplayStatics::SpawnSound2D(this, bgm);
if (currentBGM)
{
saveBGMPlayTime = 0.f;
bBGMPlaying = true;
currentBGM->bIsUISound = false;
currentBGM->SetVolumeMultiplier(1.0f);
currentBGM->SetPitchMultiplier(1.0f);
currentBGM->Priority = 10.0f;
currentBGM->Play();
}
}
}
SetBGMMute함수를 해당과 같이 수정합니다
void ASwordPlayerGameBase::SetBGMMute(bool bMute)
{
if (currentBGM)
{
if (bMute)
{
currentBGM->Stop();
bBGMPlaying = false;
}
else
{
currentBGM = UGameplayStatics::SpawnSound2D(this, currentBGM->Sound, 1.0f, 1.0f, saveBGMPlayTime);
bBGMPlaying = true;
}
}
}
이제 정지한 시점으로부터 bgm이 잘 나옵니다
(사실 야매방법이기때문에 더 좋은방법있으면 알려주실분 구합니다...)
사운드 추가한 완성본
'Unreal5 프로젝트 다이어리' 카테고리의 다른 글
Unreal - 데미지 오버레이 만들기 (0) | 2025.06.08 |
---|---|
Unreal - 딜레이 프로그래스바 (0) | 2025.06.07 |
Unreal - 위젯 나이아가라 (0) | 2025.06.06 |
Unreal - Foot Step Sound (0) | 2025.05.30 |
Unreal - 위젯에 동영상 파일 추가하기 (인트로) (0) | 2025.05.30 |