Unreal - 비동기 로딩 Level Streaming

2025. 6. 26. 17:03·Unreal5 프로젝트 다이어리

 

만약 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
'Unreal5 프로젝트 다이어리' 카테고리의 다른 글
  • Unreal - Look At (고개 돌리기)
  • Unreal - 컷씬 리터칭
  • Unreal - LandScape 발소리
  • Unreal - BGM, 사운드 관리법
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
  • 인기 글

  • 최근 글

  • 최근 댓글

  • 링크

  • 공지사항

  • 블로그 메뉴

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

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

티스토리툴바