C/C++教程

【UE4】异步加载关卡 LoadingScreen ( 蓝图和C++ )

本文主要是介绍【UE4】异步加载关卡 LoadingScreen ( 蓝图和C++ ),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
  • 一般先跳转到一个临时的关卡,然后异步加载目标关卡,同时展示Loading界面
  • 对于含有流关卡的目标关卡,可以先载入子关卡

蓝图异步加载

  • 无进度条

image

C++ 异步加载关卡

LoadPackageAsync

bool bIsLoaded;
	 
UPROPERTY()
	FString LoadPackagePath;

// 用于加载完成的回调
DECLARE_DYNAMIC_DELEGATE(FOnAsyncLoadFinished);

UFUNCTION(BlueprintCallable)
	void MyAsyncLoad(const FOnAsyncLoadFinished& OnAsyncLoadFinished);

UFUNCTION(BlueprintCallable)
	float GetLoadProgress();
//开始异步加载
void ALoadActor::MyAsyncLoad(const FOnAsyncLoadFinished& OnAsyncLoadFinished)
{
//不使用GetBaseFilename,编译 error C4458: LoadPackageAsync failed to begin to load a package···
	LoadPackagePath = FPaths::GetBaseFilename(LoadPackageSoftObjectPath.ToString(), false);

	bIsLoaded = false;

	UE_LOG(LogTemp, Warning, TEXT("String: %s"), *LoadPackagePath);
	LoadPackageAsync(
		LoadPackagePath,
		FLoadPackageAsyncDelegate::CreateLambda([=](const FName& PackageName, UPackage* LoadedPackage, EAsyncLoadingResult::Type Result) 
			{
				if (Result == EAsyncLoadingResult::Failed)
				{
					UE_LOG(LogTemp, Warning, TEXT("Load Failed"));
				}
				else if (Result == EAsyncLoadingResult::Succeeded)
				{
					bIsLoaded = true;
					UE_LOG(LogTemp, Warning, TEXT("Load Succeeded"));
					OnAsyncLoadFinished.ExecuteIfBound();
				}
			}), 0, PKG_ContainsMap);
}

//获取加载的进度 -1 —— 100
float ALoadActor::GetLoadProgress()
{
	float FloatPercentage = GetAsyncLoadPercentage(*LoadPackagePath);
	if (!bIsLoaded)
	{
		FString ResultStr = FString::Printf(TEXT("Percentage: %f"), FloatPercentage);
		GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::Green, ResultStr);
		UE_LOG(LogTemp, Warning, TEXT("Percentage: %f"), FloatPercentage);
	}
	else {
		FloatPercentage = 100;
	}
	return FloatPercentage;	
}

image

效果

image

FStreamableManager

  • 获取依赖项
  • 将依赖项转为 FSoftObjectPath 存储到容器中
  • 使用 RequestAsyncLoad 加载
  • 使用 GetLoadCount 获取加载进度

参考部分代码

image

参考

  • Unreal Engine 4 —— 异步加载关卡的实现方法及思考

  • [中文直播]第33期 | UE4资产管理基础1 | Epic 大钊

  • 引擎版本 4.26

这篇关于【UE4】异步加载关卡 LoadingScreen ( 蓝图和C++ )的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!