UE5基于RumtimeFBXImport插件使用C++加载服务器上fbx文件方法

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);
}
相关推荐
BugPhysician2 天前
影之刃零首发UE崩溃排查:着色器缓存、虚拟内存与驱动版本
ue5·影之刃零·影之刃零报错·影之刃零进不去·ue崩溃·ue引擎崩溃·ue5引擎崩溃
电子云与长程纠缠4 天前
UE5魂系游戏开发教程05
开发语言·学习·游戏·ui·ue5
fanfan_hongyun4 天前
UE5.1 蓝图调用C++方法
ue5
易知微EasyV数据可视化4 天前
UE5 中接入 GDAL:从vcpkg编译到插件打包
ue5
努力学习的小廉4 天前
UE5入门系列(一):环境搭建.md
ue5
fanfan_hongyun4 天前
【UE5.1 创建C++文件】
c++·ue5
努力学习的小廉4 天前
UE5 入门系列(二):编辑器基础操作速通
ue5·编辑器
Дерек的学习记录5 天前
Unreal Engine 5:开关门实现和流程控制
开发语言·学习·ue5
远离UE46 天前
拉格朗日 动力学问题
ue5·图形渲染·图形学·动力学
远离UE410 天前
UE5 Substrate Thin Film 源码拆析
开发语言·javascript·ue5