UE5的RumtimeFBXImport插件其实只能加载本机的fbx文件,要加载服务器上的fbx文件的话,需要先将该fbx文件下载到本地,然后再使用RumtimeFBXImport插件加载。
示例文件如下:
cpp
#include "Loader/WebLoader.h"
#include "Misc/FileHelper.h"
#include "Misc/Paths.h"
#include "HttpModule.h"
#include "Http.h"
#include "FBXImportManager.h"
AWebLoader::AWebLoader(){}
void AWebLoader::BeginPlay()
{
Super::BeginPlay();
if (WebFbxPath.Len() > 0)
{
LoadFileFromServer(WebFbxPath);
}
}
void AWebLoader::LoadFileFromServer(const FString& URL)
{
TSharedRef<IHttpRequest, ESPMode::ThreadSafe> HttpRequest = FHttpModule::Get().CreateRequest();
HttpRequest->SetURL(URL);
HttpRequest->SetVerb("GET");
HttpRequest->OnProcessRequestComplete().BindUObject(this, &AWebLoader::OnFileDownloadComplete);
HttpRequest->ProcessRequest();
}
void AWebLoader::OnFileDownloadComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
if (bWasSuccessful && Response.IsValid() && Response->GetResponseCode() == EHttpResponseCodes::Ok)
{
FString ProjectRoot = FPaths::Combine(FPaths::ProjectDir(), LocalRelatedPath);
FString FileName = FPaths::GetCleanFilename(WebFbxPath);
FString LocalFullPath = FPaths::Combine(ProjectRoot, FileName);
if (!FFileHelper::SaveArrayToFile(Response->GetContent(), *LocalFullPath))
{
UE_LOG(LogTemp, Error, TEXT("File downloaded failed!"));
return;
}
//
LoadLocalFBX(LocalFullPath);
}
else
{
UE_LOG(LogTemp, Error, TEXT("File download failed."));
}
}
void AWebLoader::LoadLocalFBX(FString LocalFullPath)
{
if (!FBXImportManager)
{
FBXImportManager = GetWorld()->SpawnActor<AFBXImportManager>();
}
FBXImportManager->InitializeFBXLoad();
FBXImportManager->ImportFBXFile(LocalFullPath, FVector::UpVector, BaseMaterial, EFBXCollisionType::MeshCollision);
}