Unreal - BGM, 사운드 관리법

2025. 6. 21. 20:06·Unreal5 프로젝트 다이어리

게임모드에서 AudioComponet로 PlaySound2D로 게임 BGM을 실행중인데

여러가지 사운드가 겹치면 BGM이 처음부터 다시 실행하는 에러가있다

 

 

지금 현재 사용하고있는방식이 아니지만

레벨블루프린트에서 Play Sound 2D식으로 재생해도

여러가지 사운드가 겹쳐버리면 처음부터 시작하는 에러가 있다

 

사운드를 PROPERTY로 선언

UPROPERTY(EditAnywhere, Category = "sound")
USoundBase* Stage1BGM;

 

BGM을 재생시키는 함수입니다 

매개변수로 정해진 USoundBase의 bgm 을 재생시킵니다

FadeIn함수로 서서히 커지며 자연스럽게 시작되며 bgm을 재생시킵니다

void ASwordPlayerGameBase::PlayBGM(USoundBase* bgm)
{
	if (!IsValid(currentBGM) || !currentBGM->IsRegistered())
	{
		currentBGM = NewObject<UAudioComponent>(this, UAudioComponent::StaticClass(), TEXT("BGM_AudioComponent"));
		if (!currentBGM) return;

		currentBGM->bAutoActivate = false;
		currentBGM->bIsUISound = false;
		currentBGM->bAllowSpatialization = false;
		currentBGM->bOverridePriority = true;
		currentBGM->Priority = 100.0f;

		currentBGM->RegisterComponentWithWorld(GetWorld());
		currentBGM->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform);
	}

	if (currentBGM->IsPlaying())
	{
		currentBGM->Stop();
	}

	currentBGM->SetSound(bgm);
	float fadeBGMInDuration = 2.0f;
	float bgmVolume = 1.0f;
	float bgmStartTime = 0.0f;
	currentBGM->FadeIn(fadeBGMInDuration, bgmVolume, bgmStartTime);

	//save할떄 쓰던 상태값들
	saveBGMPlayTime = 0.f;
	bBGMPlaying = true;
}

 

EnumClass로 만들어 GameMode에서 원하는 bgm을 선택할수있도록 하겠습니다

UENUM(BlueprintType)
enum class EBGMIndex : uint8
{
	None    UMETA(DisplayName = "None"),
	Town    UMETA(DisplayName = "Town BGM"),
	Library UMETA(DisplayName = "Library BGM"),
	Stage1  UMETA(DisplayName = "Stage 1 BGM"),
	Stage2  UMETA(DisplayName = "Stage 2 BGM"),
	Stage3  UMETA(DisplayName = "Stage 3 BGM")
};

 

UPROPERTY(EditAnywhere, Category = "sound")
USoundBase* townBGM;

UPROPERTY(EditAnywhere, Category = "sound")
USoundBase* libraryBGM;

UPROPERTY(EditAnywhere, Category = "sound")
USoundBase* stage1BGM;

UPROPERTY(EditAnywhere, Category = "sound")
USoundBase* stage2BGM;

UPROPERTY(EditAnywhere, Category = "sound")
USoundBase* stage3BGM;

UPROPERTY(EditAnywhere, Category = "Sound")
EBGMIndex bgmIndex = EBGMIndex::None;

 

Switch문으로 bgmIndex에 따른 PlayBGM함수의 매개변수로 지정된 BGM을 재생시킵니다

switch (bgmIndex)
{
case EBGMIndex::Town:
	if (townBGM) PlayBGM(townBGM);
	break;
case EBGMIndex::Library:
	if (libraryBGM) PlayBGM(libraryBGM);
	break;
case EBGMIndex::Stage1:
	if (stage1BGM) PlayBGM(stage1BGM);
	break;
case EBGMIndex::Stage2:
	if (stage2BGM) PlayBGM(stage2BGM);
	break;
case EBGMIndex::Stage3:
	if (stage3BGM) PlayBGM(stage3BGM);
	break;
default:
	break;
}

 

 

PROPERTY를 할당

 

각각 플레이되는 맵의 GameMode에서 enum을 선택해주시면됩니다

 

 

또한 Concurrency 설정을 해주어 동시사운드 제한을 하여 중복 사운드 찢김현상을 방지합니다

 

더이상 사운드 찢김현상도 없고 사운드가 끊기지도 않는다

 

 

'Unreal5 프로젝트 다이어리' 카테고리의 다른 글

Unreal - 비동기 로딩 Level Streaming  (0) 2025.06.26
Unreal - LandScape 발소리  (0) 2025.06.26
Unreal - Material Parameter Collection 사용하기  (0) 2025.06.21
Unreal - 드래곤 캐릭터 만들기(2)  (3) 2025.06.14
Unreal - 드래곤 캐릭터 만들기 (1)  (0) 2025.06.12
'Unreal5 프로젝트 다이어리' 카테고리의 다른 글
  • Unreal - 비동기 로딩 Level Streaming
  • Unreal - LandScape 발소리
  • Unreal - Material Parameter Collection 사용하기
  • Unreal - 드래곤 캐릭터 만들기(2)
lucodev
lucodev
커피와 노트북 그리고 개발
  • lucodev
    루코 개발테이블
    lucodev
  • 전체
    오늘
    어제
    • 분류 전체보기 (121) N
      • Unreal5 프로젝트 다이어리 (73)
      • Unreal5 프로젝트 다이어리2 (3) N
      • Unreal 팁 (8)
      • Unreal 디버깅 (8)
      • C++ 프로그래머스 다이어리 (21) N
        • Stack (3)
        • Hash (4)
        • Heap (2)
        • Sort (1) N
      • 코드 개인보관함 (8) N
  • 인기 글

  • 최근 글

  • 최근 댓글

  • 링크

  • 공지사항

  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 태그

    언리얼 foot step
    언리얼 비헤이비어트리
    unreal 로딩
    언리얼 로딩
    언리얼 look at
    언리얼 시퀀스
    언리얼 로딩창
    언리얼 페이드 아웃
    언리얼 모션매칭
    언리얼 behaviortree
    unreal loading
    unreal sequence
    언리얼
    언리얼 behavior tree
    언리얼 motionmatching
    unreal 모션매칭
    unreal 컷씬
    unreal 시퀀스
    언리얼 컷씬
    unreal look at
  • hELLO· Designed By정상우.v4.10.3
lucodev
Unreal - BGM, 사운드 관리법
상단으로

티스토리툴바