게임모드에서 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 |