만약 OpenLevel을 하여 레벨을 이동시
트리거가 발동되었을때 로딩하는 동기 로딩방식이기때문에
레벨의 규모가 크다면 렉 이발생합니다
레벨이동 -> 멈춤 -> 레벨오픈
UGameplayStatics::OpenLevel(this, FName(*targetLevelPath));
이럴때 사용하는 방식이 비동기 로딩 Level Streaming
미리 로딩할 맵을 로드 해두고 open하는 방식입니다
제가 구현한 방식은
레벨이동 트리거발동 -> 로딩맵으로 이동 -> 로딩맵에서 로딩바가 진행될때 미리 도착맵을 로드
-> 로딩바가 끝나면 로드된 맵을 Open
1. 레벨이 이동되어도 유지되는 게임인스턴스에 마지막으로 이동할 맵 변수값을 미리 만들어줍니다
UPROPERTY(VisibleAnywhere)
FString lastTargetLevel;
2. 레벨이동을 하는 트리거에서 레벨 게임인스턴스에 저장시킬 도착맵을 선언
UPROPERTY(EditAnywhere, Category = "LevelStreaming")
FString lastTargetLevel;
게임인스턴스에 도착할 맵을 알려주고 로딩맵(targetLoadingMap)으로 이동합니다
void ATeleporterCristal::OpenLevel()
{
//미리 로딩
UStatGameInstance* gameInstance = Cast<UStatGameInstance>(UGameplayStatics::GetGameInstance(this));
if (gameInstance)
{
gameInstance->lastTargetLevel = lastTargetLevel;
}
//로딩맵으로 이동
if (!targetLoadingMap.IsEmpty())
{
UGameplayStatics::OpenLevel(this, FName(*targetLoadingMap));
}
}
3. 로딩맵에서 실행될 로딩전용 위젯이 실행이 된다면 게임인스턴스에 저장된 맵을 미리 로드
virtual void NativeConstruct() override;
LoadStreamLevel로 미리 로드합니다
void ULoadingWidget::NativeConstruct()
{
Super::NativeConstruct();
UStatGameInstance* gameInstance = Cast<UStatGameInstance>(UGameplayStatics::GetGameInstance(this));
if (gameInstance)
{
FName targetLevelName = FName(*gameInstance->lastTargetLevel);
FLatentActionInfo latentInfo;
latentInfo.CallbackTarget = this;
latentInfo.ExecutionFunction = FName("OnLevelLoaded");
latentInfo.Linkage = 0;
latentInfo.UUID = __LINE__;
UGameplayStatics::LoadStreamLevel(this, targetLevelName, true, false, latentInfo);
}
}
4. 로딩바를 구현하고 로딩바가 끝나면 게임인스턴스에 저장된 맵 이름으로 맵을 오픈
void ULoadingWidget::UpdateProgressBar()
{
elapsedTime += 0.01f;
float alpha = elapsedTime / totalTime;
alpha = FMath::Clamp(alpha, 0.0f, 1.0f);
ProgressBar_Loading->SetPercent(alpha);
if (alpha >= 1.0f)
{
GetWorld()->GetTimerManager().ClearTimer(th_Progressbar);
UStatGameInstance* gameInstance = Cast<UStatGameInstance>(UGameplayStatics::GetGameInstance(this));
if (gameInstance)
{
UGameplayStatics::OpenLevel(this, FName(*gameInstance->lastTargetLevel));
}
}
}
이렇게 구현한다면 로딩맵동안 미리 마지막 타겟 맵을 미리 로드할수있게되어 맵을 로드할때 렉이 많이 줄어들게됩니다
결과물
'Unreal5 프로젝트 다이어리' 카테고리의 다른 글
Unreal - Look At (고개 돌리기) (1) | 2025.06.28 |
---|---|
Unreal - 컷씬 리터칭 (0) | 2025.06.27 |
Unreal - LandScape 발소리 (0) | 2025.06.26 |
Unreal - BGM, 사운드 관리법 (0) | 2025.06.21 |
Unreal - Material Parameter Collection 사용하기 (0) | 2025.06.21 |