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; }
FSoftObjectPath
存储到容器中RequestAsyncLoad
加载GetLoadCount
获取加载进度参考部分代码
Unreal Engine 4 —— 异步加载关卡的实现方法及思考
[中文直播]第33期 | UE4资产管理基础1 | Epic 大钊
引擎版本 4.26